休言女子非英物,夜夜龙泉壁上鸣。这篇文章主要讲述使用Android Camera2,应用无限期陷入STATE_WAITING_PRECAPTURE或STATE_WAITING_NON_PRECAPTURE相关的知识,希望能为你提供帮助。
我正在使用Android Camera2 API。我想知道什么时候适合:
- 最初显示TakePicture按钮(即相机已准备好拍摄照片)。
- 再次显示此按钮(如果拍摄过程失败,当然还有完成时),因为我在点击它时隐藏它(捕捉图片)。
takePicture.setVisibility(View.GONE)
和lockFocus()
的takePicture.setVisibility(View.VISIBLE)
尝试过unlockFocus()
。虽然这在大多数时间都有效,但有时按钮会消失并且再也不会出现(当我拍摄照片的过程失败时),特别是当它非常快速地点击它时(一旦它出现在屏幕上)。此时没有错误也没有发生崩溃。代码取自googlesamples/android-Camera2Basic。
更新:
我在
takePicture.setVisibility(View.VISIBLE)
里面尝试过onCaptureSequenceCompleted
。按钮确实出现,但点击它时不会重新启动捕获图片的过程(请参阅this seven-second video)。发生这种情况时,不会调用onCaptureFailed
。然而,由于STATE_WAITING_PRECAPTURE
不满足STATE_WAITING_NON_PRECAPTURE
条件,它无限期地卡在aeState
or if
中。case 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) {
mState = STATE_WAITING_NON_PRECAPTURE;
}
break;
}
case STATE_WAITING_NON_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) {
mState = STATE_PICTURE_TAKEN;
captureStillPicture();
}
break;
}
答案当它没有拍照时,
CameraCaptureSession.CaptureCallback
的onCaptureCompleted
将不会被调用,所以在takePicture.setVisibility(View.VISIBLE)
中调用unlockFocus()
的onCaptureCompleted
将不被调用,然后按钮消失。您应该在捕获请求失败时处理这种情况。
onCaptureFailed
中的onCaptureSequenceCompleted
和CameraCaptureSession.CaptureCallback
将满足您的要求,我更喜欢使用onCaptureSequenceCompleted
,因为无论捕获请求失败还是成功,都会调用它。但是,您可能还需要考虑中止情况,该情况与回调方法onCaptureSequenceAborted
相关。有关完整文档,请参阅CameraCaptureSession.CaptureCallback。
另一答案【使用Android Camera2,应用无限期陷入STATE_WAITING_PRECAPTURE或STATE_WAITING_NON_PRECAPTURE】我认为它应该是这样的,
case STATE_WAITING_PRECAPTURE:
{
JQLog.d(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
|| aeState == CaptureResult.CONTROL_AE_STATE_CONVERGED) {
state = STATE_WAITING_NON_PRECAPTURE;
}
break;
}
推荐阅读
- Android(相机在接受拍摄照片后未关闭)
- 如何记录在Android中打开相机的历史记录
- Android翻盖前置摄像头镜头翻转视频
- Android ACTION_IMAGE_CAPTURE意图
- Android相机照片预览正在逆转
- Android相机预览始终是侧面的
- Android(java.lang.RuntimeException:takePicture失败)
- Android - Things Raspberry Pi - Google移动视觉支持
- Android保存视图到jpg或png