簡體   English   中英

從表面視圖camera2 api獲取相機預覽幀

[英]Get camera preview frame from surface view camera2 api

我一直在嘗試使用camera2 api獲得一天的相機預覽幀,但沒有取得任何成功。 我找到了一些解決方案,但無法實現這些解決方案。 因此,如果任何人都可以提供一種簡單而直接的方式來實現,那就太棒了。

首先,您應該設置目標曲面以從中獲取圖像。

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示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM