Skip to content

Commit

Permalink
[*] updated readme with proper use-case code
Browse files Browse the repository at this point in the history
[+] added check for camera permission, autofocus and camera hardware detection
  • Loading branch information
nisrulz committed May 17, 2016
1 parent a55eea8 commit 18b03c0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 15 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 :

Expand Down
13 changes: 3 additions & 10 deletions app/src/main/java/github/nisrulz/projectqreader/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
37 changes: 32 additions & 5 deletions library/src/main/java/github/nisrulz/qreader/QREader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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();
}
}
Expand All @@ -199,7 +212,7 @@ public void stop() {
cameraRunning = false;
}
} catch (Exception ie) {
Log.e(TAG, ie.getMessage());
Log.e(LOGTAG, ie.getMessage());
ie.printStackTrace();
}
}
Expand All @@ -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);
}
}

0 comments on commit 18b03c0

Please sign in to comment.