optimize and increase sampling rate of qr reading

This commit is contained in:
Craig Raw 2021-06-29 10:53:22 +02:00
parent b6a353815c
commit badf8c8f2f

View file

@ -7,6 +7,7 @@ import com.github.sarxos.webcam.WebcamUpdater;
import com.google.zxing.*; import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource; import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer; import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import javafx.beans.property.BooleanProperty; import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ObjectProperty; import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleBooleanProperty; import javafx.beans.property.SimpleBooleanProperty;
@ -29,10 +30,11 @@ public class WebcamService extends ScheduledService<Image> {
private final ObjectProperty<Result> resultProperty = new SimpleObjectProperty<>(null); private final ObjectProperty<Result> resultProperty = new SimpleObjectProperty<>(null);
private static final int QR_SAMPLE_PERIOD_MILLIS = 400; private static final int QR_SAMPLE_PERIOD_MILLIS = 200;
private Webcam cam; private Webcam cam;
private long lastQrSampleTime; private long lastQrSampleTime;
private final Reader qrReader;
static { static {
Webcam.setDriver(new WebcamScanDriver()); Webcam.setDriver(new WebcamScanDriver());
@ -43,6 +45,7 @@ public class WebcamService extends ScheduledService<Image> {
this.listener = listener; this.listener = listener;
this.delayCalculator = delayCalculator; this.delayCalculator = delayCalculator;
this.lastQrSampleTime = System.currentTimeMillis(); this.lastQrSampleTime = System.currentTimeMillis();
this.qrReader = new QRCodeReader();
} }
@Override @Override
@ -106,9 +109,9 @@ public class WebcamService extends ScheduledService<Image> {
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
try { try {
Result result = new MultiFormatReader().decode(bitmap); Result result = qrReader.decode(bitmap);
resultProperty.set(result); resultProperty.set(result);
} catch(NotFoundException e) { } catch(ReaderException e) {
// fall thru, it means there is no QR code in image // fall thru, it means there is no QR code in image
} }
} }