本文实例为大家分享了Android Camera实现旋转角度的具体代码,供大家参考,具体内容如下 概述 相机图像数据都是来自于图像传感器(Image Sensor),相机模
本文实例为大家分享了Android Camera实现旋转角度的具体代码,供大家参考,具体内容如下
相机图像数据都是来自于图像传感器(Image Sensor
),相机模组出厂的时候有一个默认的取景方向,一般为以下两种,请留意相机模组中小人的方向
Sensor
安装默认都是 Sensor
的长边与手机的长边平行将上述图2的模组装入手机,结果为下图
Sensor orientation
是270度Sensor orientation
90度为例(大多数sensor都是该情况)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);
}
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));
--结束END--
本文标题: Android Camera实现旋转角度
本文链接: https://www.lsjlt.com/news/164722.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0