Bom dia,
Estou tentando integrar um leitor de código de barras a minha aplicação mas não estou conseguindo fazer com que a câmera detecte o código de barras, nem QRCode nem nada. Notei também que a câmera não da foco, pode ser isso que esteja causando o problema mas não vejo razão para isso pois já adicionei todas as permissões no manifest:
meu código:
.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scanner);
Bundle args = getIntent().getExtras();
preview = (SurfaceView) findViewById(R.id.camera_view);
textView = (TextView) findViewById(R.id.code_info);
barcodeDetector =
new BarcodeDetector.Builder(this)
.setBarcodeFormats(Barcode.QR_CODE)
.build();
preview.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
cameraSource.start(preview.getHolder());
Log.i("camera","iniciando camera");
} 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();
}
});
barcodeDetector.setProcessor(new FocusingProcessor<Barcode>(barcodeDetector, new Tracker<Barcode>()) {
@Override
public void receiveDetections(Detector.Detections<Barcode> detections){
if(detections != null){
SparseArray<Barcode> barcodes = detections.getDetectedItems();
if(barcodes != null && barcodes.size() > 0){
Log.i("entrou","entrou");
}
}
}
@Override
public int selectFocus(Detector.Detections<Barcode> detections) {
return 0;
}
@Override
public void release(){
super.release();
}
});
cameraSource = new CameraSource
.Builder(this, barcodeDetector)
.setFacing(CameraSource.CAMERA_FACING_BACK)
.setRequestedPreviewSize(640, 480)
.build();
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:padding="16dp">
<SurfaceView
android:layout_width="640px"
android:layout_height="480px"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:id="@+id/camera_view"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/code_info"
android:layout_toRightOf="@+id/camera_view"
android:textSize="20sp"
android:layout_marginLeft="16dp"
android:text="Nothing to read."
android:layout_alignParentTop="true"
/>
</RelativeLayout>
Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:installLocation="preferExternal"
android:screenOrientation="portrait"
...>
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardwar.ListaProe.camera" />
<uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:screenOrientation="portrait"
android:supportsRtl="true"
android:theme="@style/AppTheme"
...>
<meta-data android:name="com.google.android.gms.vision.DEPENDENCIES" android:value="barcode"/>
Eu testei com QRCode pois acho que o foco não faz muita diferença nesse caso, mas sem o foco é impossível ler código de barras