Android?Camera實(shí)現(xiàn)旋轉(zhuǎn)角度
本文實(shí)例為大家分享了Android Camera實(shí)現(xiàn)旋轉(zhuǎn)角度的具體代碼,供大家參考,具體內(nèi)容如下
概述
相機(jī)圖像數(shù)據(jù)都是來(lái)自于圖像傳感器(Image Sensor),相機(jī)模組出廠的時(shí)候有一個(gè)默認(rèn)的取景方向,一般為以下兩種,請(qǐng)留意相機(jī)模組中小人的方向


Sensor安裝默認(rèn)都是Sensor的長(zhǎng)邊與手機(jī)的長(zhǎng)邊平行- 將上述圖1的模組裝入手機(jī),結(jié)果為下圖

- 兩顆模組不一定如圖左右擺放,也可以上下擺放,只要遵循長(zhǎng)邊對(duì)長(zhǎng)邊即可
- 此時(shí)使用后攝預(yù)覽或拍照,取景方向是正常的,而手機(jī)目前相對(duì)自然方向(正常豎屏使用狀態(tài))順時(shí)針夾角為90度,這也就是常說(shuō)的 Sensor orientation 是90度
將上述圖2的模組裝入手機(jī),結(jié)果為下圖

- 兩顆模組不一定如圖左右擺放,也可以上下擺放,只要遵循長(zhǎng)邊對(duì)長(zhǎng)邊即可
- 此時(shí)使用后攝預(yù)覽或拍照,若要使取景方向正常,需將手機(jī)順時(shí)針旋轉(zhuǎn)180度,此時(shí)手機(jī)相對(duì)自然方向(正常豎屏使用狀態(tài))順時(shí)針夾角為270度,這也就是常說(shuō)的
Sensor orientation是270度
旋轉(zhuǎn)角度規(guī)律
- 以下說(shuō)明以
Sensor orientation90度為例(大多數(shù)sensor都是該情況) - 屏幕顯示旋轉(zhuǎn)角度:Activity#getWindowManager().getDefaultDisplay().getRotation()的值,可以是
ROTATION_0(正常豎屏使用狀態(tài))、ROTATION_90(手機(jī)向右側(cè)放)、ROTATION_180(手機(jī)豎屏倒置)、ROTATION_270(手機(jī)向左側(cè)放) - 以屏幕角度
ROTATION_180且使用后攝為例,其他情況類(lèi)比推理
當(dāng)前情況下圖1模組中的小人頭部朝向左邊,有兩種方式判斷當(dāng)前sensor取景后圖像方向
簡(jiǎn)單方式:跟隨小人的視角去看實(shí)際被拍攝的物體(假設(shè)為正常站立的人),所看到的景象是頭部向右橫置的人,此時(shí)若要使看到的圖像恢復(fù)為正常情況,則需要將圖像順時(shí)針旋轉(zhuǎn)270度
復(fù)雜方式:sensor掃描方向遵從小人頭部左側(cè)頂點(diǎn)向右掃描,當(dāng)前情況下也就是從左下向上逐行掃描,然后依次存儲(chǔ)到內(nèi)存中,存儲(chǔ)為圖片的時(shí)候是水平從左向右存儲(chǔ),導(dǎo)致存儲(chǔ)后的圖像是頭部向右橫置的人,若要使圖像被拍攝后為正常情況,則需要將圖像順時(shí)針旋轉(zhuǎn)270度
代碼實(shí)現(xiàn)
Camera API1(官方實(shí)現(xiàn))
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 不需要經(jīng)過(guò)任何預(yù)覽畫(huà)面方向的矯正,就可以正確現(xiàn)實(shí)畫(huà)面,因?yàn)楫?dāng)使用 TextureView 或者 SurfaceView 進(jìn)行畫(huà)面預(yù)覽的時(shí)候,系統(tǒng)會(huì)自動(dòng)矯正預(yù)覽畫(huà)面的方向
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));以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android通過(guò)繼承Binder類(lèi)實(shí)現(xiàn)多進(jìn)程通信
本篇文章主要介紹了Android通過(guò)繼承Binder類(lèi)實(shí)現(xiàn)多進(jìn)程通信,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03
Android Jetpack組件中LiveData的優(yōu)劣
LiveData是Jetpack組件的一部分,更多的時(shí)候是搭配ViewModel來(lái)使用,相對(duì)于Observable,LiveData的最大優(yōu)勢(shì)是其具有生命感知的,換句話(huà)說(shuō),LiveData可以保證只有在組件(?Activity、Fragment、Service)處于活動(dòng)生命周期狀態(tài)的時(shí)候才會(huì)更新數(shù)據(jù)2023-04-04
android自定義view實(shí)現(xiàn)鐘表效果
這篇文章主要為大家詳細(xì)介紹了android自定義view實(shí)現(xiàn)鐘表效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12
Android中ViewFlipper和AdapterViewFlipper使用的方法實(shí)例
ViewFlipper和AdapterViewFlipper是Android自帶的一個(gè)多頁(yè)面管理控件,下面這篇文章主要給大家介紹了關(guān)于Android中ViewFlipper和AdapterViewFlipper使用的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
Android中Textview和圖片同行顯示(文字超出用省略號(hào),圖片自動(dòng)靠右邊)
Android中Textview和圖片同行顯示,文字超出用省略號(hào)顯示,圖片自動(dòng)靠右邊??吹竭@個(gè)問(wèn)題本來(lái)認(rèn)為是一個(gè)很正常的需求,看起來(lái)很簡(jiǎn)單,但是做起來(lái)卻遇到了很蛋疼的問(wèn)題,怎么搞的都不行,堵了很長(zhǎng)時(shí)間,下面說(shuō)一下解決的方案,希望遇到這樣問(wèn)題的朋友可以使用。2016-12-12
利用Warensoft Stock Service編寫(xiě)高頻交易軟件
本文主要介紹了利用Warensoft Stock Service編寫(xiě)高頻交易軟件的方法步驟,具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01

