You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
2.3 KiB
65 lines
2.3 KiB
package dev.garrettmills.starship.hyperlink.scanner;
|
|
|
|
import android.util.Log;
|
|
import android.widget.Toast;
|
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.camera.core.ImageAnalysis;
|
|
import androidx.camera.core.ImageProxy;
|
|
import androidx.core.content.ContextCompat;
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import com.google.zxing.BinaryBitmap;
|
|
import com.google.zxing.ChecksumException;
|
|
import com.google.zxing.FormatException;
|
|
import com.google.zxing.NotFoundException;
|
|
import com.google.zxing.PlanarYUVLuminanceSource;
|
|
import com.google.zxing.Result;
|
|
import com.google.zxing.common.HybridBinarizer;
|
|
import com.google.zxing.multi.qrcode.QRCodeMultiReader;
|
|
|
|
import static android.graphics.ImageFormat.YUV_420_888;
|
|
import static android.graphics.ImageFormat.YUV_422_888;
|
|
import static android.graphics.ImageFormat.YUV_444_888;
|
|
|
|
public class QRCodeImageAnalyzer implements ImageAnalysis.Analyzer {
|
|
private QRCodeListener listener;
|
|
|
|
public QRCodeImageAnalyzer(QRCodeListener listener) {
|
|
this.listener = listener;
|
|
}
|
|
|
|
@Override
|
|
public void analyze(@NonNull ImageProxy image) {
|
|
if ( image.getFormat() == YUV_420_888 || image.getFormat() == YUV_422_888 || image.getFormat() == YUV_444_888 ) {
|
|
ByteBuffer byteBuffer = image.getPlanes()[0].getBuffer();
|
|
byte[] imageData = new byte[byteBuffer.capacity()];
|
|
byteBuffer.get(imageData);
|
|
|
|
PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(
|
|
imageData,
|
|
image.getWidth(), image.getHeight(),
|
|
0, 0,
|
|
image.getWidth(), image.getHeight(),
|
|
false
|
|
);
|
|
|
|
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));
|
|
|
|
try {
|
|
Result result = new QRCodeMultiReader().decode(binaryBitmap);
|
|
listener.onQRCodeFound(result.getText());
|
|
Log.i("QRCodeImageAnalyzer", "QRCode found");
|
|
} catch (FormatException | ChecksumException | NotFoundException e) {
|
|
listener.onQRCodeNotFound();
|
|
Log.i("QRCodeImageAnalyzer", "QRCode not found");
|
|
}
|
|
} else {
|
|
Log.i("QRCodeImageAnalyzer", "Incorrect image format");
|
|
}
|
|
|
|
image.close();
|
|
}
|
|
}
|