From a55eea80cdbe6bda2bdbaa44dade935cb9c37b66 Mon Sep 17 00:00:00 2001 From: Nishant Srivastava Date: Tue, 17 May 2016 22:24:07 +0530 Subject: [PATCH 1/2] [*] updated code to fix issue #8 , bumped up the version. --- README.md | 2 +- app/build.gradle | 2 +- .../nisrulz/projectqreader/MainActivity.java | 22 ++++++------ gradle/wrapper/gradle-wrapper.properties | 2 +- library/build.gradle | 4 +-- .../java/github/nisrulz/qreader/QREader.java | 35 ++++++++++--------- 6 files changed, 34 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 8adf8df..de38f60 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.3' +compile 'com.github.nisrulz:qreader:1.0.4' ``` #Usage diff --git a/app/build.gradle b/app/build.gradle index f07e9cc..418a43a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -38,6 +38,6 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' - compile 'com.android.support:appcompat-v7:23.3.0' + compile 'com.android.support:appcompat-v7:23.4.0' compile project(':library') } diff --git a/app/src/main/java/github/nisrulz/projectqreader/MainActivity.java b/app/src/main/java/github/nisrulz/projectqreader/MainActivity.java index 4e2b23d..62eda69 100644 --- a/app/src/main/java/github/nisrulz/projectqreader/MainActivity.java +++ b/app/src/main/java/github/nisrulz/projectqreader/MainActivity.java @@ -17,10 +17,10 @@ package github.nisrulz.projectqreader; import android.os.Bundle; +import android.os.PersistableBundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.SurfaceView; -import android.widget.Button; import android.widget.TextView; import github.nisrulz.qreader.QRDataListener; import github.nisrulz.qreader.QREader; @@ -29,8 +29,6 @@ public class MainActivity extends AppCompatActivity { private SurfaceView surfaceView; private TextView textView_qrcode_info; - private Button btn_toggle; - private boolean isRunning = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -53,24 +51,24 @@ public class MainActivity extends AppCompatActivity { QREader.getInstance().init(this, surfaceView); } - @Override protected void onResume() { - super.onResume(); + @Override public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) { + super.onSaveInstanceState(outState, outPersistentState); + } - QREader.getInstance().start(); - isRunning = true; + @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { + super.onRestoreInstanceState(savedInstanceState); } - @Override protected void onPause() { - super.onPause(); + @Override protected void onStart() { + super.onStart(); - QREader.getInstance().stop(); - isRunning = false; + QREader.getInstance().start(); } @Override protected void onDestroy() { super.onDestroy(); + QREader.getInstance().stop(); QREader.getInstance().releaseAndCleanup(); - isRunning = false; } } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index cdbac48..ed96b98 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -19,4 +19,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.12-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-all.zip diff --git a/library/build.gradle b/library/build.gradle index 79d4ff8..2f2300a 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -25,8 +25,8 @@ android { defaultConfig { minSdkVersion 9 targetSdkVersion 23 - versionCode 4 - versionName "1.0.3" + versionCode 5 + versionName "1.0.4" consumerProguardFiles 'consumer-proguard-rules.pro' } buildTypes { diff --git a/library/src/main/java/github/nisrulz/qreader/QREader.java b/library/src/main/java/github/nisrulz/qreader/QREader.java index dfc184a..e9b0a94 100644 --- a/library/src/main/java/github/nisrulz/qreader/QREader.java +++ b/library/src/main/java/github/nisrulz/qreader/QREader.java @@ -115,11 +115,28 @@ public void init(final Context context, final SurfaceView surfaceView) { this.context = context; this.surfaceView = surfaceView; + // Setup Barcodedetector if (barcodeDetector == null) { barcodeDetector = new BarcodeDetector.Builder(context).setBarcodeFormats(Barcode.QR_CODE).build(); + + 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); + } + } + }); + } } + // Setup Camera if (cameraSource == null) { cameraSource = new CameraSource.Builder(context, barcodeDetector).setAutoFocusEnabled(autofocus_enabled) @@ -144,24 +161,10 @@ public void start() { } @Override public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) { - 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 surfaceDestroyed(SurfaceHolder surfaceHolder) { - + stop(); } }; @@ -202,7 +205,7 @@ public void stop() { } /** - * Release and cleanup qr eader. + * Release and cleanup qreader. */ public void releaseAndCleanup() { stop(); From 18b03c03aa7783df66177bcd733daded79673c15 Mon Sep 17 00:00:00 2001 From: Nishant Srivastava Date: Tue, 17 May 2016 22:51:34 +0530 Subject: [PATCH 2/2] [*] updated readme with proper use-case code [+] added check for camera permission, autofocus and camera hardware detection --- README.md | 18 +++++++++ .../nisrulz/projectqreader/MainActivity.java | 13 ++----- .../java/github/nisrulz/qreader/QREader.java | 37 ++++++++++++++++--- 3 files changed, 53 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index de38f60..8b31234 100755 --- a/README.md +++ b/README.md @@ -76,6 +76,24 @@ QREader.getInstance().stop(); QREader.getInstance().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(); + } +``` + > NOTE : diff --git a/app/src/main/java/github/nisrulz/projectqreader/MainActivity.java b/app/src/main/java/github/nisrulz/projectqreader/MainActivity.java index 62eda69..57f4d77 100644 --- a/app/src/main/java/github/nisrulz/projectqreader/MainActivity.java +++ b/app/src/main/java/github/nisrulz/projectqreader/MainActivity.java @@ -17,7 +17,6 @@ package github.nisrulz.projectqreader; import android.os.Bundle; -import android.os.PersistableBundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.SurfaceView; @@ -51,24 +50,18 @@ public class MainActivity extends AppCompatActivity { QREader.getInstance().init(this, surfaceView); } - @Override public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) { - super.onSaveInstanceState(outState, outPersistentState); - } - - @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { - super.onRestoreInstanceState(savedInstanceState); - } - @Override protected void onStart() { super.onStart(); + // Call in onStart QREader.getInstance().start(); } @Override protected void onDestroy() { super.onDestroy(); - QREader.getInstance().stop(); + // Call in onDestroy + QREader.getInstance().stop(); QREader.getInstance().releaseAndCleanup(); } } diff --git a/library/src/main/java/github/nisrulz/qreader/QREader.java b/library/src/main/java/github/nisrulz/qreader/QREader.java index e9b0a94..ec54640 100644 --- a/library/src/main/java/github/nisrulz/qreader/QREader.java +++ b/library/src/main/java/github/nisrulz/qreader/QREader.java @@ -34,8 +34,7 @@ * QREader Singleton */ public class QREader { - - private static final String TAG = "QREader"; + private static final String LOGTAG = "QREader"; private CameraSource cameraSource = null; private BarcodeDetector barcodeDetector = null; @@ -115,6 +114,20 @@ public void init(final Context context, final SurfaceView surfaceView) { this.context = context; this.surfaceView = surfaceView; + if (!hasAutofocus(context)) { + Log.e(LOGTAG, "Do not have autofocus feature, disabling autofocus feature in the library!"); + autofocus_enabled = false; + } + + if (!hasCameraHardware(context)) { + Log.e(LOGTAG, "Does not have camera hardware!"); + return; + } + if (!checkCameraPermission(context)) { + Log.e(LOGTAG, "Do not have camera permission!"); + return; + } + // Setup Barcodedetector if (barcodeDetector == null) { barcodeDetector = @@ -174,14 +187,14 @@ private void startCameraView(Context context, CameraSource cameraSource, if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { - Log.e(TAG, "Permission not granted!"); + Log.e(LOGTAG, "Permission not granted!"); return; } else if (!cameraRunning && cameraSource != null && surfaceView != null) { cameraSource.start(surfaceView.getHolder()); cameraRunning = true; } } catch (IOException ie) { - Log.e(TAG, ie.getMessage()); + Log.e(LOGTAG, ie.getMessage()); ie.printStackTrace(); } } @@ -199,7 +212,7 @@ public void stop() { cameraRunning = false; } } catch (Exception ie) { - Log.e(TAG, ie.getMessage()); + Log.e(LOGTAG, ie.getMessage()); ie.printStackTrace(); } } @@ -214,5 +227,19 @@ public void releaseAndCleanup() { cameraSource = null; } } + + private boolean checkCameraPermission(Context context) { + String permission = Manifest.permission.CAMERA; + int res = context.checkCallingOrSelfPermission(permission); + return (res == PackageManager.PERMISSION_GRANTED); + } + + private boolean hasCameraHardware(Context context) { + return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA); + } + + private boolean hasAutofocus(Context context) { + return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_AUTOFOCUS); + } }