From 995cc0d91f3fe20973de83f6060871d182f7b5cc Mon Sep 17 00:00:00 2001
From: Gonzalo Toledano
Date: Thu, 24 Dec 2015 12:11:16 +0100
Subject: [PATCH] Updated to latest release of Google Vision API Now uses
default autofocus feature Minor improvements
---
AndroidVisionQRReader/build.gradle | 4 +-
.../src/main/AndroidManifest.xml | 5 +
.../AndroidVisionQRReader/QRActivity.java | 12 ++-
.../camera/CameraSourcePreview.java | 52 ++++++-----
.../camera/VisionApiCameraFix.java | 93 -------------------
.../src/main/res/layout/activity_qr.xml | 7 +-
app/build.gradle | 6 +-
.../java/com/example/qrtest/MainActivity.java | 12 ++-
app/src/main/res/layout/activity_main.xml | 22 ++---
9 files changed, 67 insertions(+), 146 deletions(-)
delete mode 100644 AndroidVisionQRReader/src/main/java/com/gnzlt/AndroidVisionQRReader/camera/VisionApiCameraFix.java
diff --git a/AndroidVisionQRReader/build.gradle b/AndroidVisionQRReader/build.gradle
index cb5b2a0..247136d 100644
--- a/AndroidVisionQRReader/build.gradle
+++ b/AndroidVisionQRReader/build.gradle
@@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
android {
compileSdkVersion 23
- buildToolsVersion "23.0.1"
+ buildToolsVersion "23.0.2"
defaultConfig {
minSdkVersion 9
@@ -21,5 +21,5 @@ dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.+'
- compile 'com.google.android.gms:play-services-vision:8.1.+'
+ compile 'com.google.android.gms:play-services-vision:8.4.+'
}
diff --git a/AndroidVisionQRReader/src/main/AndroidManifest.xml b/AndroidVisionQRReader/src/main/AndroidManifest.xml
index f7c744d..b703228 100644
--- a/AndroidVisionQRReader/src/main/AndroidManifest.xml
+++ b/AndroidVisionQRReader/src/main/AndroidManifest.xml
@@ -3,11 +3,16 @@
package="com.gnzlt.AndroidVisionQRReader"
xmlns:android="http://schemas.android.com/apk/res/android">
+
+
+
diff --git a/AndroidVisionQRReader/src/main/java/com/gnzlt/AndroidVisionQRReader/QRActivity.java b/AndroidVisionQRReader/src/main/java/com/gnzlt/AndroidVisionQRReader/QRActivity.java
index 4718651..3ab2c7c 100644
--- a/AndroidVisionQRReader/src/main/java/com/gnzlt/AndroidVisionQRReader/QRActivity.java
+++ b/AndroidVisionQRReader/src/main/java/com/gnzlt/AndroidVisionQRReader/QRActivity.java
@@ -24,8 +24,8 @@
public class QRActivity extends AppCompatActivity {
- private static final String TAG = "QRActivity";
public static final String EXTRA_QR_RESULT = "EXTRA_QR_RESULT";
+ private static final String TAG = "QRActivity";
private static final int PERMISSIONS_REQUEST = 100;
private BarcodeDetector mBarcodeDetector;
@@ -77,7 +77,7 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, in
}
private void setupBarcodeDetector() {
- mBarcodeDetector = new BarcodeDetector.Builder(this)
+ mBarcodeDetector = new BarcodeDetector.Builder(getApplicationContext())
.setBarcodeFormats(Barcode.QR_CODE)
.build();
@@ -103,14 +103,14 @@ public void receiveDetections(Detector.Detections detections) {
if (!mBarcodeDetector.isOperational())
Log.w(TAG, "Detector dependencies are not yet available.");
-
}
private void setupCameraSource() {
- mCameraSource = new CameraSource.Builder(this, mBarcodeDetector)
+ mCameraSource = new CameraSource.Builder(getApplicationContext(), mBarcodeDetector)
.setFacing(CameraSource.CAMERA_FACING_BACK)
.setRequestedFps(15.0f)
.setRequestedPreviewSize(1600, 1024)
+ .setAutoFocusEnabled(true)
.build();
}
@@ -146,7 +146,9 @@ private void returnData(String data) {
@Override
protected void onPause() {
super.onPause();
- mPreview.stop();
+ if (mPreview != null) {
+ mPreview.stop();
+ }
}
@Override
diff --git a/AndroidVisionQRReader/src/main/java/com/gnzlt/AndroidVisionQRReader/camera/CameraSourcePreview.java b/AndroidVisionQRReader/src/main/java/com/gnzlt/AndroidVisionQRReader/camera/CameraSourcePreview.java
index ace3147..cb31990 100755
--- a/AndroidVisionQRReader/src/main/java/com/gnzlt/AndroidVisionQRReader/camera/CameraSourcePreview.java
+++ b/AndroidVisionQRReader/src/main/java/com/gnzlt/AndroidVisionQRReader/camera/CameraSourcePreview.java
@@ -17,7 +17,6 @@
import android.content.Context;
import android.content.res.Configuration;
-import android.hardware.Camera;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
@@ -50,7 +49,7 @@ public CameraSourcePreview(Context context, AttributeSet attrs) {
addView(mSurfaceView);
}
- public void start(CameraSource cameraSource) throws IOException {
+ public void start(CameraSource cameraSource) throws IOException, SecurityException {
if (cameraSource == null) {
stop();
}
@@ -76,35 +75,13 @@ public void release() {
}
}
- private void startIfReady() throws IOException {
+ private void startIfReady() throws IOException, SecurityException {
if (mStartRequested && mSurfaceAvailable) {
mCameraSource.start(mSurfaceView.getHolder());
- VisionApiCameraFix.cameraFocus(mCameraSource, Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
mStartRequested = false;
}
}
- private class SurfaceCallback implements SurfaceHolder.Callback {
- @Override
- public void surfaceCreated(SurfaceHolder surface) {
- mSurfaceAvailable = true;
- try {
- startIfReady();
- } catch (IOException e) {
- Log.e(TAG, "Could not start camera source.", e);
- }
- }
-
- @Override
- public void surfaceDestroyed(SurfaceHolder surface) {
- mSurfaceAvailable = false;
- }
-
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
- }
- }
-
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
int width = 320;
@@ -143,6 +120,8 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
try {
startIfReady();
+ } catch (SecurityException se) {
+ Log.e(TAG, "Do not have permission to start the camera", se);
} catch (IOException e) {
Log.e(TAG, "Could not start camera source.", e);
}
@@ -160,4 +139,27 @@ private boolean isPortraitMode() {
Log.d(TAG, "isPortraitMode returning false by default");
return false;
}
+
+ private class SurfaceCallback implements SurfaceHolder.Callback {
+ @Override
+ public void surfaceCreated(SurfaceHolder surface) {
+ mSurfaceAvailable = true;
+ try {
+ startIfReady();
+ } catch (SecurityException se) {
+ Log.e(TAG, "Do not have permission to start the camera", se);
+ } catch (IOException e) {
+ Log.e(TAG, "Could not start camera source.", e);
+ }
+ }
+
+ @Override
+ public void surfaceDestroyed(SurfaceHolder surface) {
+ mSurfaceAvailable = false;
+ }
+
+ @Override
+ public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
+ }
+ }
}
diff --git a/AndroidVisionQRReader/src/main/java/com/gnzlt/AndroidVisionQRReader/camera/VisionApiCameraFix.java b/AndroidVisionQRReader/src/main/java/com/gnzlt/AndroidVisionQRReader/camera/VisionApiCameraFix.java
deleted file mode 100644
index 57e0e05..0000000
--- a/AndroidVisionQRReader/src/main/java/com/gnzlt/AndroidVisionQRReader/camera/VisionApiCameraFix.java
+++ /dev/null
@@ -1,93 +0,0 @@
-package com.gnzlt.AndroidVisionQRReader.camera;
-
-import android.hardware.Camera;
-import android.support.annotation.NonNull;
-import android.support.annotation.StringDef;
-
-import com.google.android.gms.vision.CameraSource;
-
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.reflect.Field;
-
-public class VisionApiCameraFix {
- /*
- * IF YOU WANT TO JUST ACCESS THE CAMERA INSTANCE SO THAT YOU CAN SET ANY OF THE PARAMETERS, VISIT THE FOLLOWING LINK:
- * https://gist.github.com/Gericop/364dd12b105fdc28a0b6
- */
-
- /**
- * Custom annotation to allow only valid focus modes.
- */
- @StringDef({
- Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE,
- Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO,
- Camera.Parameters.FOCUS_MODE_AUTO,
- Camera.Parameters.FOCUS_MODE_EDOF,
- Camera.Parameters.FOCUS_MODE_FIXED,
- Camera.Parameters.FOCUS_MODE_INFINITY,
- Camera.Parameters.FOCUS_MODE_MACRO
- })
- @Retention(RetentionPolicy.SOURCE)
- private @interface FocusMode {
- }
-
- /**
- *
- * Sets the Mobile Vision API provided {@link com.google.android.gms.vision.CameraSource}'s
- * focus mode. Use {@link Camera.Parameters#FOCUS_MODE_CONTINUOUS_PICTURE} or
- * {@link Camera.Parameters#FOCUS_MODE_CONTINUOUS_VIDEO} for continuous autofocus.
- *
- *
- * Note that the CameraSource's {@link CameraSource#start()} or
- * {@link CameraSource#start(SurfaceHolder)} has to be called and the camera image has to be
- * showing prior using this method as the CameraSource only creates the camera after calling
- * one of those methods and the camera is not available immediately. You could implement some
- * kind of a callback method for the SurfaceHolder that notifies you when the imaging is ready
- * or use a direct action (e.g. button press) to set the focus mode.
- *
- *
- * Check out CameraSourcePreview.java
- * which contains the method startIfReady() that has the following line:
- *
mCameraSource.start(mSurfaceView.getHolder());
- * After this call you can use our cameraFocus(...) method because the camera is ready.
- *