From 706c2aa22c8c75fdc6195bb30ff7b5aac1e527be Mon Sep 17 00:00:00 2001 From: Nishant Srivastava Date: Mon, 23 May 2016 13:58:26 +0530 Subject: [PATCH 1/5] [+] added builder pattern to eliminate multiple confusing constructors. [*] fixed a setProcessor issue [-] QREader is no more a singleton, the dev needs to implement that as per their use-case --- .../nisrulz/projectqreader/MainActivity.java | 13 +- .../nisrulz/qreader/QRDataListener.java | 2 +- .../java/github/nisrulz/qreader/QREader.java | 172 +++++++++--------- 3 files changed, 90 insertions(+), 97 deletions(-) diff --git a/app/src/main/java/github/nisrulz/projectqreader/MainActivity.java b/app/src/main/java/github/nisrulz/projectqreader/MainActivity.java index 57f4d77..31a9739 100644 --- a/app/src/main/java/github/nisrulz/projectqreader/MainActivity.java +++ b/app/src/main/java/github/nisrulz/projectqreader/MainActivity.java @@ -28,6 +28,7 @@ public class MainActivity extends AppCompatActivity { private SurfaceView surfaceView; private TextView textView_qrcode_info; + QREader qrEader; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -36,7 +37,7 @@ public class MainActivity extends AppCompatActivity { surfaceView = (SurfaceView) findViewById(R.id.camera_view); textView_qrcode_info = (TextView) findViewById(R.id.code_info); - QREader.getInstance().setUpConfig(new QRDataListener() { + qrEader = new QREader.Builder(this, surfaceView, new QRDataListener() { @Override public void onDetected(final String data) { Log.d("QREader", "Value : " + data); textView_qrcode_info.post(new Runnable() { @@ -45,23 +46,23 @@ public class MainActivity extends AppCompatActivity { } }); } - }); + }).build(); - QREader.getInstance().init(this, surfaceView); + qrEader.init(); } @Override protected void onStart() { super.onStart(); // Call in onStart - QREader.getInstance().start(); + qrEader.start(); } @Override protected void onDestroy() { super.onDestroy(); // Call in onDestroy - QREader.getInstance().stop(); - QREader.getInstance().releaseAndCleanup(); + qrEader.stop(); + qrEader.releaseAndCleanup(); } } diff --git a/library/src/main/java/github/nisrulz/qreader/QRDataListener.java b/library/src/main/java/github/nisrulz/qreader/QRDataListener.java index 8fa4792..51cf64e 100644 --- a/library/src/main/java/github/nisrulz/qreader/QRDataListener.java +++ b/library/src/main/java/github/nisrulz/qreader/QRDataListener.java @@ -18,5 +18,5 @@ public interface QRDataListener { - void onDetected(String data); + void onDetected(final String data); } diff --git a/library/src/main/java/github/nisrulz/qreader/QREader.java b/library/src/main/java/github/nisrulz/qreader/QREader.java index ec54640..d0e2c2e 100644 --- a/library/src/main/java/github/nisrulz/qreader/QREader.java +++ b/library/src/main/java/github/nisrulz/qreader/QREader.java @@ -38,82 +38,32 @@ public class QREader { private CameraSource cameraSource = null; private BarcodeDetector barcodeDetector = null; + public static final int FRONT_CAM = CameraSource.CAMERA_FACING_FRONT; + public static final int BACK_CAM = CameraSource.CAMERA_FACING_BACK; + private boolean autofocus_enabled; - private int width; - private int height; - private int facing; + private final int width; + private final int height; + private final int facing; private boolean cameraRunning = false; - private QRDataListener qrDataListener; - private Context context; - private SurfaceView surfaceView; - - private static QREader INSTANCE; - - private QREader() { - - } - - /** - * Gets instance. - * - * @return the instance - */ - public static QREader getInstance() { - if (INSTANCE == null) { - INSTANCE = new QREader(); - } - return INSTANCE; - } - - /** - * Sets up config. - * - * @param qrDataListener the qr data listener - */ - public void setUpConfig(final QRDataListener qrDataListener) { - setUpConfig(true, 800, 800, CameraSource.CAMERA_FACING_BACK, qrDataListener); - } - - /** - * Sets up config. - * - * @param autofocus_enabled the autofocus enabled - * @param facing the facing - * @param qrDataListener the qr data listener - */ - public void setUpConfig(boolean autofocus_enabled, int facing, - final QRDataListener qrDataListener) { - setUpConfig(autofocus_enabled, 800, 800, facing, qrDataListener); - } - - /** - * Sets up config. - * - * @param autofocus_enabled the autofocus enabled - * @param width the width - * @param height the height - * @param facing the facing - * @param qrDataListener the qr data listener - */ - public void setUpConfig(boolean autofocus_enabled, int width, int height, int facing, - final QRDataListener qrDataListener) { - this.autofocus_enabled = autofocus_enabled; - this.width = width; - this.height = height; - this.facing = facing; - this.qrDataListener = qrDataListener; + private final QRDataListener qrDataListener; + private final Context context; + private final SurfaceView surfaceView; + + public QREader(Builder builder) { + this.autofocus_enabled = builder.autofocus_enabled; + this.width = builder.width; + this.height = builder.height; + this.facing = builder.facing; + this.qrDataListener = builder.qrDataListener; + this.context = builder.context; + this.surfaceView = builder.surfaceView; } /** * Init. - * - * @param context the context - * @param surfaceView the surface view */ - public void init(final Context context, final SurfaceView surfaceView) { - this.context = context; - this.surfaceView = surfaceView; - + public void init() { if (!hasAutofocus(context)) { Log.e(LOGTAG, "Do not have autofocus feature, disabling autofocus feature in the library!"); autofocus_enabled = false; @@ -129,34 +79,32 @@ public void init(final Context context, final SurfaceView surfaceView) { } // Setup Barcodedetector - if (barcodeDetector == null) { - barcodeDetector = - new BarcodeDetector.Builder(context).setBarcodeFormats(Barcode.QR_CODE).build(); + barcodeDetector = + new BarcodeDetector.Builder(context).setBarcodeFormats(Barcode.QR_CODE).build(); - if (barcodeDetector.isOperational()) { - barcodeDetector.setProcessor(new Detector.Processor() { - @Override public void release() { + if (barcodeDetector.isOperational()) { + barcodeDetector.setProcessor(new Detector.Processor() { + @Override public void release() { - } + } - @Override public void receiveDetections(Detector.Detections detections) { - final SparseArray barcodes = detections.getDetectedItems(); - if (barcodes.size() != 0 && qrDataListener != null) { - qrDataListener.onDetected(barcodes.valueAt(0).displayValue); - } + @Override public void receiveDetections(Detector.Detections detections) { + final SparseArray barcodes = detections.getDetectedItems(); + if (barcodes.size() != 0 && qrDataListener != null) { + qrDataListener.onDetected(barcodes.valueAt(0).displayValue); } - }); - } + } + }); + } else { + Log.e(LOGTAG,"Barcode recognition libs are not downloaded and are not operational"); } // Setup Camera - if (cameraSource == null) { - cameraSource = - new CameraSource.Builder(context, barcodeDetector).setAutoFocusEnabled(autofocus_enabled) - .setFacing(facing) - .setRequestedPreviewSize(width, height) - .build(); - } + cameraSource = + new CameraSource.Builder(context, barcodeDetector).setAutoFocusEnabled(autofocus_enabled) + .setFacing(facing) + .setRequestedPreviewSize(width, height) + .build(); } /** @@ -241,5 +189,49 @@ private boolean hasCameraHardware(Context context) { private boolean hasAutofocus(Context context) { return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_AUTOFOCUS); } + + public static class Builder { + private boolean autofocus_enabled; + private int width; + private int height; + private int facing; + private QRDataListener qrDataListener; + private Context context; + private SurfaceView surfaceView; + + public Builder(Context context, SurfaceView surfaceView, QRDataListener qrDataListener) { + this.autofocus_enabled = true; + this.width = 800; + this.height = 800; + this.facing = CameraSource.CAMERA_FACING_BACK; + this.qrDataListener = qrDataListener; + this.context = context; + this.surfaceView = surfaceView; + } + + public Builder enableAutofocus(boolean autofocus_enabled) { + this.autofocus_enabled = autofocus_enabled; + return this; + } + + public Builder width(int width) { + this.width = width; + return this; + } + + public Builder height(int height) { + this.height = height; + return this; + } + + public Builder facing(int facing) { + this.facing = facing; + return this; + } + + public QREader build() { + return new QREader(this); + } + } } From 0c82b88ced64847553e7b0ff876965839bdf55d5 Mon Sep 17 00:00:00 2001 From: Nishant Srivastava Date: Mon, 23 May 2016 14:06:23 +0530 Subject: [PATCH 2/5] [*] bumped up version of mobile-vision lib to v9.0.0 and of the lib to v.1.0.5 --- README.md | 2 +- library/build.gradle | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 8b31234..e46d3d1 100755 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Android library using google's mobile vision api to read QR Code #Integration - QREader is available in the MavenCentral, so getting it as simple as adding it as a dependency ```gradle -compile 'com.github.nisrulz:qreader:1.0.4' +compile 'com.github.nisrulz:qreader:1.0.5' ``` #Usage diff --git a/library/build.gradle b/library/build.gradle index 2f2300a..b1202ad 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -25,8 +25,8 @@ android { defaultConfig { minSdkVersion 9 targetSdkVersion 23 - versionCode 5 - versionName "1.0.4" + versionCode 6 + versionName "1.0.5" consumerProguardFiles 'consumer-proguard-rules.pro' } buildTypes { @@ -40,7 +40,7 @@ android { dependencies { testCompile 'junit:junit:4.12' // Add Vision API - compile "com.google.android.gms:play-services-vision:8.4.0" + compile "com.google.android.gms:play-services-vision:9.0.0" } // Place this in the 'build.gradle' of the library module From 7ef202a436bd8e2446b10d2811a690e8f95e2b0e Mon Sep 17 00:00:00 2001 From: Nishant Srivastava Date: Mon, 23 May 2016 14:26:22 +0530 Subject: [PATCH 3/5] [*] updated Readme and constant reference --- README.md | 144 +++++++++++------- .../nisrulz/projectqreader/MainActivity.java | 2 +- .../java/github/nisrulz/qreader/QREader.java | 4 +- 3 files changed, 91 insertions(+), 59 deletions(-) diff --git a/README.md b/README.md index e46d3d1..47d2e31 100755 --- a/README.md +++ b/README.md @@ -22,76 +22,108 @@ compile 'com.github.nisrulz:qreader:1.0.5' SurfaceView surfaceView = (SurfaceView) findViewById(R.id.camera_view); ``` -+ Next setup the config with `QRDataListener` as the last argument in any of the `setConfig()` calls - + The default config uses autofocus, back camera and preview size set at 800x800 and is referenced as below ++ Next create an object of `QREader` using the `Builder` in your `onCreate()` ```java - QREader.getInstance().setUpConfig(new QRDataListener() { - @Override - public void onDetected(final String data) { - Log.d("QREader", "Value : " + data); - - // Post data on UI Thread - textView_qrcode_info.post(new Runnable() { - @Override - public void run() { - textView_qrcode_info.setText(data); - } - }); - } - }); + QREader qrEader; + . + .. + ... + @Override protected void onCreate(Bundle savedInstanceState) { + .. + + surfaceView = (SurfaceView) findViewById(R.id.camera_view); + + qrEader = new QREader.Builder(this, surfaceView, new QRDataListener() { + @Override public void onDetected(final String data) { + // Do something with the string data + + Log.d("QREader", "Value : " + data); + } + }).build(); + } ``` - + There are other config methods to give you more granular configurations + *where* + + `Builder` takes in arguments as `Builder(context, surfaceview, qrdatalistner)` + + To modify further you can call below functions on the `Builder` before calling the `build()` + + `enableAutofocus(boolean autofocus_enabled)` // Default is `true` + + `width(int width)` // Default is `800` + + `height(int height)` // Default is `800` + + `facing(int facing)` // Default is `QREader.BACK_CAM` + + where argument can be one of `QREader.BACK_CAM` , `QREader.FRONT_CAM` + ++ Next Call ` qrEader.init()`, right after you `build()` your object using the `Builder`. ```java - // Disable/Enable autofocus - // Choose between Front facing or Back facing camera | Possible arguments : CameraSource.CAMERA_FACING_BACK / CameraSource.CAMERA_FACING_FRONT - public void setUpConfig(boolean autofocus_enabled, int facing, QRDataListener qrDataListener) { - // Change all the config values - public void setUpConfig(boolean autofocus_enabled, int width, int height, int facing, QRDataListener qrDataListener) { - ``` - -+ Call `QREader.getInstance().init()` with required arguments in your Activity code, to start reading QR code. -```java -QREader.getInstance().init(this, surfaceView); -``` - -*where* - -|argument|type| -|---|---| -|this|`Context`| -|surfaceView|`SurfaceView`| - + @Override protected void onCreate(Bundle savedInstanceState) { + .. + + qrEader = new QREader.Builder(this, surfaceView, new QRDataListener() { + @Override public void onDetected(final String data) { + .. + } + }).build(); + + // Call Init + qrEader.init(); + } + ``` -+ To start QR code detection ++ To start QR code detection, call `start()` on the `qreader` object ```java -QREader.getInstance().start(); +qrEader.start(); ``` -+ To stop QR code detection ++ To stop QR code detection, call `stop()` on the `qreader` object ```java -QREader.getInstance().stop(); +qrEader.stop(); ``` -+ To `releaseAndCleanup` by QREader ++ To release and cleanup , call `releaseAndCleanup()` on the `qreader` object ```java -QREader.getInstance().releaseAndCleanup(); +qrEader.releaseAndCleanup(); ``` A typical use case would be , which works well with locking your device and when the app goes into background and then comes back in foreground ```java - @Override protected void onStart() { - super.onStart(); - - // Call in onStart - QREader.getInstance().start(); - } - - @Override protected void onDestroy() { - super.onDestroy(); - - // Call in onDestroy - QREader.getInstance().stop(); - QREader.getInstance().releaseAndCleanup(); - } + private SurfaceView surfaceView; + private TextView textView_qrcode_info; + QREader qrEader; + + @Override protected void onCreate(Bundle savedInstanceState) { + ... + ... + + surfaceView = (SurfaceView) findViewById(R.id.camera_view); + textView_qrcode_info = (TextView) findViewById(R.id.code_info); + + qrEader = new QREader.Builder(this, surfaceView, new QRDataListener() { + @Override public void onDetected(final String data) { + Log.d("QREader", "Value : " + data); + textView_qrcode_info.post(new Runnable() { + @Override public void run() { + textView_qrcode_info.setText(data); + } + }); + } + }).build(); + + qrEader.init(); + } + + @Override protected void onStart() { + super.onStart(); + ... + + // Call in onStart + qrEader.start(); + } + + @Override protected void onDestroy() { + super.onDestroy(); + .. + + // Call in onDestroy + qrEader.stop(); + qrEader.releaseAndCleanup(); + } ``` diff --git a/app/src/main/java/github/nisrulz/projectqreader/MainActivity.java b/app/src/main/java/github/nisrulz/projectqreader/MainActivity.java index 31a9739..850b8d9 100644 --- a/app/src/main/java/github/nisrulz/projectqreader/MainActivity.java +++ b/app/src/main/java/github/nisrulz/projectqreader/MainActivity.java @@ -47,7 +47,7 @@ public class MainActivity extends AppCompatActivity { }); } }).build(); - + qrEader.init(); } diff --git a/library/src/main/java/github/nisrulz/qreader/QREader.java b/library/src/main/java/github/nisrulz/qreader/QREader.java index d0e2c2e..ba48b68 100644 --- a/library/src/main/java/github/nisrulz/qreader/QREader.java +++ b/library/src/main/java/github/nisrulz/qreader/QREader.java @@ -96,7 +96,7 @@ public void init() { } }); } else { - Log.e(LOGTAG,"Barcode recognition libs are not downloaded and are not operational"); + Log.e(LOGTAG, "Barcode recognition libs are not downloaded and are not operational"); } // Setup Camera @@ -203,7 +203,7 @@ public Builder(Context context, SurfaceView surfaceView, QRDataListener qrDataLi this.autofocus_enabled = true; this.width = 800; this.height = 800; - this.facing = CameraSource.CAMERA_FACING_BACK; + this.facing = BACK_CAM; this.qrDataListener = qrDataListener; this.context = context; this.surfaceView = surfaceView; From 7dd679f4ddf6fd749b5c250bcc004bff53b6979c Mon Sep 17 00:00:00 2001 From: Nishant Srivastava Date: Mon, 23 May 2016 14:28:59 +0530 Subject: [PATCH 4/5] [*] updated readme to specify optional tag --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 47d2e31..7c47570 100755 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ SurfaceView surfaceView = (SurfaceView) findViewById(R.id.camera_view); *where* + `Builder` takes in arguments as `Builder(context, surfaceview, qrdatalistner)` - + To modify further you can call below functions on the `Builder` before calling the `build()` + + **[Optional]** To modify further you can call below functions on the `Builder` before calling the `build()` + `enableAutofocus(boolean autofocus_enabled)` // Default is `true` + `width(int width)` // Default is `800` + `height(int height)` // Default is `800` From b5559433b73a262d90142be11a1ac15cc69c27ab Mon Sep 17 00:00:00 2001 From: Nishant Srivastava Date: Mon, 23 May 2016 14:32:38 +0530 Subject: [PATCH 5/5] [+] added the optional calls to builder in sample app --- .../java/github/nisrulz/projectqreader/MainActivity.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/github/nisrulz/projectqreader/MainActivity.java b/app/src/main/java/github/nisrulz/projectqreader/MainActivity.java index 850b8d9..9cefe15 100644 --- a/app/src/main/java/github/nisrulz/projectqreader/MainActivity.java +++ b/app/src/main/java/github/nisrulz/projectqreader/MainActivity.java @@ -46,8 +46,12 @@ public class MainActivity extends AppCompatActivity { } }); } - }).build(); - + }).facing(QREader.BACK_CAM) + .enableAutofocus(true) + .height(800) + .width(800) + .build(); + qrEader.init(); }