本文實例講述了Android編程中調(diào)用Camera時預(yù)覽畫面有旋轉(zhuǎn)問題的解決方法。分享給大家供大家參考,具體如下:
在調(diào)用Camera寫應(yīng)用的時候,前后攝像頭的情況有時候是不一樣的。有時候,明明后攝像頭沒有問題,而調(diào)用到前攝像頭時,卻倒轉(zhuǎn)了180°,或者其他角度,百思不得其解。在查看了Android源碼之后,發(fā)現(xiàn)它的解決辦法很是好,接下來貼個源碼,以備日后查看。
public static int getDisplayRotation(Activity activity) { int rotation = activity.getWindowManager().getDefaultDisplay() .getRotation(); switch (rotation) { 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;}public static void setCameraDisplayOrientation(Activity activity, int cameraId, Camera camera) { // See android.hardware.Camera.setCameraDisplayOrientation for // documentation. Camera.CameraInfo info = new Camera.CameraInfo(); Camera.getCameraInfo(cameraId, info); int degrees = getDisplayRotation(activity); int result; if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { result = (info.orientation + degrees) % 360; result = (360 - result) % 360; // compensate the mirror } else { // back-facing result = (info.orientation - degrees + 360) % 360; } camera.setDisplayOrientation(result);}在調(diào)用Camera的時候只要調(diào)用setCameraDisplayOrientation這個方法就可以了。
希望本文所述對大家Android程序設(shè)計有所幫助。
新聞熱點
疑難解答
圖片精選