Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  [*] updated readme with proper use-case code [+] added check for camera permission, autofocus and camera hardware detection
  [*] updated code to fix issue #8 , bumped up the version.
  • Loading branch information
nisrulz committed May 17, 2016
2 parents 0b3cd52 + 18b03c0 commit 354b58d
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 40 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,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
Expand Down Expand Up @@ -78,6 +78,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
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
19 changes: 5 additions & 14 deletions app/src/main/java/github/nisrulz/projectqreader/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
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;
Expand All @@ -29,8 +28,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);
Expand All @@ -53,24 +50,18 @@ public class MainActivity extends AppCompatActivity {
QREader.getInstance().init(this, surfaceView);
}

@Override protected void onResume() {
super.onResume();
@Override protected void onStart() {
super.onStart();

// Call in onStart
QREader.getInstance().start();
isRunning = true;
}

@Override protected void onPause() {
super.onPause();

QREader.getInstance().stop();
isRunning = false;
}

@Override protected void onDestroy() {
super.onDestroy();

// Call in onDestroy
QREader.getInstance().stop();
QREader.getInstance().releaseAndCleanup();
isRunning = false;
}
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
72 changes: 51 additions & 21 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,11 +114,42 @@ 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 =
new BarcodeDetector.Builder(context).setBarcodeFormats(Barcode.QR_CODE).build();

if (barcodeDetector.isOperational()) {
barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
@Override public void release() {

}

@Override public void receiveDetections(Detector.Detections<Barcode> detections) {
final SparseArray<Barcode> 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)
Expand All @@ -144,24 +174,10 @@ public void start() {
}

@Override public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
if (barcodeDetector.isOperational()) {
barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
@Override public void release() {

}

@Override public void receiveDetections(Detector.Detections<Barcode> detections) {
final SparseArray<Barcode> barcodes = detections.getDetectedItems();
if (barcodes.size() != 0 && qrDataListener != null) {
qrDataListener.onDetected(barcodes.valueAt(0).displayValue);
}
}
});
}
}

@Override public void surfaceDestroyed(SurfaceHolder surfaceHolder) {

stop();
}
};

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

/**
* Release and cleanup qr eader.
* Release and cleanup qreader.
*/
public void releaseAndCleanup() {
stop();
Expand All @@ -211,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 354b58d

Please sign in to comment.