新的camera2 API让我很困惑.我想开发一个使用该设备相机的应用程序(适用于Android API 10 - 21).如前所述这里,我应该使用"相机" API.
但是,当我尝试将"Camera"API (android.hardware.Camera)添加到清单的用户功能时,它被标记为已弃用.另一方面,我无法将其更改为"camera2"API (android.hardware.camera2),因为它只与Android API 21+(Android 5 - Lollipop)兼容 - 也会链接它,但我只能添加2个链接.
我不仅希望我的应用程序在旧版本的Android上运行,而且还需要最新版本的...
我有一个相机应用程序,锁定到景观.需要时,我会旋转从中获取的原始字节,onPreviewFrame()并使用它们对视频进行编码.
然而,这种方法在Nexus 5X和6设备中失败了,因为它们的反向传感器让我倒置了帧.
随着旋转的预览会不会帮助我在这种情况下,描述在这里:
这不影响在onPreviewFrame(byte [],Camera)中传递的字节数组的顺序
我想知道是否有任何方法可以检测相机传感器是否反转和/或原始帧是否颠倒,因此在旋转时会增加额外的校正.
Camera.setDisplayOrientation 的文档页面包含以下代码示例,声明使用它将"使摄像机图像以与显示相同的方向显示":
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; // …Run Code Online (Sandbox Code Playgroud) 我创建了一个相机应用程序,我希望我的应用程序可以在所有4种可能的方向上转动,并相应地更新相机预览.因为我使用了以下我复制过的方法: Android - 相机预览是横向的
public void updateCameraDisplay(int w, int h) {
// set preview size and make any resize, rotate or
// reformatting changes here
Log.i("CameraPreviews", "Updating camera orientation with w=" + w
+ " and h=" + h);
Parameters parameters = camera.getParameters();
Display display = getActivity().getWindowManager()
.getDefaultDisplay();
int rotation = getActivity().getResources().getConfiguration().orientation;
Log.i("CameraPreviews", "rotation is " + display.getRotation());
if (display.getRotation() == Surface.ROTATION_0) {
parameters.setPreviewSize(h, w);
camera.setDisplayOrientation(0);
}
if (display.getRotation() == Surface.ROTATION_90) {
parameters.setPreviewSize(w, h);
camera.setDisplayOrientation(270);
}
if (display.getRotation() == Surface.ROTATION_180) …Run Code Online (Sandbox Code Playgroud) 我遇到了 CameraX 屏幕旋转支持的问题。
肖像:

风景:

转换代码:
private void updateTransform() {
Log.d(TAG, "updateTransform: ");
Matrix matrix = new Matrix();
float centerX = cameraViewTextureV.getWidth() / 2f;
float centerY = cameraViewTextureV.getHeight() / 2f;
switch (cameraViewTextureV.getDisplay().getRotation()) {
case Surface.ROTATION_0:
rotation = 0;
break;
case Surface.ROTATION_90:
rotation = 90;
break;
case Surface.ROTATION_180:
rotation = 180;
break;
case Surface.ROTATION_270:
rotation = 270;
break;
default:
break;
}
matrix.postRotate((float) -rotation, centerX, centerY);
cameraViewTextureV.setTransform(matrix);
}
Run Code Online (Sandbox Code Playgroud)
所以,正如你在图片中看到的,相机支持屏幕旋转不正确......我updateTransform在屏幕旋转时调用方法......从Android开发者网站的cameraX官方指南中获取此代码。
将非常感谢任何修复建议。祝你今天过得愉快!