Merge branch '2269-use-full-camera-preview-when-scanning-qr-codes' into 'master'

Use whole preview image when decoding QR

Closes #2269

See merge request briar/briar!1600
This commit is contained in:
akwizgran
2022-03-08 15:02:25 +00:00

View File

@@ -1,7 +1,6 @@
package org.briarproject.briar.android.qrcode; package org.briarproject.briar.android.qrcode;
import android.hardware.Camera; import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.PreviewCallback; import android.hardware.Camera.PreviewCallback;
import android.hardware.Camera.Size; import android.hardware.Camera.Size;
@@ -78,9 +77,7 @@ public class QrCodeDecoder implements PreviewConsumer, PreviewCallback {
// The preview should be in NV21 format: width * height bytes of // The preview should be in NV21 format: width * height bytes of
// Y followed by width * height / 2 bytes of interleaved U and V // Y followed by width * height / 2 bytes of interleaved U and V
if (data.length == size.width * size.height * 3 / 2) { if (data.length == size.width * size.height * 3 / 2) {
CameraInfo info = new CameraInfo(); decode(data, size.width, size.height);
Camera.getCameraInfo(cameraIndex, info);
decode(data, size.width, size.height, info.orientation);
} else { } else {
// Camera parameters have changed - ask for a new preview // Camera parameters have changed - ask for a new preview
LOG.info("Preview size does not match camera parameters"); LOG.info("Preview size does not match camera parameters");
@@ -94,9 +91,9 @@ public class QrCodeDecoder implements PreviewConsumer, PreviewCallback {
} }
} }
private void decode(byte[] data, int width, int height, int orientation) { private void decode(byte[] data, int width, int height) {
ioExecutor.execute(() -> { ioExecutor.execute(() -> {
BinaryBitmap bitmap = binarize(data, width, height, orientation); BinaryBitmap bitmap = binarize(data, width, height);
Result result; Result result;
try { try {
result = reader.decode(bitmap, result = reader.decode(bitmap,
@@ -113,16 +110,9 @@ public class QrCodeDecoder implements PreviewConsumer, PreviewCallback {
}); });
} }
private static BinaryBitmap binarize(byte[] data, int width, int height, private static BinaryBitmap binarize(byte[] data, int width, int height) {
int orientation) { LuminanceSource src = new PlanarYUVLuminanceSource(data, width, height,
// Crop to a square at the top (portrait) or left (landscape) of the 0, 0, width, height, false);
// screen - this will be faster to decode and should include
// everything visible in the viewfinder
int crop = Math.min(width, height);
int left = orientation >= 180 ? width - crop : 0;
int top = orientation >= 180 ? height - crop : 0;
LuminanceSource src = new PlanarYUVLuminanceSource(data, width,
height, left, top, crop, crop, false);
return new BinaryBitmap(new HybridBinarizer(src)); return new BinaryBitmap(new HybridBinarizer(src));
} }