Remove padding from Y plane data before passing it to ZXing

Fixes #726
Fixes #657
This commit is contained in:
Alexander Bakker 2021-05-03 23:48:15 +02:00
parent a62e474e33
commit fb58c877d1

View file

@ -43,11 +43,7 @@ public class QrCodeAnalyzer implements ImageAnalysis.Analyzer {
return;
}
ByteBuffer buf = image.getPlanes()[0].getBuffer();
byte[] data = new byte[buf.remaining()];
buf.get(data);
buf.rewind();
byte[] data = getLuminancePlaneData(image);
PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(
data, image.getWidth(), image.getHeight(), 0, 0, image.getWidth(), image.getHeight(), false
);
@ -66,6 +62,33 @@ public class QrCodeAnalyzer implements ImageAnalysis.Analyzer {
}
}
private static byte[] getLuminancePlaneData(ImageProxy image) {
ImageProxy.PlaneProxy plane = image.getPlanes()[0];
ByteBuffer buf = plane.getBuffer();
byte[] data = new byte[buf.remaining()];
buf.get(data);
buf.rewind();
int width = image.getWidth();
int height = image.getHeight();
int rowStride = plane.getRowStride();
int pixelStride = plane.getPixelStride();
if (width != rowStride || pixelStride != 1) {
// remove padding from the Y plane data
byte[] cleanData = new byte[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
cleanData[y * width + x] = data[y * rowStride + x * pixelStride];
}
}
return cleanData;
}
return data;
}
public interface Listener {
void onQrCodeDetected(Result result);
}