gpt4 book ai didi

Android JavaCV + Camera2

转载 作者:太空宇宙 更新时间:2023-11-03 13:15:08 24 4
gpt4 key购买 nike

尝试使用 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;
}

enter image description here

选项 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; }

enter image description here

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

最佳答案

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

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

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

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

关于Android JavaCV + Camera2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37645018/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com