Получить кадр предварительного просмотра камеры с поверхности view camera2 api - PullRequest
0 голосов
/ 14 июня 2019

Я пытался получить рамку предварительного просмотра камеры в течение дня, используя camera2 api, но безуспешно.Я нашел несколько решений, но не смог их реализовать.Так что если кто-то может дать простой и прямой способ реализации, это было бы здорово.

1 Ответ

0 голосов
/ 14 июня 2019

Прежде всего, вы должны установить целевую поверхность, чтобы получить изображение с нее.

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

...