从Surface View Camera2 API获取相机预览框

时间:2019-06-14 06:13:11

标签: android camera frame qr-code android-camera2

我一直在尝试使用camera2 api获取一天的相机预览帧,但没有成功。我找到了一些解决方案,但无法实现。因此,如果任何人都可以给出一种简单而直接的方法来实现,那将是很好的。

1 个答案:

答案 0 :(得分:0)

首先,应设置目标表面以从中获取图像。

mCaptureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);

imageReader =  ImageReader.newInstance(currentWidth, currentHeight, ImageFormat.JPEG, MAX_IMAGES);

List<Surface> outputSurfaces = new ArrayList<>();

outputSurfaces.add(imageReader.getSurface()); // here add as many surface as you want
mCaptureRequestBuilder.addTarget()//this function allow you to set targets

,此回调允许您检索框架

readerListener = new ImageReader.OnImageAvailableListener() {
                @Override
                public void onImageAvailable(ImageReader reader) {

                  image = reader.acquireLatestImage();
                if(image == null){
                    //System.out.println("it is null image"); // image reader did not get new image
                    return;
                }
                Image.Plane[] planes = image.getPlanes();
                  if(planes[0].getBuffer() == null){ // here 0 indicates first target I set in outputSurfaces list
                      System.out.println("it is null object reference of planes");
                    return;
                  }
                 //.... do whatever you want here

                //After you are done with an image then close it 
               image.close(); 

               }
}

有关完整示例,请参阅Google samples