如何解决在 Android 上获取预览会话的相机对焦状态
我在 Android 上编写了一个使用相机的应用。它在 SurfaceTexture 上创建预览,并使用 PreviewBulder / CaptureRequestBuilder / RepeatingRequest。我需要知道焦点是否已解决,但我只找到了在 Capturesession 期间获取焦点状态的方法。有谁知道如何在预览期间实现这一目标?我不使用任何捕获,而是使用预览中的帧。
感谢您的帮助:)
对应的代码部分(对不起,它不可爱:)):
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;
}
};
final CameraCaptureSession.CaptureCallback captureCallbackListener = new CameraCaptureSession.CaptureCallback() {
@override
public void onCaptureCompleted(CameraCaptureSession session,CaptureRequest request,TotalCaptureResult result) {
super.onCaptureCompleted(session,request,result);
// makeText(MainActivity.this,"Saved:" + file,LENGTH_SHORT).show();
createCameraPreview();
}
};
protected void startBackgroundThread() {
mBackgroundThread = new HandlerThread("Camera Background");
mBackgroundThread.start();
mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
}
protected void stopBackgroundThread() {
mBackgroundThread.quitSafely();
try {
mBackgroundThread.join();
mBackgroundThread = null;
mBackgroundHandler = null;
} catch (InterruptedException e) {
e.printstacktrace();
}
}
protected void createCameraPreview() {
try {
SurfaceTexture texture = textureView.getSurfaceTexture();
assert texture != null;
texture.setDefaultBufferSize(imageDimension.getWidth(),imageDimension.getHeight());
Surface surface = new Surface(texture);
CameraManager manager;
manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
String camerId = manager.getCameraIdList()[0];
Cameracharacteristics characteristics = manager.getCameracharacteristics(camerId);
boolean aelockavailable = characteristics.get(Cameracharacteristics.CONTROL_AE_LOCK_AVAILABLE);
} catch (Exception e) {}
captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
captureRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,captureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
//captureRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,captureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
captureRequestBuilder.addTarget(surface);
cameraDevice.createCaptureSession(Arrays.asList(surface),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) {
//makeText(MainActivity.this,"Configuration change",LENGTH_SHORT).show();
}
},null);
} catch (CameraAccessException e) {
e.printstacktrace();
}
}
void openCamera() {
CameraManager manager = (CameraManager)
getSystemService(Context.CAMERA_SERVICE);
//Log.e(TAG,"is camera open");
try {
cameraId = manager.getCameraIdList()[0];
Cameracharacteristics characteristics =
manager.getCameracharacteristics(cameraId);
StreamConfigurationMap map =
characteristics.get(Cameracharacteristics.SCALER_STREAM_CONfigURATION_MAP);
assert map != null;
imageDimension = map.getoutputSizes(SurfaceTexture.class)[0];
// 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");
}
void updatePreview() {
if (null == cameraDevice) {
//Log.e(TAG,"updatePreview error,return");
}
captureRequestBuilder.set(CaptureRequest.CONTROL_MODE,CameraMetadata.CONTROL_MODE_AUTO);
captureRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,captureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
//captureRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
if (flashchanged) {
flashchanged = false;
if (lamp && autoflash) {
captureRequestBuilder.set(CaptureRequest.FLASH_MODE,CaptureRequest.FLASH_MODE_TORCH);
} else {
captureRequestBuilder.set(CaptureRequest.FLASH_MODE,CaptureRequest.FLASH_MODE_OFF);
}
}
try {
cameraCaptureSessions.setRepeatingRequest(captureRequestBuilder.build(),null,mBackgroundHandler);
} catch (CameraAccessException e) {
e.printstacktrace();
}
}
private void closeCamera() {
if (null != cameraDevice) {
cameraDevice.close();
cameraDevice = null;
}
if (null != imageReader) {
imageReader.close();
imageReader = null;
}
}```
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。