From 18b03c03aa7783df66177bcd733daded79673c15 Mon Sep 17 00:00:00 2001 From: Nishant Srivastava Date: Tue, 17 May 2016 22:51:34 +0530 Subject: [PATCH] [*] 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); + } }