Converting preview frame to bitmap

后端 未结 5 1725
天涯浪人
天涯浪人 2020-11-29 07:30

I know the subject was on the board many times, but i can not get it work anyhow... I want to save view frames from preview to jpeg files. It looks more or less(code is simp

5条回答
  •  执笔经年
    2020-11-29 08:34

    Alternatively, if you DO need a bitmap for some reason, and/or want to do this without creating a YUVImage and compressing to JPEG, you can use the handy RenderScript 'ScriptIntrinsicYuvToRGB' (API 17+):

    @Override 
    public void onPreviewFrame(byte[] data, Camera camera) { 
       Bitmap bitmap = Bitmap.createBitmap(r.width(), r.height(), Bitmap.Config.ARGB_8888);
        Allocation bmData = renderScriptNV21ToRGBA8888(
            mContext, r.width(), r.height(), data);
        bmData.copyTo(bitmap);
    }
    
    public Allocation renderScriptNV21ToRGBA8888(Context context, int width, int height, byte[] nv21) {
      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;
    }
    

提交回复
热议问题