国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android通過ExifInterface判斷Camera圖片方向的方法

2019-12-12 00:24:27
字體:
來源:轉載
供稿:網友

Android的Camera相關應用開發中,有一個必須搞清楚的知識點,就是Camera的預覽方向和拍照方向

圖像的Sensor方向:手機Camera的圖像數據都是來自于攝像頭硬件的圖像傳感器(Image Sensor),這個Sensor被固定到手機之后是有一個默認的取景方向的,這個方向如下圖所示,坐標原點位于手機橫放時的左上角:

android應用里使用相機圖片時必須要考慮的一個問題就是圖片朝向,只有判斷對朝向才能調整圖片從而更好的展現。本文將介紹一種通過ExifInterface判斷圖片朝向的方法!上代碼:

/** * 利用給定路徑下的圖片設置ImageView * @param imgPath 手機圖片文件路徑 * @param imgView 需要設置的ImageView */ public void setImg(String imgPath, ImageView imgView) { File file = new File(imgPath); if (file.exists() && file.canRead()) {  // -------1.圖片縮放--------  // 手機屏幕信息  DisplayMetrics metric = new DisplayMetrics();  getWindowManager().getDefaultDisplay().getMetrics(metric);  int dw = metric.widthPixels; // 屏幕寬  int dh = metric.heightPixels; // 屏幕高  // 加載圖像,只是為了獲取尺寸  BitmapFactory.Options options = new BitmapFactory.Options();  options.inJustDecodeBounds = true; // 設置之后可以獲取尺寸信息  Bitmap bitmap = BitmapFactory.decodeFile(imgPath, options);  // 計算水平和垂直縮放系數  int heightRatio = (int) Math.ceil(options.outHeight / (float) dh);  int widthRatio = (int) Math.ceil(options.outWidth / (float) dw);  // 判斷哪個大  if (heightRatio > 1 && widthRatio > 1) {  if (heightRatio > widthRatio) {   options.inSampleSize = heightRatio;  } else {   options.inSampleSize = widthRatio;  }  }  // 圖片縮放  options.inJustDecodeBounds = false;  bitmap = BitmapFactory.decodeFile(imgPath, options);  // -------2.判斷圖片朝向--------  try {  ExifInterface exif = new ExifInterface(imgPath);  int degree = 0; // 圖片旋轉角度  if (exif != null) {   int orientation = exif.getAttributeInt(    ExifInterface.TAG_ORIENTATION, -1);   if (orientation != -1) {   switch (orientation) {   case ExifInterface.ORIENTATION_ROTATE_90:    degree = 90;    break;   case ExifInterface.ORIENTATION_ROTATE_180:    degree = 180;    break;   case ExifInterface.ORIENTATION_ROTATE_270:    degree = 270;    break;   default:    break;   }   }  }  if (degree != 0) { // 圖片需要旋轉   int width = bitmap.getWidth();   int height = bitmap.getHeight();   Matrix matrix = new Matrix();   matrix.preRotate(degree);   Bitmap mRotateBitmap = Bitmap.createBitmap(bitmap, 0, 0,    width, height, matrix, true);   imgView.setImageBitmap(mRotateBitmap);  } else {   imgView.setImageBitmap(bitmap);  }  } catch (IOException e) {  } } }

本代碼包含兩大功能:

1. 圖片縮放:原始圖片一般比較大,經過縮小才能使用;

2. 圖片旋轉:由于用戶拍照時手機角度不同,所得照片可能需要旋轉。

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對武林網的支持。如果你想了解更多相關內容請查看下面相關鏈接

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 灌阳县| 鱼台县| 华宁县| 图木舒克市| 中江县| 蒲江县| 祁门县| 鄄城县| 广丰县| 南溪县| 九龙坡区| 华安县| 孟村| 高尔夫| 巴林左旗| 蒲城县| 五峰| 房山区| 阿克| 大丰市| 宁德市| 兰溪市| 沧源| 伊金霍洛旗| 广丰县| 内丘县| 汝州市| 疏勒县| 望江县| 洮南市| 溧水县| 通城县| 高邑县| 喀喇| 湘阴县| 蒲江县| 北碚区| 金昌市| 开鲁县| 汶上县| 定远县|