title: Android camera2 API - Display processed frame in real time
body: <p>I'm trying to create an app that processes camera images in real time and displays them on screen. I'm using the camera2 API. I have created a native library to process the images using OpenCV.</p>
  
  <p>So far I have managed to set up an ImageReader that receives images in YUV_420_888 format like this.</p>
  
  <pre><code>        mImageReader = ImageReader.newInstance(
                  mPreviewSize.getWidth(),
                  mPreviewSize.getHeight(),
                  ImageFormat.YUV_420_888,
                  4);
          mImageReader.setOnImageAvailableListener(mOnImageAvailableListener, mImageReaderHandler);
  </code></pre>
  
  <p>From there I'm able to get the image planes (Y, U and V), get their ByteBuffer objects and pass them to my native function. This happens in the <code>mOnImageAvailableListener</code>:</p>
  
  <pre><code>        Image image = reader.acquireLatestImage();
  
          Image.Plane[] planes = image.getPlanes();
          Image.Plane YPlane = planes[0];
          Image.Plane UPlane = planes[1];
          Image.Plane VPlane = planes[2];
  
          ByteBuffer YPlaneBuffer = YPlane.getBuffer();
          ByteBuffer UPlaneBuffer = UPlane.getBuffer();
          ByteBuffer VPlaneBuffer = VPlane.getBuffer();
  
          myNativeMethod(YPlaneBuffer, UPlaneBuffer, VPlaneBuffer, w, h);
  
          image.close();
  </code></pre>
  
  <p>On the native side I'm able to get the data pointers from the buffers, create a <code>cv::Mat</code> from the data and perform the image processing. </p>
  
  <p>Now the next step would be to show my processed output, but I'm unsure how to show my processed image. Any help would be greatly appreciated.</p>