我正在尝试使用Camera2 API将相机数据流式传输到SurfaceView.我正在按照本指南:
Camera2 guide
我无法通过第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 camera)
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.
解决方法
添加到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(); }