1
Good morning,
I’m trying to integrate a barcode reader into my application but I’m not getting the camera to detect the barcode, or Qrcode or anything. I also noticed that the camera does not focus, this may be what is causing the problem but I see no reason for this as I have already added all permissions in the manifest:
my code:
.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"/>
I tested with Qrcode because I think the focus doesn’t make much difference in this case, but without the focus is impossible to read barcode