Skip to content

Commit 995cc0d

Browse files
committed
Updated to latest release of Google Vision API
Now uses default autofocus feature Minor improvements
1 parent 16c67c6 commit 995cc0d

File tree

9 files changed

+67
-146
lines changed

9 files changed

+67
-146
lines changed

AndroidVisionQRReader/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
22

33
android {
44
compileSdkVersion 23
5-
buildToolsVersion "23.0.1"
5+
buildToolsVersion "23.0.2"
66

77
defaultConfig {
88
minSdkVersion 9
@@ -21,5 +21,5 @@ dependencies {
2121
compile fileTree(dir: 'libs', include: ['*.jar'])
2222

2323
compile 'com.android.support:appcompat-v7:23.1.+'
24-
compile 'com.google.android.gms:play-services-vision:8.1.+'
24+
compile 'com.google.android.gms:play-services-vision:8.4.+'
2525
}

AndroidVisionQRReader/src/main/AndroidManifest.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
package="com.gnzlt.AndroidVisionQRReader"
44
xmlns:android="http://schemas.android.com/apk/res/android">
55

6+
<uses-feature android:name="android.hardware.camera"/>
7+
68
<uses-permission android:name="android.permission.CAMERA"/>
79

810
<meta-data
911
android:name="com.google.android.gms.vision.DEPENDENCIES"
1012
android:value="barcode"/>
13+
<meta-data
14+
android:name="com.google.android.gms.version"
15+
android:value="@integer/google_play_services_version"/>
1116

1217
<application
1318
android:label="@string/app_name">

AndroidVisionQRReader/src/main/java/com/gnzlt/AndroidVisionQRReader/QRActivity.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
public class QRActivity extends AppCompatActivity {
2626

27-
private static final String TAG = "QRActivity";
2827
public static final String EXTRA_QR_RESULT = "EXTRA_QR_RESULT";
28+
private static final String TAG = "QRActivity";
2929
private static final int PERMISSIONS_REQUEST = 100;
3030

3131
private BarcodeDetector mBarcodeDetector;
@@ -77,7 +77,7 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, in
7777
}
7878

7979
private void setupBarcodeDetector() {
80-
mBarcodeDetector = new BarcodeDetector.Builder(this)
80+
mBarcodeDetector = new BarcodeDetector.Builder(getApplicationContext())
8181
.setBarcodeFormats(Barcode.QR_CODE)
8282
.build();
8383

@@ -103,14 +103,14 @@ public void receiveDetections(Detector.Detections<Barcode> detections) {
103103

104104
if (!mBarcodeDetector.isOperational())
105105
Log.w(TAG, "Detector dependencies are not yet available.");
106-
107106
}
108107

109108
private void setupCameraSource() {
110-
mCameraSource = new CameraSource.Builder(this, mBarcodeDetector)
109+
mCameraSource = new CameraSource.Builder(getApplicationContext(), mBarcodeDetector)
111110
.setFacing(CameraSource.CAMERA_FACING_BACK)
112111
.setRequestedFps(15.0f)
113112
.setRequestedPreviewSize(1600, 1024)
113+
.setAutoFocusEnabled(true)
114114
.build();
115115
}
116116

@@ -146,7 +146,9 @@ private void returnData(String data) {
146146
@Override
147147
protected void onPause() {
148148
super.onPause();
149-
mPreview.stop();
149+
if (mPreview != null) {
150+
mPreview.stop();
151+
}
150152
}
151153

152154
@Override

AndroidVisionQRReader/src/main/java/com/gnzlt/AndroidVisionQRReader/camera/CameraSourcePreview.java

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import android.content.Context;
1919
import android.content.res.Configuration;
20-
import android.hardware.Camera;
2120
import android.util.AttributeSet;
2221
import android.util.Log;
2322
import android.view.SurfaceHolder;
@@ -50,7 +49,7 @@ public CameraSourcePreview(Context context, AttributeSet attrs) {
5049
addView(mSurfaceView);
5150
}
5251

53-
public void start(CameraSource cameraSource) throws IOException {
52+
public void start(CameraSource cameraSource) throws IOException, SecurityException {
5453
if (cameraSource == null) {
5554
stop();
5655
}
@@ -76,35 +75,13 @@ public void release() {
7675
}
7776
}
7877

79-
private void startIfReady() throws IOException {
78+
private void startIfReady() throws IOException, SecurityException {
8079
if (mStartRequested && mSurfaceAvailable) {
8180
mCameraSource.start(mSurfaceView.getHolder());
82-
VisionApiCameraFix.cameraFocus(mCameraSource, Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
8381
mStartRequested = false;
8482
}
8583
}
8684

87-
private class SurfaceCallback implements SurfaceHolder.Callback {
88-
@Override
89-
public void surfaceCreated(SurfaceHolder surface) {
90-
mSurfaceAvailable = true;
91-
try {
92-
startIfReady();
93-
} catch (IOException e) {
94-
Log.e(TAG, "Could not start camera source.", e);
95-
}
96-
}
97-
98-
@Override
99-
public void surfaceDestroyed(SurfaceHolder surface) {
100-
mSurfaceAvailable = false;
101-
}
102-
103-
@Override
104-
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
105-
}
106-
}
107-
10885
@Override
10986
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
11087
int width = 320;
@@ -143,6 +120,8 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
143120

144121
try {
145122
startIfReady();
123+
} catch (SecurityException se) {
124+
Log.e(TAG, "Do not have permission to start the camera", se);
146125
} catch (IOException e) {
147126
Log.e(TAG, "Could not start camera source.", e);
148127
}
@@ -160,4 +139,27 @@ private boolean isPortraitMode() {
160139
Log.d(TAG, "isPortraitMode returning false by default");
161140
return false;
162141
}
142+
143+
private class SurfaceCallback implements SurfaceHolder.Callback {
144+
@Override
145+
public void surfaceCreated(SurfaceHolder surface) {
146+
mSurfaceAvailable = true;
147+
try {
148+
startIfReady();
149+
} catch (SecurityException se) {
150+
Log.e(TAG, "Do not have permission to start the camera", se);
151+
} catch (IOException e) {
152+
Log.e(TAG, "Could not start camera source.", e);
153+
}
154+
}
155+
156+
@Override
157+
public void surfaceDestroyed(SurfaceHolder surface) {
158+
mSurfaceAvailable = false;
159+
}
160+
161+
@Override
162+
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
163+
}
164+
}
163165
}

AndroidVisionQRReader/src/main/java/com/gnzlt/AndroidVisionQRReader/camera/VisionApiCameraFix.java

Lines changed: 0 additions & 93 deletions
This file was deleted.
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
1+
<RelativeLayout
2+
xmlns:android="http://schemas.android.com/apk/res/android"
23
xmlns:tools="http://schemas.android.com/tools"
34
android:layout_width="match_parent"
45
android:layout_height="match_parent"
6+
android:keepScreenOn="true"
57
tools:context=".QRActivity">
68

79
<com.gnzlt.AndroidVisionQRReader.camera.CameraSourcePreview
810
android:id="@+id/cameraSourcePreview"
911
android:layout_width="match_parent"
10-
android:layout_height="match_parent"
11-
android:layout_centerVertical="true" />
12+
android:layout_height="match_parent"/>
1213
</RelativeLayout>

app/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ apply plugin: 'com.android.application'
22

33
android {
44
compileSdkVersion 23
5-
buildToolsVersion "23.0.1"
5+
buildToolsVersion "23.0.2"
66

77
defaultConfig {
88
applicationId "com.example.qrtest"
99
minSdkVersion 9
1010
targetSdkVersion 23
11-
versionCode 1
12-
versionName "1.0"
11+
versionCode 2
12+
versionName "1.1"
1313
}
1414
buildTypes {
1515
release {

app/src/main/java/com/example/qrtest/MainActivity.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,14 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
5757
super.onActivityResult(requestCode, resultCode, data);
5858

5959
if (requestCode == QR_REQUEST) {
60-
if (resultCode == RESULT_OK)
61-
mResultTextView.setText(data.getStringExtra(QRActivity.EXTRA_QR_RESULT));
62-
else
63-
mResultTextView.setText("Error");
60+
String result;
61+
if (resultCode == RESULT_OK) {
62+
result = data.getStringExtra(QRActivity.EXTRA_QR_RESULT);
63+
} else {
64+
result = "Error";
65+
}
66+
mResultTextView.setText(result);
67+
mResultTextView.setVisibility(View.VISIBLE);
6468
}
6569
}
6670
}
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2-
xmlns:tools="http://schemas.android.com/tools"
3-
android:layout_width="match_parent"
4-
android:layout_height="match_parent"
5-
android:paddingBottom="@dimen/activity_vertical_margin"
6-
android:paddingLeft="@dimen/activity_horizontal_margin"
7-
android:paddingRight="@dimen/activity_horizontal_margin"
8-
android:paddingTop="@dimen/activity_vertical_margin"
9-
tools:context=".MainActivity">
2+
xmlns:tools="http://schemas.android.com/tools"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:paddingBottom="@dimen/activity_vertical_margin"
6+
android:paddingLeft="@dimen/activity_horizontal_margin"
7+
android:paddingRight="@dimen/activity_horizontal_margin"
8+
android:paddingTop="@dimen/activity_vertical_margin"
9+
tools:context=".MainActivity">
1010

1111
<Button
1212
android:id="@+id/scan"
1313
android:layout_width="wrap_content"
1414
android:layout_height="wrap_content"
1515
android:layout_centerInParent="true"
1616
android:onClick="requestQRCodeScan"
17-
android:text="Scan QR Code" />
17+
android:text="Scan QR Code"/>
1818

1919
<TextView
2020
android:id="@+id/result"
@@ -24,6 +24,6 @@
2424
android:layout_centerHorizontal="true"
2525
android:layout_marginTop="20dp"
2626
android:gravity="center"
27-
android:text="Scan Result" />
28-
27+
android:text="Scan Result"
28+
android:visibility="gone"/>
2929
</RelativeLayout>

0 commit comments

Comments
 (0)