一、預覽時正確顯示java
主要參考系統相機代碼實現getDisplayOritation就能夠了數據庫
//在preview以前調用setDisplayOrientationide
int degrees = getDisplayOritation(getDispalyRotation(), cameraId); mCamera.setDisplayOrientation(degrees); mCamera.startPreview();
getDisplayOritation函數以下:函數
private int getDisplayOritation(int degrees, int cameraId) { Camera.CameraInfo info = new Camera.CameraInfo(); Camera.getCameraInfo(cameraId, info); int result; if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { result = (info.orientation + degrees) % 360; result = (360 - result) % 360; } else { result = (info.orientation - degrees + 360) % 360; } return result; } private int getDispalyRotation() { int i = getWindowManager().getDefaultDisplay().getRotation(); switch (i) { case Surface.ROTATION_0: return 0; case Surface.ROTATION_90: return 90; case Surface.ROTATION_180: return 180; case Surface.ROTATION_270: return 270; } return 0; }
二、顯示圖片時正確顯示spa
豎屏拍照的照片,直接使用的話,會旋轉90度code
參考系統圖庫的代碼,須要先查詢mediascanner的orientation字段,而後應用再把角度旋轉過來,這樣顯示就ok了orm
參考代碼以下:圖片
假設c爲查詢mediaprovider數據庫返回的cursorci
int rotation = c.getInt(c.getColumnIndex(MediaStore.Images.ImageColumns.ORIENTATION)); if (rotation != 0) { Bitmap bitmap = BitmapFactory.decodeFile(path); imageBefore.setImageBitmap(bitmap); Matrix m = new Matrix(); m.setRotate(rotation); Bitmap transformed = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true); return transformed; }