Skip to content

Instantly share code, notes, and snippets.

@PattyAppier
Last active October 10, 2018 07:30
Show Gist options
  • Save PattyAppier/0a1aa36ad2f9fa3eeb3ee017cf1dc299 to your computer and use it in GitHub Desktop.
Save PattyAppier/0a1aa36ad2f9fa3eeb3ee017cf1dc299 to your computer and use it in GitHub Desktop.
GoogleMobileVision-BarcodeDetector
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.pattyappier.pattyluvapp.no5robot"
minSdkVersion 27
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:mediarouter-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.google.android.gms:play-services:12.0.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
<?xml version="1.0" encoding="utf-8"?>
<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:padding="16dp"
tools:context=".MainActivity">
<SurfaceView
android:layout_width="640dp"
android:layout_height="480dp"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:id="@+id/camera_view"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/code_info"
android:layout_marginStart="@+id/camera_view"
android:textSize="20sp"
android:text="Nothing to read."
android:layout_alignParentTop="true"
/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pattyappier.pattyluvapp.no5robot">
<meta-data android:name="com.google.android.gms.vision.DEPENDENCIES" android:value="barcode"/>
<uses-permission android:name="android.permission.CAMERA" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package com.pattyappier.pattyluvapp.no5robot;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap;
import com.google.android.gms.vision.barcode.BarcodeDetector;
import com.google.android.gms.vision.barcode.Barcode;
import com.google.android.gms.vision.Frame;
import android.util.Log;
import android.util.SparseArray;
import android.view.SurfaceView;
import android.widget.TextView;
import com.google.android.gms.vision.CameraSource;
import android.view.SurfaceHolder;
import java.io.IOException;
import com.google.android.gms.vision.Detector;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bitmap myQRCode = null;
try {
myQRCode = BitmapFactory.decodeStream(getAssets().open("myqrcode.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(this).setBarcodeFormats(Barcode.QR_CODE).build();
Frame myFrame = new Frame.Builder()
.setBitmap(myQRCode)
.build();
SparseArray<Barcode> barcodes = barcodeDetector.detect(myFrame);
if (barcodes.size() != 0) {
Log.d("My QR Code's Data",
barcodes.valueAt(0).displayValue
);
}
SurfaceView cameraView = findViewById(R.id.camera_view);
TextView barcodeInfo = findViewById(R.id.code_info);
barcodeDetector =
new BarcodeDetector.Builder(this)
.setBarcodeFormats(Barcode.QR_CODE)
.build();
CameraSource cameraSource = new CameraSource
.Builder(this, barcodeDetector)
.setRequestedPreviewSize(640, 480)
.build();
cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
cameraSource.start(cameraView.getHolder()); // cameraSource shall declared to be final
} catch (IOException ie) {
Log.e("CAMERA SOURCE", ie.getMessage());
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
cameraSource.stop(); // cameraSource shall declared to be final
}
});
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) {
// 因为receiveDetections在非UI线程中执行
barcodeInfo.post(new Runnable() { //barcodeInfo needs to be declared final
public void run() {
barcodeInfo.setText( //barcodeInfo needs to be declared final
barcodes.valueAt(0).displayValue
);
}
});
}
}
});
}//on crate
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment