2020-06-07 22:29:52 +02:00
|
|
|
package com.beemdevelopment.aegis.helpers;
|
|
|
|
|
2022-05-01 19:17:03 +02:00
|
|
|
import static android.graphics.ImageFormat.YUV_420_888;
|
|
|
|
|
2021-01-19 20:26:45 +01:00
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.Looper;
|
2020-06-07 22:29:52 +02:00
|
|
|
import android.util.Log;
|
2021-01-19 20:26:45 +01:00
|
|
|
import android.util.Size;
|
2020-06-07 22:29:52 +02:00
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.camera.core.ImageAnalysis;
|
|
|
|
import androidx.camera.core.ImageProxy;
|
|
|
|
|
2022-05-01 19:17:03 +02:00
|
|
|
import com.google.zxing.BarcodeFormat;
|
2020-06-07 22:29:52 +02:00
|
|
|
import com.google.zxing.BinaryBitmap;
|
2022-05-01 19:17:03 +02:00
|
|
|
import com.google.zxing.DecodeHintType;
|
|
|
|
import com.google.zxing.MultiFormatReader;
|
2020-06-07 22:29:52 +02:00
|
|
|
import com.google.zxing.NotFoundException;
|
|
|
|
import com.google.zxing.PlanarYUVLuminanceSource;
|
|
|
|
import com.google.zxing.Result;
|
|
|
|
import com.google.zxing.common.HybridBinarizer;
|
|
|
|
|
|
|
|
import java.nio.ByteBuffer;
|
2022-05-01 19:17:03 +02:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2020-06-07 22:29:52 +02:00
|
|
|
|
|
|
|
public class QrCodeAnalyzer implements ImageAnalysis.Analyzer {
|
|
|
|
private static final String TAG = QrCodeAnalyzer.class.getSimpleName();
|
2021-01-19 20:26:45 +01:00
|
|
|
public static final Size RESOLUTION = new Size(1200, 1600);
|
2020-06-07 22:29:52 +02:00
|
|
|
|
|
|
|
private final QrCodeAnalyzer.Listener _listener;
|
|
|
|
|
|
|
|
public QrCodeAnalyzer(QrCodeAnalyzer.Listener listener) {
|
|
|
|
_listener = listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void analyze(@NonNull ImageProxy image) {
|
|
|
|
int format = image.getFormat();
|
2022-05-01 19:17:03 +02:00
|
|
|
if (format != YUV_420_888) {
|
|
|
|
Log.e(TAG, String.format("Unexpected YUV image format: %d", format));
|
2020-06-07 22:29:52 +02:00
|
|
|
image.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-01 19:17:03 +02:00
|
|
|
ImageProxy.PlaneProxy plane = image.getPlanes()[0];
|
|
|
|
ByteBuffer buf = plane.getBuffer();
|
|
|
|
byte[] data = new byte[buf.remaining()];
|
|
|
|
buf.get(data);
|
|
|
|
buf.rewind();
|
|
|
|
|
2020-06-07 22:29:52 +02:00
|
|
|
PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(
|
2022-05-01 19:17:03 +02:00
|
|
|
data,
|
|
|
|
plane.getRowStride(),
|
|
|
|
image.getHeight(),
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
image.getWidth(),
|
|
|
|
image.getHeight(),
|
|
|
|
false
|
2020-06-07 22:29:52 +02:00
|
|
|
);
|
|
|
|
|
2022-05-01 19:17:03 +02:00
|
|
|
MultiFormatReader reader = new MultiFormatReader();
|
2020-06-07 22:29:52 +02:00
|
|
|
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
|
|
|
|
try {
|
2022-05-01 19:17:03 +02:00
|
|
|
Map<DecodeHintType, Object> hints = new HashMap<>();
|
|
|
|
hints.put(DecodeHintType.POSSIBLE_FORMATS, Collections.singletonList(BarcodeFormat.QR_CODE));
|
|
|
|
hints.put(DecodeHintType.ALSO_INVERTED, true);
|
|
|
|
|
|
|
|
Result result = reader.decode(bitmap, hints);
|
2020-06-07 22:29:52 +02:00
|
|
|
if (_listener != null) {
|
2021-01-19 20:26:45 +01:00
|
|
|
new Handler(Looper.getMainLooper()).post(() -> _listener.onQrCodeDetected(result));
|
2020-06-07 22:29:52 +02:00
|
|
|
}
|
2022-05-01 19:17:03 +02:00
|
|
|
} catch (NotFoundException ignored) {
|
2020-06-07 22:29:52 +02:00
|
|
|
|
|
|
|
} finally {
|
|
|
|
image.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface Listener {
|
|
|
|
void onQrCodeDetected(Result result);
|
|
|
|
}
|
|
|
|
}
|