網站首頁 編程語言 正文
本文實例為大家分享了Android?Camera實現旋轉角度的具體代碼,供大家參考,具體內容如下
概述
相機圖像數據都是來自于圖像傳感器(Image Sensor
),相機模組出廠的時候有一個默認的取景方向,一般為以下兩種,請留意相機模組中小人的方向
-
Sensor
安裝默認都是Sensor
的長邊與手機的長邊平行 - 將上述圖1的模組裝入手機,結果為下圖
- 兩顆模組不一定如圖左右擺放,也可以上下擺放,只要遵循長邊對長邊即可
- 此時使用后攝預覽或拍照,取景方向是正常的,而手機目前相對自然方向(正常豎屏使用狀態)順時針夾角為90度,這也就是常說的 Sensor orientation 是90度
將上述圖2的模組裝入手機,結果為下圖
- 兩顆模組不一定如圖左右擺放,也可以上下擺放,只要遵循長邊對長邊即可
- 此時使用后攝預覽或拍照,若要使取景方向正常,需將手機順時針旋轉180度,此時手機相對自然方向(正常豎屏使用狀態)順時針夾角為270度,這也就是常說的
Sensor orientation
是270度
旋轉角度規律
- 以下說明以
Sensor orientation
90度為例(大多數sensor都是該情況) - 屏幕顯示旋轉角度:Activity#getWindowManager().getDefaultDisplay().getRotation()的值,可以是
ROTATION_0
(正常豎屏使用狀態)、ROTATION_90
(手機向右側放)、ROTATION_180
(手機豎屏倒置)、ROTATION_270
(手機向左側放) - 以屏幕角度
ROTATION_180
且使用后攝為例,其他情況類比推理
當前情況下圖1模組中的小人頭部朝向左邊,有兩種方式判斷當前sensor取景后圖像方向
簡單方式:跟隨小人的視角去看實際被拍攝的物體(假設為正常站立的人),所看到的景象是頭部向右橫置的人,此時若要使看到的圖像恢復為正常情況,則需要將圖像順時針旋轉270度
復雜方式:sensor掃描方向遵從小人頭部左側頂點向右掃描,當前情況下也就是從左下向上逐行掃描,然后依次存儲到內存中,存儲為圖片的時候是水平從左向右存儲,導致存儲后的圖像是頭部向右橫置的人,若要使圖像被拍攝后為正常情況,則需要將圖像順時針旋轉270度
代碼實現
Camera API1(官方實現)
public static void setCameraDisplayOrientation(Activity activity, int cameraId, android.hardware.Camera camera) { ?? ?android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo(); ? ? android.hardware.Camera.getCameraInfo(cameraId, info); ? ? int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); ? ? int degrees = 0; ? ? switch (rotation) { ? ? ? ? case Surface.ROTATION_0: degrees = 0; break; ? ? ? ? case Surface.ROTATION_90: degrees = 90; break; ? ? ? ? case Surface.ROTATION_180: degrees = 180; break; ? ? ? ? case Surface.ROTATION_270: degrees = 270; break; ? ? } ? ? 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); }
Camera API2
Camera API2
不需要經過任何預覽畫面方向的矯正,就可以正確現實畫面,因為當使用 TextureView
或者 SurfaceView
進行畫面預覽的時候,系統會自動矯正預覽畫面的方向
private static final SparseIntArray ORIENTATIONS = new SparseIntArray(); // Conversion from screen rotation to JPEG orientation. static { ? ? ORIENTATIONS.append(Surface.ROTATION_0, 90); ? ? ORIENTATIONS.append(Surface.ROTATION_90, 0); ? ? ORIENTATIONS.append(Surface.ROTATION_180, 270); ? ? ORIENTATIONS.append(Surface.ROTATION_270, 180); } /** ?* Retrieves the JPEG orientation from the specified screen rotation. ?* ?* @param rotation The screen rotation. ?* @return The JPEG orientation (one of 0, 90, 270, and 360) ?*/ private int getOrientation(int rotation) { ? ? // Sensor orientation is 90 for most devices, or 270 for some devices (eg. Nexus 5X) ? ? // We have to take that into account and rotate JPEG properly. ? ? // For devices with orientation of 90, we simply return our mapping from ORIENTATIONS. ? ? // For devices with orientation of 270, we need to rotate the JPEG 180 degrees. ? ? return (ORIENTATIONS.get(rotation) + mSensorOrientation + 270) % 360; } final CaptureRequest.Builder captureBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE); int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(rotation));
原文鏈接:https://blog.csdn.net/zhendong_hu/article/details/103875896
相關推薦
- 2022-08-06 一次性徹底講透Python中pd.concat與pd.merge_python
- 2022-04-29 C++對象排序的比較你了解嗎_C 語言
- 2022-10-23 python?groupby函數實現分組選取最大值與最小值_python
- 2022-05-04 基于Python中的turtle繪畫星星和星空_python
- 2022-07-19 mybatis plus 代碼生成器配置
- 2022-07-09 嵌入式linux使用trace調試步驟記錄
- 2022-03-16 .NET6自定義WebAPI過濾器_實用技巧
- 2022-08-01 Flask-SQLALchemy基本使用方法_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支