Goo*_*oey 17 camera android image-processing renderscript android-camera2
我目前正在使用Javacv,它利用public void onPreviewFrame(byte[] data, Camera camera)相机功能.
由于相机已被弃用,我一直在研究camera2和MediaProjection.这两个库都使用ImageReader类.
目前我ImageReader使用以下代码实例化这样的:
ImageReader.newInstance(DISPLAY_WIDTH, DISPLAY_HEIGHT, PixelFormat.RGBA_8888, 2);
并附上OnImageAvailableListener这样的:
private final ImageReader.OnImageAvailableListener mOnImageAvailableListener
= new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader reader) {
mBackgroundHandler.post(new processImage(reader.acquireNextImage()));
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用RGBA_8888Javacv 的格式按照这个帖子:https://github.com/bytedeco/javacv/issues/298但这对我不起作用.
所以我正在考虑使用Render脚本将这些Image转换为NV21(YUV_420_SP)格式(这是onPreviewFrame函数中相机的默认输出),因为这对我来说对camera库有用.
我还阅读了这个和本网站的帖子进行转换,但这些对我不起作用,我担心它们会太慢.此外,我对C的了解非常有限.基本上看起来我想要https://developer.android.com/reference/android/renderscript/ScriptIntrinsicYuvToRGB.html的反向操作
那么你怎么能从一个Image匹配onPreviewFrame函数输出的字节数组,即NV21(YUV_420_SP)格式?最好使用Renderscript,因为它更快.
我尝试过使用ImageFormat.YUV_420_888但无济于事.我一直得到错误The producer output buffer format 0x1 doesn't match the ImageReader's configured buffer format.我转回去PixelFormat.RGBA_8888发现里面只有一架飞机ImageObject.该平面的字节缓冲区大小为宽*高*4(分别为R,G,B,A的一个字节).所以我试着将其转换为NV21格式.
我修改了这个答案的代码以产生以下功能:
void RGBtoNV21(byte[] yuv420sp, byte[] argb, int width, int height) {
final int frameSize = width * height;
int yIndex = 0;
int uvIndex = frameSize;
int A, R, G, B, Y, U, V;
int index = 0;
int rgbIndex = 0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
R = argb[rgbIndex++];
G = argb[rgbIndex++];
B = argb[rgbIndex++];
A = argb[rgbIndex++];
// RGB to YUV conversion according to
// https://en.wikipedia.org/wiki/YUV#Y.E2.80.B2UV444_to_RGB888_conversion
Y = ((66 * R + 129 * G + 25 * B + 128) >> 8) + 16;
U = ((-38 * R - 74 * G + 112 * B + 128) >> 8) + 128;
V = ((112 * R - 94 * G - 18 * B + 128) >> 8) + 128;
// NV21 has a plane of Y and interleaved planes of VU each sampled by a factor
// of 2 meaning for every 4 Y pixels there are 1 V and 1 U.
// Note the sampling is every other pixel AND every other scanline.
yuv420sp[yIndex++] = (byte) ((Y < 0) ? 0 : ((Y > 255) ? 255 : Y));
if (i % 2 == 0 && index % 2 == 0) {
yuv420sp[uvIndex++] = (byte) ((V < 0) ? 0 : ((V > 255) ? 255 : V));
yuv420sp[uvIndex++] = (byte) ((U < 0) ? 0 : ((U > 255) ? 255 : U));
}
index++;
}
}
}
Run Code Online (Sandbox Code Playgroud)
并使用以下方法调用它:
int mWidth = mImage.getWidth();
int mHeight = mImage.getHeight();
byte[] rgbaBytes = new byte[mWidth * mHeight * 4];
mImage.getPlanes()[0].getBuffer().get(rgbaBytes);
mImage.close();
byte[] yuv = new byte[mWidth * mHeight * 3 / 2];
RGBtoNV21(yuv, rgbaBytes, mWidth, mHeight);
Run Code Online (Sandbox Code Playgroud)
这mImage是Image我的一个对象ImageReader.
然而,这会产生类似于此图像的结果:

这显然是畸形的.好像我的转换已关闭,但我无法确定究竟是什么.
@TargetApi(19)
public static byte[] yuvImageToByteArray(Image image) {
assert(image.getFormat() == ImageFormat.YUV_420_888);
int width = image.getWidth();
int height = image.getHeight();
Image.Plane[] planes = image.getPlanes();
byte[] result = new byte[width * height * 3 / 2];
int stride = planes[0].getRowStride();
assert (1 == planes[0].getPixelStride());
if (stride == width) {
planes[0].getBuffer().get(result, 0, width*height);
}
else {
for (int row = 0; row < height; row++) {
planes[0].getBuffer().position(row*stride);
planes[0].getBuffer().get(result, row*width, width);
}
}
stride = planes[1].getRowStride();
assert (stride == planes[2].getRowStride());
int pixelStride = planes[1].getPixelStride();
assert (pixelStride == planes[2].getPixelStride());
byte[] rowBytesCb = new byte[stride];
byte[] rowBytesCr = new byte[stride];
for (int row = 0; row < height/2; row++) {
int rowOffset = width*height + width/2 * row;
planes[1].getBuffer().position(row*stride);
planes[1].getBuffer().get(rowBytesCb);
planes[2].getBuffer().position(row*stride);
planes[2].getBuffer().get(rowBytesCr);
for (int col = 0; col < width/2; col++) {
result[rowOffset + col*2] = rowBytesCr[col*pixelStride];
result[rowOffset + col*2 + 1] = rowBytesCb[col*pixelStride];
}
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3135 次 |
| 最近记录: |