日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

解决Android 拍照图片被旋转问题

發布時間:2024/3/13 Android 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 解决Android 拍照图片被旋转问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
今天在項目中做拍照上傳頭像相關, 但調用系統相機拍照得到的圖片總是旋轉90度, 在網上找到了兩種答案:
  • 第一種如下, 無奈得到的旋轉角度總是 0 度 , 無法解決旋轉問題
//讀取圖片旋轉角度public static int readPictureDegree(String path) {int degree = 0;try {ExifInterface exifInterface = new ExifInterface(path);int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);LogW.out("readPictureDegree : orientation = " + orientation);if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {degree = 90;} else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {degree = 180;} else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {degree = 270;}} catch (IOException e) {e.printStackTrace();}return degree;}//旋轉圖片public static Bitmap rotateBitmap(int angle, Bitmap bitmap) {Matrix matrix = new Matrix();matrix.postRotate(angle);Bitmap rotation = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),matrix, true);return rotation;}
  • 第二種如下 , 通過比較圖片寬高來旋轉圖片 , 可惜雖然豎著牌的照片能正常顯示, 可一旦將相機橫向拍攝, 展示到界面就多旋轉了 90 度 , 故方法二無效
int width = bitmap.getWidth();int height = bitmap.getHeight();if(width < height) {bitmap = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getWidth());}else {Matrix matrix = new Matrix();matrix.postRotate(90);bitmap = Bitmap.createBitmap(bitmap,0,0,bitmap.getHeight(),bitmap.getHeight(),matrix,true);}

萬般無奈之下, 我無意之間使用方法一中的 ExifInterface 打印了拍照后壓縮前角度信息, toast 打印結果是 6 , 對應常量 ExifInterface.ORIENTATION_ROTATE_90 , 可我記得之前獲取到的旋轉角度是0 , 仔細一想發現獲取角度為0那次操作的是壓縮后的圖片, 我懷疑是不是壓縮將圖片旋轉信息抹掉了.

于是我在壓縮前獲取圖片旋轉信息:

ExifInterface exifInterface = new ExifInterface(finalUserIcon.getAbsolutePath());//finalUserIcon為壓縮前圖片 final int degree = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); UIUtil.debugToast(String.valueOf(degree));

壓縮后將原圖旋轉信息保存替換現有旋轉信息:

try {ExifInterface endEI = new ExifInterface(imgPath);//imgPath為壓縮后圖片路徑endEI.setAttribute(ExifInterface.TAG_ORIENTATION, String.valueOf(degree));endEI.saveAttributes(); } catch (IOException e) {e.printStackTrace(); }

然后采用方法一的方式獲取旋轉信息并糾正旋轉角度:

int degree = readPictureDegree(imgPath);Bitmap bitmap = rotateBitmap(degree, BitmapFactory.decodeFile(imgPath));saveBitmap(imgPath);/** 保存方法 */public static void saveBitmap(String path, Bitmap bm) {File f = new File(path);if (f.exists()) {f.delete();}try {FileOutputStream out = new FileOutputStream(f);bm.compress(Bitmap.CompressFormat.PNG, 90, out);out.flush();out.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}

至此問題得到解決, 無論橫著拍照還是豎著拍照都能正確展示.

總結

以上是生活随笔為你收集整理的解决Android 拍照图片被旋转问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。