Android編程中調(diào)用Camera時預覽畫面有旋轉問題的解決方法
本文實例講述了Android編程中調(diào)用Camera時預覽畫面有旋轉問題的解決方法。分享給大家供大家參考,具體如下:
在調(diào)用Camera寫應用的時候,前后攝像頭的情況有時候是不一樣的。有時候,明明后攝像頭沒有問題,而調(diào)用到前攝像頭時,卻倒轉了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程序設計有所幫助。
相關文章
Android 實現(xiàn)徹底退出自己APP 并殺掉所有相關的進程
這篇文章主要介紹了Android 實現(xiàn)徹底退出自己APP 并殺掉所有相關的進程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android中通過view方式獲取當前Activity的屏幕截圖實現(xiàn)方法
這篇文章主要介紹了Android中通過view方式獲取當前Activity的屏幕截圖實現(xiàn)方法,本文方法相對簡單,容易理解,需要的朋友可以參考下2014-09-09Android中WebView加載網(wǎng)頁設置進度條
這篇文章主要為大家詳細介紹了Android中WebView加載網(wǎng)頁設置進度條,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-05-05