title: How to transform each YUV_420_888 frame to RGB efficiently (e.g., using renderscript yuv2rgb.rs) in Android camera2?
body: <p>I am building a image processing program using Android camera2. Since the image format of each captured frame is YUV_420_888, I need to transform it to RGB efficiently for image processing. I googled and read a lot (especially the following two links), and finally found that renderscript may be the solution. However, I don't know how to use the yuv2rgb script in my code.</p>
  
  <p><a href="http://werner-dittmann.blogspot.jp/2016/03/using-android-renderscript-to-convert.html" rel="nofollow noreferrer">http://werner-dittmann.blogspot.jp/2016/03/using-android-renderscript-to-convert.html</a></p>
  
  <p><a href="https://stackoverflow.com/questions/28430024/convert-android-media-image-yuv-420-888-to-bitmap">Convert android.media.Image (YUV_420_888) to Bitmap</a></p>
  
  <p>Currently, I use the TextureView surface to show the preview, and use ImageReader to capture each YUV_420_888 frame in onImageAvailable function.</p>
  
  <pre><code>   protected void createCameraPreview() {
      try {
          SurfaceTexture texture = textureView.getSurfaceTexture();
          assert texture != null;
          texture.setDefaultBufferSize(imageDimension.getWidth(), imageDimension.getHeight());
          Surface surface = new Surface(texture);
          Surface mImageSurface = mImageReader.getSurface();
          captureRequestBuilder =    cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
          captureRequestBuilder.addTarget(surface)
          List surfaces = new ArrayList&lt;&gt;();
          surfaces.add(surface);
          surfaces.add(mImageSurface);
          captureRequestBuilder.addTarget(mImageSurface);
       cameraCaptureSessions.setRepeatingRequest(captureRequestBuilder.build(), null, mBackgroundHandler);
  
          cameraDevice.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback(){
                      @Override
                      public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
                          //The camera is already closed
                          if (null == cameraDevice) {
                              return;
                          }
                          // When the session is ready, we start displaying the preview.
                          cameraCaptureSessions = cameraCaptureSession;
                          updatePreview();
                      }
                      @Override
                      public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
                          Toast.makeText(MainActivity.this, "Configuration change", Toast.LENGTH_SHORT).show();
                      }
                  }, null);
          } catch (CameraAccessException e) {
               e.printStackTrace();
          }
  }
  
  
    private final ImageReader.OnImageAvailableListener mOnImageAvailableListener  = new ImageReader.OnImageAvailableListener() {
      @Override
      public void onImageAvailable(ImageReader reader) {
          Image img = null;
          img = reader.acquireNextImage(); // we got YUV_420_888 frame here
          // transform to RGB format here?
          // image processing
      }
  };
  </code></pre>
  
  <p>How to update my codes to achieve the goal (e.g., using the <a href="https://github.com/pinguo-yuyidong/Camera2/blob/master/camera2/src/main/rs/yuv2rgb.rs" rel="nofollow noreferrer">yuv2rgb.rs</a>)? Thanks.</p>