title: Convert Flutter Frame Camera to android Bitmap
body: <p>I'm developing a Flutter plugin for Android and iOS using the Flutter plugin <a href="https://pub.dev/packages/camera" rel="nofollow noreferrer">camera</a>. First I stream the frame from the plugin using this method:</p>
  <pre class="lang-dart prettyprint-override"><code>controller.startImageStream((CameraImage img) {
       runlOnFrame(
                  bytesList: img.planes.map((plane) {
                    return plane.bytes;
                  }).toList(),
                  imageHeight: img.height,
                  imageWidth: img.width,
                )
  }
  </code></pre>
  <p>The <code>runlOnFrame</code> method is developed in Java and it does the conversion from  YUV bytes to bitmap in  RGBA format like this:</p>
  <p>Get Argument Function :</p>
  <pre><code>getArgs(HashMap args){
       List&lt;byte[]&gt; bytesList = (ArrayList) args.get(&quot;bytesList&quot;);
       int imageHeight = (int) (args.get(&quot;imageHeight&quot;));
       int imageWidth = (int) (args.get(&quot;imageWidth&quot;));
  }
  
  runOnFrame(List&lt;byte[]&gt; bytesList, int imageHeight, int imageWidth, ) throws IOException {
      int rotation = 90;
      ByteBuffer Y = ByteBuffer.wrap(bytesList.get(0));
      ByteBuffer U = ByteBuffer.wrap(bytesList.get(1));
      ByteBuffer V = ByteBuffer.wrap(bytesList.get(2));
  
      int Yb = Y.remaining();
      int Ub = U.remaining();
      int Vb = V.remaining();
  
      byte[] data = new byte[Yb + Ub + Vb];
  
      Y.get(data, 0, Yb);
      V.get(data, Yb, Vb);
      U.get(data, Yb + Vb, Ub);
  
      Bitmap bitmapRaw = Bitmap.createBitmap(imageWidth, imageHeight, Bitmap.Config.ARGB_8888);
      Allocation bmData = renderScriptNV21ToRGBA888(
          mRegistrar.context(),
          imageWidth,
          imageHeight,
          data);
      bmData.copyTo(bitmapRaw);
  
      Matrix matrix = new Matrix();
      matrix.postRotate(rotation);
      Bitmap finalbitmapRaw = Bitmap.createBitmap(bitmapRaw, 0, 0, bitmapRaw.getWidth(), bitmapRaw.getHeight(), matrix, true);
      saveBitm(finalbitmapRaw); // Function to save converted bitmap
      
    } 
  </code></pre>
  <pre><code>  public Allocation renderScriptNV21ToRGBA888(Context context, int width, int height, byte[] nv21) {
      // https://stackoverflow.com/a/36409748
      RenderScript rs = RenderScript.create(context);
      ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));
  
      Type.Builder yuvType = new Type.Builder(rs, Element.U8(rs)).setX(nv21.length);
      Allocation in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);
  
      Type.Builder rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(width).setY(height);
      Allocation out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT);
  
      in.copyFrom(nv21);
  
      yuvToRgbIntrinsic.setInput(in);
      yuvToRgbIntrinsic.forEach(out);
      return out;
    }
  </code></pre>
  <p>The problem is i'm getting green bitmap images like this:</p>
  <p><img src="https://imgur.com/2Gcgcc9.jpg" alt="" /></p>