我尝试在 Android 中预览的当前帧上做一些 ImageProcessing。对于其他所有智能手机,它的效果都很好。但现在,我必须让 Nexus 5x 可以访问该应用程序。
在那里,我从预览中获得了 180 度旋转的图像。我该如何解决?这是我设置相机和预览的代码:
private void resetCamera(int width, int height){
Log.d(TAG, "Reset Camera!");
CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
for (String cameraId : cameraManager.getCameraIdList()) {
CameraCharacteristics cameraCharacteristics = cameraManager.getCameraCharacteristics(cameraId);
mSensorOrientation = cameraCharacteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
if (cameraCharacteristics.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_FRONT) {
continue;
}
StreamConfigurationMap map = cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
mImageReader = ImageReader.newInstance(width,
height,
ImageFormat.YUV_420_888,
10);
mImageReader.setOnImageAvailableListener(mOnImageAvailableTrack,
mBackgroundHandler);
mPreviewSize = getPreferredPreviewSize(map.getOutputSizes(SurfaceTexture.class), width, height);
mCameraId = cameraId;
return;
}
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
预习:
private void createTrackingPreviewSession(){
try{
SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture();
surfaceTexture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
Surface previewSurface = new Surface(surfaceTexture);
mPreviewCaptureBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
mPreviewCaptureBuilder.addTarget(previewSurface);
mPreviewCaptureBuilder.addTarget(mImageReader.getSurface());
int rotation = getWindowManager().getDefaultDisplay().getRotation();
mPreviewCaptureBuilder.set(CaptureRequest.JPEG_ORIENTATION,
getOrientation(rotation));
mCameraDevice.createCaptureSession(Arrays.asList(previewSurface, mImageReader.getSurface()),
new CameraCaptureSession.StateCallback(){
@Override
public void onConfigured(CameraCaptureSession session) {
if(mCameraDevice == null){
return;
}
try{
Toast.makeText(getApplicationContext(), "Create Tracking Session", Toast.LENGTH_SHORT).show();
mPreviewCapture = mPreviewCaptureBuilder.build();
mCaptureSession = session;
mCaptureSession.setRepeatingRequest(
mPreviewCapture,
mSessionCaptureCallback,
mBackgroundHandler
);
} catch(CameraAccessException e){
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(CameraCaptureSession session) {
Toast.makeText(
getApplicationContext(),
"create Camera session failed!",
Toast.LENGTH_SHORT
).show();
}
}, null);
} catch(CameraAccessException e){
e.printStackTrace();
}
}
如您所见,我尝试使用该修复程序来捕获图像,但它并没有帮助我仍然从预览中获得转换后的图像。
如果可能的话,我想要一个尝试更改相机设置的解决方案。通过绑定的 ImageReader 获得图像后,我不想自己转动图像。