There is a google library, this is a codelab hers.
To use just add this dependency to your Gradle
compile 'com.google.android.gms:play-services-vision:10.0.1'
Example class of Barcode
import android.content.Intent;
import android.hardware.Camera;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.util.SparseArray;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;
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;
public class BarcodeScannerActivity extends Activity {
SurfaceView cameraView;
BarcodeDetector barcodeDetector;
CameraSource cameraSource;
SparseArray<Barcode> barcodes;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_barcodescanner);
cameraView = (SurfaceView) findViewById(R.id.surface_camera);
barcodeDetector = new BarcodeDetector.Builder(BarcodeScannerActivity.this)
//Aqui você escolhe que tipo de leitura ele vai suportar
.setBarcodeFormats(Barcode.ALL_FORMATS)
.build();
setupCamera();
setupReader();
}
private void setupCamera() {
CameraSource.Builder builder = new CameraSource.Builder(getApplicationContext(), barcodeDetector);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
builder = builder.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
}
cameraSource = builder.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH).build();
cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
try {
cameraSource.start(cameraView.getHolder());
} catch (IOException | SecurityException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
cameraSource.stop();
}
});
}
private void setupReader() {
barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
@Override
public void release() {
}
@Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
//Quando le algum codigo de barra ele cai aqui
barcodes = detections.getDetectedItems();
if (barcodes != null && barcodes.size() >= 1) {
String barcode = barcodes.valueAt(0).displayValue;
}
}
});
}
}
Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<SurfaceView
android:id="@+id/surface_camera"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:fitsSystemWindows="true" />
<TextView
android:id="@+id/scanText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="ESCANEANDO"
android:textSize="40sp" />
Try Google. https://developers.google.com/vision/
– Tiago P Casemiro