Android編程中調(diào)用Camera時預(yù)覽畫面有旋轉(zhuǎn)問題的解決方法
本文實例講述了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è)計有所幫助。
相關(guān)文章
Android 實現(xiàn)徹底退出自己APP 并殺掉所有相關(guān)的進(jìn)程
這篇文章主要介紹了Android 實現(xiàn)徹底退出自己APP 并殺掉所有相關(guān)的進(jìn)程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03很詳細(xì)的android序列化過程Parcelable
這篇文章主要為大家詳細(xì)介紹了很詳細(xì)的android序列化過程Parcelable,代碼注釋很詳細(xì),感興趣的小伙伴們可以參考一下2016-08-08Android中通過view方式獲取當(dāng)前Activity的屏幕截圖實現(xiàn)方法
這篇文章主要介紹了Android中通過view方式獲取當(dāng)前Activity的屏幕截圖實現(xiàn)方法,本文方法相對簡單,容易理解,需要的朋友可以參考下2014-09-09Android中WebView加載網(wǎng)頁設(shè)置進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android中WebView加載網(wǎng)頁設(shè)置進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-05-0521天學(xué)習(xí)android開發(fā)教程之XML解析與生成
21天學(xué)習(xí)android開發(fā)教程之XML解析與生成,使用SAX來解析XML,在Android里面可以使用SAX和DOM,DOM需要把整個XML文件讀入內(nèi)存再解析,比較消耗內(nèi)存,而SAX基于事件驅(qū)動的處理方式,可以在各節(jié)點觸發(fā)回調(diào)函數(shù),需要的朋友可以參考下2016-02-02