diff --git a/qreader/src/main/java/github/nisrulz/qreader/QRBarcodeListener.java b/qreader/src/main/java/github/nisrulz/qreader/QRBarcodeListener.java new file mode 100644 index 0000000..cc48d9b --- /dev/null +++ b/qreader/src/main/java/github/nisrulz/qreader/QRBarcodeListener.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2016 Nishant Srivastava + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package github.nisrulz.qreader; + +import com.google.android.gms.vision.barcode.Barcode; + +/** + *The interface to get the Barcode + */ +public interface QRBarcodeListener { + + /** + * On detected. + * + * @param data + * the barcode object + */ + //Changed the data from String to Barcode object + public void onDetected(Barcode data); + + +} \ No newline at end of file diff --git a/qreader/src/main/java/github/nisrulz/qreader/QRDataListener.java b/qreader/src/main/java/github/nisrulz/qreader/QRDataListener.java index b9add1e..f9c058e 100644 --- a/qreader/src/main/java/github/nisrulz/qreader/QRDataListener.java +++ b/qreader/src/main/java/github/nisrulz/qreader/QRDataListener.java @@ -16,17 +16,52 @@ package github.nisrulz.qreader; +import android.graphics.Rect; + +import com.google.android.gms.vision.barcode.Barcode; + /** * The interface Qr data listener. */ -public interface QRDataListener { - - /** - * On detected. - * - * @param data - * the data - */ - // Called from not main thread. Be careful - void onDetected(final String data); +public abstract class QRDataListener implements QRBarcodeListener { + + //Added Rect to limit the reading area + private Rect readingArea; + //Added Rect that allows to get the read data area + private Rect readData; + + /** + * On detected. + * + * @param data + * the data + */ + // Called from not main thread. Be careful + public abstract void onDetected(final String data); + + @Override + public void onDetected(Barcode data) { + readData = data.getBoundingBox(); + if(readingArea == null) { + onDetected(data.displayValue); + } + else { + if(readingArea.contains(readData)) { + onDetected(data.displayValue); + } + } + + + } + + //readData is readOnly + public Rect getReadData() { + return readData; + } + + //readingData is set only + + public void setReadingArea(Rect readingArea) { + this.readingArea = readingArea; + } } diff --git a/qreader/src/main/java/github/nisrulz/qreader/QREader.java b/qreader/src/main/java/github/nisrulz/qreader/QREader.java index 1ee15fb..29075a2 100644 --- a/qreader/src/main/java/github/nisrulz/qreader/QREader.java +++ b/qreader/src/main/java/github/nisrulz/qreader/QREader.java @@ -54,7 +54,8 @@ public class QREader { private final int width; private final int height; private final int facing; - private final QRDataListener qrDataListener; + //Changed the qrDataListener from QRDataListenr to QrBarcodeListener + private final QRBarcodeListener qrDataListener; private final Context context; private final SurfaceView surfaceView; private boolean autoFocusEnabled; @@ -173,7 +174,8 @@ public void release() { public void receiveDetections(Detector.Detections detections) { final SparseArray barcodes = detections.getDetectedItems(); if (barcodes.size() != 0 && qrDataListener != null) { - qrDataListener.onDetected(barcodes.valueAt(0).displayValue); + //Instead of just sending the string, the complete Barcode object is sent + qrDataListener.onDetected(barcodes.valueAt(0)); } } }); @@ -274,7 +276,8 @@ public static class Builder { private int width; private int height; private int facing; - private final QRDataListener qrDataListener; + //Changed the qrDataListener from QRDataListenr to QrBarcodeListener + private final QRBarcodeListener qrDataListener; private final Context context; private final SurfaceView surfaceView; private BarcodeDetector barcodeDetector; @@ -289,7 +292,8 @@ public static class Builder { * @param qrDataListener * the qr data listener */ - public Builder(Context context, SurfaceView surfaceView, QRDataListener qrDataListener) { + //Changed the qrDataListener from QRDataListenr to QrBarcodeListener + public Builder(Context context, SurfaceView surfaceView, QRBarcodeListener qrDataListener) { this.autofocusEnabled = true; this.width = 800; this.height = 800;