我尝试在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获取图像后,我不想自己转动图像。