2018-12-14 14:28:47 1686瀏覽
今天扣丁學(xué)堂Android培訓(xùn)老師給大家詳細(xì)介紹一下關(guān)于Android通過ExifInterface判斷Camera圖片方向的方法,首先在Camera相關(guān)應(yīng)用開發(fā)中,有一個(gè)必須搞清楚的知識點(diǎn),就是Camera的預(yù)覽方向和拍照方向圖像的Sensor方向:手機(jī)Camera的圖像數(shù)據(jù)都是來自于攝像頭硬件的圖像傳感器(ImageSensor),這個(gè)Sensor被固定到手機(jī)之后是有一個(gè)默認(rèn)的取景方向的,這個(gè)方向如下圖所示,坐標(biāo)原點(diǎn)位于手機(jī)橫放時(shí)的左上角:
/** * 利用給定路徑下的圖片設(shè)置ImageView * @param imgPath 手機(jī)圖片文件路徑 * @param imgView 需要設(shè)置的ImageView */ public void setImg(String imgPath, ImageView imgView) { File file = new File(imgPath); if (file.exists() && file.canRead()) { // -------1.圖片縮放-------- // 手機(jī)屏幕信息 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; // 設(shè)置之后可以獲取尺寸信息 Bitmap bitmap = BitmapFactory.decodeFile(imgPath, options); // 計(jì)算水平和垂直縮放系數(shù) int heightRatio = (int) Math.ceil(options.outHeight / (float) dh); int widthRatio = (int) Math.ceil(options.outWidth / (float) dw); // 判斷哪個(gè)大 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; // 圖片旋轉(zhuǎn)角度 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) { // 圖片需要旋轉(zhuǎn) 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) { } } }
【關(guān)注微信公眾號獲取更多學(xué)習(xí)資料】
查看更多關(guān)于“Android開發(fā)技術(shù)”的相關(guān)資訊>>