AndroidJavaCV + Camera2

Android JavaCV + Camera2

尝试使用 javaCV 从摄像头录制视频,

  // recoder settings:
  private int imageWidth  = 320;  
  private int imageHeight = 240;
  private int frameRate   = 30;

  recorder = new FFmpegFrameRecorder(ffmpeg_link, imageWidth, imageHeight, 1);
  recorder.setFormat("mp4");
  recorder.setFrameRate(frameRate);

  // frame settings:
  IplImage yuvIplimage = null;
  yuvIplimage = IplImage.create(320, 320, IPL_DEPTH_16U, 1); //32 not supported

  //image reader:
  private ImageReader mImageReader;
  mImageReader = ImageReader.newInstance(320, 320, ImageFormat.YUV_420_888, 10);  
  mImageReader.setOnImageAvailableListener(
                    mOnImageAvailableListener, mBackgroundHandler);

private final ImageReader.OnImageAvailableListener mOnImageAvailableListener
        = new ImageReader.OnImageAvailableListener() {

    @Override  
    public void onImageAvailable(ImageReader reader) {
        Image image = reader.acquireNextImage();// acquireLatestImage(); - also tried
        if (image == null)
            return; 

        final ByteBuffer buffer = image.getPlanes()[0].getBuffer();
        byte[] bytes = new byte[buffer.remaining()];
        buffer.get(bytes, 0, bytes.length); 


        if (yuvIplimage != null ) {
            // OPTION 1
            yuvIplimage.getByteBuffer().put(convertYUV420ToNV21(image)); 
            // OPTION 2
            //yuvIplimage.getByteBuffer().put(decodeYUV420SP(bytes,320,320));
            try {
                if (started)  {
                recorder.record(yuvIplimage);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }  

        image.close();
    }
}; 

选项 1 是使用代码将图像解码为 NV21:

 private byte[] convertYUV420ToNV21(Image imgYUV420) {
    byte[] rez;

    ByteBuffer buffer0 = imgYUV420.getPlanes()[0].getBuffer();
    ByteBuffer buffer2 = imgYUV420.getPlanes()[2].getBuffer();
    int buffer0_size = buffer0.remaining();
    int buffer2_size = buffer2.remaining();
    rez = new byte[buffer0_size + buffer2_size]; 

    buffer0.get(rez, 0, buffer0_size);  
    buffer2.get(rez, buffer0_size, buffer2_size);

    return rez;
}

选项 2 是转换为 rgb,就好像我理解 corect:

public byte[] decodeYUV420SP( byte[] yuv420sp, int width, int height) {
    final int frameSize = width * height;
    byte rgb[]=new byte[width*height];
    for (int j = 0, yp = 0; j < height; j++) {
        int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;
        for (int i = 0; i < width; i++, yp++) {
            int y = (0xff & ((int) yuv420sp[yp])) - 16;
            if (y < 0) y = 0;
            if ((i & 1) == 0) {
                v = (0xff & yuv420sp[uvp++]) - 128;
                u = (0xff & yuv420sp[uvp++]) - 128;
            }
            int y1192 = 1192 * y;
            int r = (y1192 + 1634 * v);
            int g = (y1192 - 833 * v - 400 * u);
            int b = (y1192 + 2066 * u);
            if (r < 0) r = 0; else if (r > 262143) r = 262143;
            if (g < 0) g = 0; else if (g > 262143) g = 262143;
            if (b < 0) b = 0; else if (b > 262143) b = 262143;
            rgb[yp] = (byte) (0xff000000 | ((r << 6) & 0xff0000)
                    | ((g >> 2) & 0xff00) | ((b >> 10) & 0xff));
        }
    }
    return rgb;   }

它看起来也不正确。 wisch 是将 camera2 图像转换为 IplImage 的正确方法吗? 可以在飞行中完成吗?

如果您愿意将您的处理转移到 C++,您可以像

那样进行转换

cv::cvtColor((cv::_InputArray)mNV, (cv::_OutputArray)rgba, CV_YUV2RGBA_NV12, 0);

这会将图像存储为 RGBA 垫,您可以从那里正常保存它。

如果刻录机需要 NV21,那么将图像转换为 NV21 而不是 RGB 可能是最快的选择。

但你为什么不直接使用 android.media.MediaRecorder?它效率更高,可以使用硬件编码器。

但是如果您需要坚持使用 ffmpeg,那么您的第一个选项对于许多设备都是不正确的。此外,请确保您之前删除了 buffer.get 调用 - 它会使平面 0 的其余读取无法正常工作,这可能是您当前的问题。一旦你读了一次平面 0,.remaining() 将 return 0.

YUV 图像有 3 个平面,除非您检查过底层格式实际上是 NV21,否则您不应该盲目地假设。,或者假设行步幅等于宽度。 为了安全起见,将三个平面复制到半平面byte[].

时,需要同时查看行和像素步幅