Я занимаюсь разработкой приложения для камеры, и проблема в том, что моя камера застряла в том, что после однократного захвата состояние переходит в состояние предварительного захвата, что задерживает мое состояние захвата.
@Override
public void TakePicture() throws CameraAccessException {
LockFocus();
}
/ / Lock
@Override
public void LockFocus() throws CameraAccessException {
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
CameraMetadata.CONTROL_AF_TRIGGER_START);
mState = STATE_WAITING_LOCK;
Capturesession.capture(mPreviewRequestBuilder.build(), new CameraCapture(), mBackground);
}
// Захватить CallBack здесь состояние переходит в STATE_WAITING_PRECAPTURE или в основном в STATE_WAITING_NON_PRECAPTURE
public class CameraCapture extends CameraCaptureSession.CaptureCallback{
@Override
public void onCaptureStarted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, long timestamp, long frameNumber) {
}
@Override
public void onCaptureProgressed(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull CaptureResult partialResult) {
process(partialResult);
}
@Override
public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
process(result);
}
private void process(CaptureResult result) {
switch (mState){
case STATE_PREVIEW:
// We have nothing to do when the camera preview is working normally.
Log.i(TAG, "STATE PREVIEW: ");
break;
case STATE_WAITING_LOCK:
Integer afState =result.get(CaptureResult.CONTROL_AF_STATE);
if (afState==null){
try {
Log.i(TAG, "STATE LOCKING: ");
CaptureRandomImage();
} catch (CameraAccessException e) {
Log.i(TAG, "process: "+e.getMessage()); }
}else if(CaptureRequest.CONTROL_AF_STATE_FOCUSED_LOCKED==afState ||
CaptureRequest.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED==afState
){
Log.i(TAG, "STATE WAITING: ");
Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
Log.i(TAG, "process statw: "+aeState);
if (aeState == null ||
aeState == CaptureResult.CONTROL_AE_STATE_CONVERGED) {
Log.i(TAG, "State picture : ");
mState = STATE_PICTURE_TAKEN;
try {
CaptureRandomImage();
} catch (CameraAccessException e) {
// e.printStackTrace();
}
} else {
Log.i(TAG, "process: runPrecaptureSequence");
runPrecaptureSequence();
}
}
break;
case STATE_WAITING_PRECAPTURE:
Log.i(TAG, "STATE_WAITING_PRECAPTURE: ");
// CONTROL_AE_STATE can be null on some devices
Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
if (aeState == null ||
aeState == CaptureResult.CONTROL_AE_STATE_PRECAPTURE ||
aeState == CaptureRequest.CONTROL_AE_STATE_FLASH_REQUIRED) {
Log.i(TAG, "\"STATE_WAITING_PRECAPTURE null: ");
mState = STATE_WAITING_NON_PRECAPTURE;
}
break;
case STATE_WAITING_NON_PRECAPTURE:
Log.i(TAG, "STATE_WAITING_NON_PRECAPTURE ");
// CONTROL_AE_STATE can be null on some devices
Integer aeeState = result.get(CaptureResult.CONTROL_AE_STATE);
Log.i(TAG, "AE STATE: "+aeeState);
Log.i(TAG, "boolean : "+(aeeState != CaptureResult.CONTROL_AE_STATE_PRECAPTURE));
if (aeeState == null || aeeState != CaptureResult.CONTROL_AE_STATE_PRECAPTURE ) {
mState = STATE_PICTURE_TAKEN;
try {
Log.i(TAG, "STATE_WAITING_NON_PRECAPTURE null ");
CaptureRandomImage();
} catch (CameraAccessException e) {
Log.i(TAG, "State Wait : "+e.getMessage()); }
}
break;
} }
@Override
public void onCaptureFailed(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull CaptureFailure failure) {
super.onCaptureFailed(session, request, failure); }
}
// Capture Image
@Override
public void CaptureRandomImage() throws CameraAccessException {
final Activity activity = (Activity) context;
if (null == activity || null == cameraDevice) {
return;
}
final CaptureRequest.Builder captureBuilder =
cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
captureBuilder.addTarget(mImageReader.getSurface());
captureBuilder.set(CaptureRequest.SCALER_CROP_REGION,rect);
captureBuilder.set(CaptureRequest.CONTROL_AF_MODE,
CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
mRotation = activity.getWindowManager().getDefaultDisplay().getRotation();
captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(mRotation));
CameraCaptureSession.CaptureCallback CaptureCallback
= new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureCompleted(@NonNull CameraCaptureSession session,
@NonNull CaptureRequest request,
@NonNull TotalCaptureResult result) {
unlockFocus();
}
};
Capturesession.stopRepeating();
Capturesession.abortCaptures();
Capturesession.capture(captureBuilder.build(),CaptureCallback, null);
appSounds.setCameraShutterSound();
}
// Разблокировать фокус
@Override
public void unlockFocus() {
try {
// Reset the auto-focus trigger
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
Capturesession.capture(mPreviewRequestBuilder.build(), new CameraCapture(),
mBackground);
// After this, the camera will go back to the normal state of preview.
mState = STATE_PREVIEW;
mPreviewRequestBuilder.set(CaptureRequest.SCALER_CROP_REGION,rect);
Capturesession.setRepeatingRequest(mPreviewRequestBuilder.build(), new CameraCapture(),
mBackground);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}