Incorrect image converting YUV_420_888 into Bitmaps under Android camera2

前端 未结 3 1458
予麋鹿
予麋鹿 2020-12-11 07:47

I’m trying to convert YUV_420_888 images into bitmaps, coming from the camera2 preview. But the output image has incorrect colors.

Next is the test code I’m running

3条回答
  •  忘掉有多难
    2020-12-11 08:43

    RenderScript support YUV_420_888 as ScriptIntrinsicYuvToRGB's source

    1. create Allocation & ScriptIntrinsicYuvToRGB

      RenderScript renderScript = RenderScript.create(this);
      ScriptIntrinsicYuvToRGB mScriptIntrinsicYuvToRGB = ScriptIntrinsicYuvToRGB.create(renderScript, Element.YUV(renderScript));
      Allocation mAllocationInYUV = Allocation.createTyped(renderScript, new Type.Builder(renderScript, Element.YUV(renderScript)).setYuvFormat(ImageFormat.YUV_420_888).setX(480).setY(640).create(), Allocation.USAGE_IO_INPUT | Allocation.USAGE_SCRIPT);
      Allocation mAllocationOutRGB = Allocation.createTyped(renderScript, Type.createXY(renderScript, Element.RGBA_8888(renderScript), 480, 640), Allocation.USAGE_SCRIPT | Allocation.USAGE_IO_OUTPUT);
      
    2. set Allocation.getSurface() to receive image data from camera

      final CaptureRequest.Builder captureRequest = session.getDevice().createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
      captureRequest.addTarget(mAllocationInYUV.getSurface());
      
    3. output to a TextureView or ImageReader or SurfaceView

      mAllocationOutRGB.setSurface(new Surface(mTextureView.getSurfaceTexture()));
      mAllocationInYUV.setOnBufferAvailableListener(new Allocation.OnBufferAvailableListener() {
          @Override
          public void onBufferAvailable(Allocation a) {
              a.ioReceive();
              mScriptIntrinsicYuvToRGB.setInput(a);
              mScriptIntrinsicYuvToRGB.forEach(mAllocationOutRGB);
              mAllocationOutRGB.ioSend();
          }
      });
      

提交回复
热议问题