Skip to content

Commit

Permalink
Updated to latest release of Google Vision API
Browse files Browse the repository at this point in the history
Now uses default autofocus feature
Minor improvements
  • Loading branch information
Gnzlt committed Dec 24, 2015
1 parent 16c67c6 commit 995cc0d
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 146 deletions.
4 changes: 2 additions & 2 deletions AndroidVisionQRReader/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
buildToolsVersion "23.0.2"

defaultConfig {
minSdkVersion 9
Expand All @@ -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.+'
}
5 changes: 5 additions & 0 deletions AndroidVisionQRReader/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
package="com.gnzlt.AndroidVisionQRReader"
xmlns:android="http://schemas.android.com/apk/res/android">

<uses-feature android:name="android.hardware.camera"/>

<uses-permission android:name="android.permission.CAMERA"/>

<meta-data
android:name="com.google.android.gms.vision.DEPENDENCIES"
android:value="barcode"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>

<application
android:label="@string/app_name">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand All @@ -103,14 +103,14 @@ public void receiveDetections(Detector.Detections<Barcode> 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();
}

Expand Down Expand Up @@ -146,7 +146,9 @@ private void returnData(String data) {
@Override
protected void onPause() {
super.onPause();
mPreview.stop();
if (mPreview != null) {
mPreview.stop();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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) {
}
}
}

This file was deleted.

7 changes: 4 additions & 3 deletions AndroidVisionQRReader/src/main/res/layout/activity_qr.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
tools:context=".QRActivity">

<com.gnzlt.AndroidVisionQRReader.camera.CameraSourcePreview
android:id="@+id/cameraSourcePreview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true" />
android:layout_height="match_parent"/>
</RelativeLayout>
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
buildToolsVersion "23.0.2"

defaultConfig {
applicationId "com.example.qrtest"
minSdkVersion 9
targetSdkVersion 23
versionCode 1
versionName "1.0"
versionCode 2
versionName "1.1"
}
buildTypes {
release {
Expand Down
12 changes: 8 additions & 4 deletions app/src/main/java/com/example/qrtest/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,14 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == QR_REQUEST) {
if (resultCode == RESULT_OK)
mResultTextView.setText(data.getStringExtra(QRActivity.EXTRA_QR_RESULT));
else
mResultTextView.setText("Error");
String result;
if (resultCode == RESULT_OK) {
result = data.getStringExtra(QRActivity.EXTRA_QR_RESULT);
} else {
result = "Error";
}
mResultTextView.setText(result);
mResultTextView.setVisibility(View.VISIBLE);
}
}
}
22 changes: 11 additions & 11 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<Button
android:id="@+id/scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="requestQRCodeScan"
android:text="Scan QR Code" />
android:text="Scan QR Code"/>

<TextView
android:id="@+id/result"
Expand All @@ -24,6 +24,6 @@
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="Scan Result" />

android:text="Scan Result"
android:visibility="gone"/>
</RelativeLayout>

0 comments on commit 995cc0d

Please sign in to comment.