-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBarcodeReader.java
More file actions
125 lines (113 loc) · 4.63 KB
/
BarcodeReader.java
File metadata and controls
125 lines (113 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.vicepredator.barcodereader;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.util.SparseArray;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;
import androidx.core.app.ActivityCompat;
import com.google.android.gms.vision.CameraSource;
import com.google.android.gms.vision.Detector;
import com.google.android.gms.vision.barcode.Barcode;
import com.google.android.gms.vision.barcode.BarcodeDetector;
import java.io.IOException;
/*
* Barcode reader by Vicepredator11
*
* to use this library follow theese steps:
* -Add the following lines to AndroidManifest.xml file:
* <meta-data android:name="com.google.android.gms.vision.DEPENDENCIES" android:value="barcode"/>
* <uses-permission android:name="android.permission.CAMERA" />
* -Add the following line to build.gradle file:
* implementation 'com.google.android.gms:play-services-vision:10.0.0'
* -Create an object in you activity the following way:
* BarcodeReader yourBarcodeReader = new BarcodeReader(yourActivity.this, yourSurfaceView);
* -To use the reader you need to create an event listenere this way:
* yourBarcodeReader.setBarcodeReaderListener(new BarcodeReader.BarcodeReaderListener() {
* @Override
* public void onCodeScanned(String yourReadedBarcode) {
* //Your code here
* //yourReadedBarcode contains the value of the barcode you have read
* }
* });
*/
public class BarcodeReader {
private static final String NEEDED_PERMISSION = "android.permission.CAMERA";
private static final int REQUEST_ID = 1;
private BarcodeDetector detector;
private SurfaceView surfaceView;
private CameraSource cameraSource;
private Context context;
private Activity activity;
private String lastCode = "";
private BarcodeReaderListener listener;
public interface BarcodeReaderListener{
public void onCodeScanned(String code);
}
public BarcodeReader(Activity act, SurfaceView surface) {
context = act.getApplicationContext();
surfaceView = surface;
activity = act;
this.listener = null;
detector = new BarcodeDetector.Builder(context).setBarcodeFormats(Barcode.QR_CODE | Barcode.EAN_13).build();
if (!detector.isOperational()) {
Toast.makeText(context, "Unable to activare the barcode reader", Toast.LENGTH_LONG);
return;
}
cameraSource = new CameraSource
.Builder(context, detector)
.setAutoFocusEnabled(true)
.build();
surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
activateCamera();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
cameraSource.stop();
}
});
}
public void setBarcodeReaderListener(BarcodeReaderListener listener){
this.listener = listener;
}
public void scan(){
detector.setProcessor(new Detector.Processor<Barcode>() {
@Override
public void release() {
}
@Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
String thisCode;
final SparseArray<Barcode> items = detections.getDetectedItems();
if (items.size() != 0){
thisCode = items.valueAt(0).rawValue;
if(!thisCode.equalsIgnoreCase(lastCode)) {
lastCode = thisCode;
if (listener != null)
listener.onCodeScanned(items.valueAt(0).rawValue);
}
}
}
});
}
private void activateCamera() {
if (ActivityCompat.checkSelfPermission(context, NEEDED_PERMISSION) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, NEEDED_PERMISSION)) {
} else {
ActivityCompat.requestPermissions(activity, new String[]{NEEDED_PERMISSION},REQUEST_ID);
}
} else {
try {
cameraSource.start(surfaceView.getHolder());
} catch (IOException e) {
Toast.makeText(context,"Errore while starting camera",Toast.LENGTH_LONG);
}
}
}
}