gpt4 book ai didi

android - Camera 2 - API - 使用前置摄像头时图像捕捉不工作?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:57:56 24 4
gpt4 key购买 nike

在我的相机应用程序中,我有一个按钮可以将相机的正面或背面更改为正面或背面,我可以使用后置摄像头拍摄和保存图像,但是当我切换到前置摄像头时,我无法拍摄图像。这就是我将相机切换到前置或后置的方式。

   ImageView switch_camera =(ImageView) rootview.findViewById(R.id.imageView7);
switch_camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


// facing = characteristics.get(CameraCharacteristics.LENS_FACING);

if (facing != null && facing == CameraCharacteristics.LENS_FACING_FRONT) {
//isfrontcam=true;
try {

//manager.openCamera(getBackFacingCameraId(manager), mStateCallback, mBackgroundHandler);
closeCamera();
openCamera(mTextureView.getWidth(), mTextureView.getHeight(),"0");
Log.e("opening ","BackCam");
facing = 1;

} catch (SecurityException e) {
e.printStackTrace();

} catch (Exception e) {
e.printStackTrace();

}
} else if (facing != null && facing == CameraCharacteristics.LENS_FACING_BACK) {
// isfrontcam = true;
try {
//manager.openCamera(getFrontFacingCameraId(manager), mStateCallback, mBackgroundHandler);

// characteristics = manager.getCameraCharacteristics("1");

closeCamera();
openCamera(mTextureView.getWidth(), mTextureView.getHeight(),"1");

Log.e("opening ", "FrontCam");
String str = getBackFacingCameraId(manager);
facing= 0;
Log.e("str", "id" + str);

} catch (SecurityException e) {
e.printStackTrace();

} catch (Exception e) {
e.printStackTrace();
}
}

单击捕获按钮时,我将调用此函数来捕获图像;

 private void lockFocus() {
try {
// This is how to tell the camera to lock focus.
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
CameraMetadata.CONTROL_AF_TRIGGER_START);
// Tell #mCaptureCallback to wait for the lock.
mState = STATE_WAITING_LOCK;
mCaptureSession.capture(mPreviewRequestBuilder.build(),mCaptureCallback,
mBackgroundHandler);
} catch (CameraAccessException e) {

e.printStackTrace();
}
}

最佳答案

检查您的 CameraCaptureSession.CaptureCallback :可能相机的状态为 CONTROL_AF_STATE_INACTIVE。当它在等待对焦时...永远不会拍摄照片。

应该是这样的

            case STATE_WAITING_LOCK: {
Integer afState = result.get(CaptureResult.CONTROL_AF_STATE);
if (afState == null) {
captureStillPicture();
} else if (CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED == afState ||
CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED == afState ||
CaptureResult.CONTROL_AF_STATE_INACTIVE == afState /*add this*/) {
// 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_CONVERGED) {
mState = STATE_PICTURE_TAKEN;
captureStillPicture();
} else {
runPrecaptureSequence();
}
}
break;
}

关于android - Camera 2 - API - 使用前置摄像头时图像捕捉不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35477938/

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