8

我正在尝试使用Camera2API 将相机数据流式传输到SurfaceView. 我正在关注本指南:Camera2 指南

我无法过去step 5

MainActivity.java::onCreate()

setContentView(R.layout.activity_main);

surfaceView = (SurfaceView)findViewById(R.id.surface);
manager = (CameraManager)getSystemService(Context.CAMERA_SERVICE);

MainActivity.java::onClick()

for (String id : manager.getCameraIdList()) {
    CameraCharacteristics characteristics = manager.getCameraCharacteristics(id);

    Integer direction = characteristics.get(CameraCharacteristics.LENS_FACING);

    if (direction != null && direction == CameraCharacteristics.LENS_FACING_BACK) {
        if (checkCallingOrSelfPermission("android.permission.CAMERA") == PackageManager.PERMISSION_GRANTED)
            manager.openCamera(id, new StateCallback(), null);

        break;
    }
}

MainActivity.java.StateCallback::onOpened(CameraDevice 相机)

List<Surface> surfaces = new LinkedList<>();
surfaces.add(surfaceView.getHolder().getSurface());

CaptureRequest.Builder builder = camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
builder.addTarget(surfaces.get(0));

camera.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() {
    @Override
    public void onConfigured(CameraCaptureSession session) {
        Log.i(TAG, "Configured");
    }

    @Override
    public void onConfigureFailed(CameraCaptureSession session) {
        Log.e(TAG, "Configured failed"); // Ends up in this function :(
    }
    }, null);

程序以onConfigureFailed()函数结束。我不知道可能是什么错误,也不知道如何检查是什么。

我的猜测是我在 中遗漏了一些东西CaptureRequest,但我不知道是什么。

我在Samsung Galaxy S4上运行。

4

2 回答 2

0

像这样覆盖onConfigureFailed()

   @Override
   public void onConfigureFailed(CameraCaptureSession session) {
       ImageReader mReader = ImageReader.newInstance(640, 480, ImageFormat.JPEG, 1);
       takePicture() // function to get image
       createCameraPreview(); // function to set camera Preview on screen
   }

调用 createCameraPreview 函数重启相机,否则会卡住。您可以使用新值更改 ImageReader

ImageReader mReader = ImageReader.newInstance(640, 480, ImageFormat.JPEG, 1);

并再次调用该takePicture()函数,以便用户不必再次单击以捕获图像。

于 2019-06-10T10:41:13.700 回答
0

添加到 onConfigured:

  if (null == cameraDevice) {
        Log.e(TAG, "updatePreview error, return");
        return;
    }
    captureRequestBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
    try {
        cameraCaptureSessions.setRepeatingRequest(captureRequestBuilder.build(), null, mBackgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
于 2017-04-12T14:20:21.760 回答