1
我從相機中獲得了YUV_420_888圖像。我想從該圖像的灰度中裁剪出一個矩形以供給圖像處理算法。這是我到目前爲止有:從相機2中裁剪出一個矩形圖像
public static byte[] YUV_420_888toCroppedY(Image image, Rect cropRect) {
byte[] yData;
ByteBuffer yBuffer = image.getPlanes()[0].getBuffer();
int ySize = yBuffer.remaining();
yData = new byte[ySize];
yBuffer.get(yData, 0, ySize);
if (cropRect != null) {
int cropArea = (cropRect.right - cropRect.left) * (cropRect.bottom - cropRect.top);
byte[] croppedY = new byte[cropArea];
int cropIndex = 0;
// from the top of the rectangle, to the bottom, sequentially add rows to the output array, croppedY
for (int y = cropRect.top; y < cropRect.top + cropRect.height(); y++) {
// (2x+W) * y + x
int rowStart = (2*cropRect.left + cropRect.width()) * y + cropRect.left;
// (2x+W) * y + x + W
int rowEnd = (2*cropRect.left + cropRect.width()) * y + cropRect.left + cropRect.width();
for (int x = rowStart; x < rowEnd; x++) {
croppedY[cropIndex] = yData[x];
cropIndex++;
}
}
return croppedY;
}
return yData;
}
此功能沒有錯誤運行,但我把它弄出來的圖像是垃圾 - 它看起來是這樣的:
我不是確定如何解決這個問題或我做錯了什麼。
我想你應該張貼原始圖像 –
@YandryPozo我沒有它,我通過我的預覽屏幕看了看,它看起來並不像 – Carpetfizz
你可能通過讀取gralloc和android緩衝區找到答案:https://source.android.com/devices/graphics/arch-bq-gralloc。這是一個很好的概述,這也解釋了爲什麼緩衝區的字節寬度可能與圖像的像素寬度有所不同:https://www.codeproject.com/articles/991640/androids-graphics-buffer-management-system -Part-I – l85m