gpt4 book ai didi

android - 如何使用 camera2 API 修复 Nexus 5x 上的 "reverse landscape"问题

转载 作者:太空宇宙 更新时间:2023-11-03 10:38:18 25 4
gpt4 key购买 nike

我的应用使用 camera2 API 管理来自设备相机的预览。但问题是我的设备是 Nexus 5x,具有翻转传感器和众所周知的反向景观“问题”。我在某处读到 camera2 api 会“自动”处理此问题,但我认为只有当您在设置捕获 session 时以 Surface View 对象的表面为目标时,这才是正确的。但是我的目标是建立在表面纹理之上的表面,我进一步使用它来渲染预览以获得立体 View ,并且通过这种方法问题仍然存在,我得到了颠倒的帧。这是代码,几乎是使用 camera2 API 时的常规工作流程。

private void openCamera() {
CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
Log.e(TAG, "is camera open");
try {
cameraId = manager.getCameraIdList()[CAMERA_SOURCE];
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
assert map != null;
imageDimension = map.getOutputSizes(SurfaceTexture.class)[CAMERA_SOURCE];
// Add permission for camera and let user grant the permission
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CAMERA_PERMISSION);
return;
}
manager.openCamera(cameraId, stateCallback, null);

} catch (CameraAccessException e) {
e.printStackTrace();
}
Log.e(TAG, "openCamera X");
}

private final CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback() {
@Override
public void onOpened(CameraDevice camera) {
//This is called when the camera is open
Log.e(TAG, "onOpened");
cameraDevice = camera;
createCameraPreview();
}
@Override
public void onDisconnected(CameraDevice camera) {
cameraDevice.close();
}
@Override
public void onError(CameraDevice camera, int error) {
cameraDevice.close();
cameraDevice = null;
}
};

protected void createCameraPreview() {
try {

// Create ImageReader Surface
int max = 2;
mReader = ImageReader.newInstance(mWidth, mHeight, ImageFormat.YUV_420_888, max);
ImageReader.OnImageAvailableListener readerListener = new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader mReader) {
Image image = null;
image = mReader.acquireLatestImage();
if (image == null) {
return;
}

byte[] bytes = convertYUV420ToNV21(image);

nativeVideoFrame(bytes);
image.close();
}
};

mReader.setOnImageAvailableListener(readerListener, mBackgroundHandler);

// Create Texture Surface
texture = createTexture();
mSurfaceTexture = new SurfaceTexture(texture);
mSurfaceTexture.setOnFrameAvailableListener(this);
mSurfaceTexture.setDefaultBufferSize(imageDimension.getWidth(), imageDimension.getHeight());
mSurface = new Surface(mSurfaceTexture);

//Attach surfaces to CaptureRequest
List<Surface> outputSurfaces = new ArrayList<Surface>(2);
outputSurfaces.add(mReader.getSurface());
outputSurfaces.add(mSurface);
captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
captureRequestBuilder.addTarget(mSurface);
captureRequestBuilder.addTarget(mReader.getSurface());

//Define the capture request
cameraDevice.createCaptureSession(outputSurfaces, new CameraCaptureSession.StateCallback(){
@Override
public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
//The camera is already closed
if (null == cameraDevice) {
return;
}
// When the session is ready, we start displaying the preview.
cameraCaptureSessions = cameraCaptureSession;
updatePreview();
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
Toast.makeText(MainActivity.this, "Configuration change", Toast.LENGTH_SHORT).show();
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}

protected void updatePreview() {
if(null == cameraDevice) {
Log.e(TAG, "updatePreview error, return");
}
captureRequestBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
try {
cameraCaptureSessions.setRepeatingRequest(captureRequestBuilder.build(), null, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}

我的问题是:我应该怎么做才能自己解决反向横向问题?我应该在哪一行代码和哪里添加?

谢谢,

JM

最佳答案

由于您使用的是 SurfaceTexture 和 ImageReader,因此您必须自己处理旋转。当与 SurfaceView 或 TextureView 结合使用时,Camera2 API 会自动处理旋转。

也就是说,您可以在通过 ImageReader.OnImageAvailableListener 回调获取帧字节后手动旋转它们,或者甚至更好地直接在 GPU 上对 OpenGL 纹理执行操作。

请注意,旋转 180° 相当于垂直翻转一次和水平翻转一次字节,这在 OpenGL 上意味着可以:

  • 将绘制相机纹理的平面旋转 180°
  • 将绘制相机纹理的平面的 x 和 y 缩放 -1
  • 更改相机平面的 UV

关于android - 如何使用 camera2 API 修复 Nexus 5x 上的 "reverse landscape"问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39126356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com