How to use ScriptIntrinsicYuvToRGB (converting byte[] yuv to byte[] rgba)

前端 未结 5 769
清酒与你
清酒与你 2020-12-01 09:40

I have byte[] yuvByteArray (540x360 image captured from Camera.PreviewCallback.onPreviewFrame method and dumped into assets/yuv.bin fi

5条回答
  •  被撕碎了的回忆
    2020-12-01 10:38

    Using API18+ wilddev´s answer can be slightly improved as you don´t need Type.Builder objects. Do all this stuff in the onCreate() method:

    aIn = Allocation.createSized(rs, Element.U8(rs), H*W*3/2); // what the f**k ?  This is 12 bit per pixel, giving the length of the camera data byte array !
    
    bmpout = Bitmap.createBitmap(W, H, Bitmap.Config.ARGB_8888); // you will need this bitmap for output anyway
    
    aOut = Allocation.createFromBitmap(rs, bmpout);  // tada !
    
    // and set the script´s input
    yuvToRgbIntrinsic.setInput(aIn);
    
    // as pixel data are stored as int (one byte for red, green, blue, alpha), you first need an int[] array
    int rgba[] = new int[w*h];   // the rgba[] array
    

    Now you can continue with these lines or put them into the onPreviewFrame() method:

    aIn.copyFrom(data);  // or aIn.copyFromUnchecked(data);  // which is faster and safe for camera data
    
    yuvToRgbIntrinsic.forEach(aOut);  // execute the script
    
    aOut.copyTo(bmpout);  // copy result from aOut to bmpout
    
    // if you need the rgba-values, do
    bmpout.getPixels(rgba, 0, W, 0, 0, W, H);
    
    // now you may loop through the rgba[] array and extraxt the r,g,b,a values 
    // and put them into a byte[] array(s), BUT this will surely have impact on the performance when doing in Java
    

提交回复
热议问题