编辑:解决了!见下文。我需要裁剪从Camera2的onImageAvailable侦听器获取的图像(YUV422888颜色空间)。我不希望或不需要将其转换为位图,因为它会极大地影响性能,而且我实际上对亮度而不是RGB信息(包含在图像的平面0中)感兴趣。我想出了以下解决方案:
不幸的是,第4点产生了损坏的图像。我究竟做错了什么?
在Camera2的onImageAvailableListener中(请注意,尽管我正在计算位图,但是只能看到正在发生的事情,因为我对Bitmap / RGB数据不感兴趣):
Image.Plane[] planes = image.getPlanes();
ByteBuffer buffer = planes[0].getBuffer(); // Grab just the Y' Plane.
buffer.rewind();
byte[] data = new byte[buffer.capacity()];
buffer.get(data);
Bitmap bitmap = cropByteArray(data, image.getWidth(), image.getHeight()); // Just for preview/sanity check purposes. The bitmap is **corrupt**.
runOnUiThread(new bitmapRunnable(bitmap) {
@Override
public void run() {
image_view_preview.setImageBitmap(this.bitmap);
}
});
该cropByteArray功能需要修复。它输出损坏的位图,并应输出类似于in的out byte []数组,但仅包含裁剪区域:
public Bitmap cropByteArray(byte[] in, int inw, int inh) {
int l = 100; // left crop start
int r = 400; // right crop end
int t = 400; // top crop start
int b = 700; // top crop end
int outw = r-l;
int outh = b-t;
byte[][] in2d = new byte[inw][inh]; // input width and height are 1080 x 1920.
byte[] out = new byte[outw*outh];
int[] pixels = new int[outw*outh];
i = 0;
for(int col = 0; col < inw; col++) {
for(int row = 0; row < inh; row++) {
in2d[col][row] = in[i++];
}
}
i = 0;
for(int col = l; col < r; col++) {
for(int row = t; row < b; row++) {
//out[i++] = in2d[col][row]; // out is the desired output of the function, but for now we output a bitmap instead
int grey = in2d[col][row] & 0xff;
pixels[i++] = 0xFF000000 | (grey * 0x00010101);
}
}
return Bitmap.createBitmap(pixels, inw, inh, Bitmap.Config.ARGB_8888);
}
编辑由于Eddy Talvala的建议而解决。以下代码将产生裁剪为所需坐标的Y'(ImageReader的亮度平面0)。裁剪的数据在输出字节数组中。生成位图只是为了确认。我还在下面附加了方便的YUVtoGrayscale()函数。
Image.Plane[] planes = image.getPlanes();
ByteBuffer buffer = planes[0].getBuffer();
int stride = planes[0].getRowStride();
buffer.rewind();
byte[] Y = new byte[buffer.capacity()];
buffer.get(Y);
int t=200; int l=600;
int out_h = 600; int out_w = 600;
byte[] out = new byte[out_w*out_h];
int firstRowOffset = stride * t + l;
for (int row = 0; row < out_h; row++) {
buffer.position(firstRowOffset + row * stride);
buffer.get(out, row * out_w, out_w);
}
Bitmap bitmap = YUVtoGrayscale(out, out_w, out_h);
这里是YUVtoGrayscale()。
public Bitmap YUVtoGrayscale(byte[] yuv, int width, int height) {
int[] pixels = new int[yuv.length];
for (int i = 0; i < yuv.length; i++) {
int grey = yuv[i] & 0xff;
pixels[i] = 0xFF000000 | (grey * 0x00010101);
}
return Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888);
}
还有一些剩余的问题。我正在使用前置摄像头,尽管TextureView内部的预览方向是正确的,但ImageViewer返回的图像是顺时针旋转并垂直翻转的(预览时一个人躺在他们的右脸颊上,只有右脸颊是左脸颊,因为我的设备上的垂直翻转角度为270度。是否存在接受的解决方案,使使用Camera2的预览和保存的照片具有相同的正确方向?
干杯。
如果您描述了图像是如何损坏的,这将很有帮助-您看到有效的图像,但图像失真了,还是只是完全的垃圾,还是完全的黑色?
但我想您没有注意Y平面的行距(https://developer.android.com/reference/android/media/Image.Plane.html#getRowStride())导致图像歪斜(垂直线变为斜线)。
当访问Y平面时,像素(x,y)的字节索引为:
y * rowStride + x
不
y * width + x
因为行跨度可能大于宽度。
我还要避免大量复制;您确实不需要2D数组,并且图像的大byte []也浪费了内存。
您可以改为seek()到每个输出行的开头,然后仅使用ByteBuffer.get(byte [],offset,length)读取需要直接复制到目标byte []中的字节。
看起来像
int stride = planes[0].getRowStride();
ByteBuffer img = planes[0].getBuffer();
int firstRowOffset = stride * t + l;
for (int row = 0; row < outh; row++) {
img.position(firstRowOffset + row * stride);
img.get(out, row * outw, outw);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句