Barcodedetector does not detect barcode and camera does not focus (I have already put the permissions in the manifest)

Asked

Viewed 555 times

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"/>

Look how the camera looks: inserir a descrição da imagem aqui

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

2 answers

1

Try to put .setAutoFocusEnabled(true) and .setRequestedFps(15.0f) in the cameraSource.

In your case it would look like this:

cameraSource = new CameraSource
     .Builder(this, barcodeDetector)
     .setFacing(CameraSource.CAMERA_FACING_BACK)
     .setRequestedPreviewSize(640, 480)
     .setAutoFocusEnabled(true)
     .setRequestedFps(15.0f)
     .build();

1

The class responsible for setting focus on your camera is the Camera on which you have the call Camera.Parameters that has some important constants for configuration. See below:

Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE,
Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO,
Camera.Parameters.FOCUS_MODE_AUTO,
Camera.Parameters.FOCUS_MODE_EDOF,
Camera.Parameters.FOCUS_MODE_FIXED,
Camera.Parameters.FOCUS_MODE_INFINITY,
Camera.Parameters.FOCUS_MODE_MACRO

In practice it would be something like:

try {
    Camera camera = (Camera) field.get(cameraSource);
    if (camera != null) {
        Camera.Parameters params = camera.getParameters();

        if (!params.getSupportedFocusModes().contains(focusMode)) {
            return false;
        }

        params.setFocusMode(focusMode);
        camera.setParameters(params);
        return true;
    }

    return false;
} catch (IllegalAccessException e) {
    e.printStackTrace();
}

If you are in doubt about using the api, on Github has a Starter juxtaposition using the Barcode-Reader. It’s worth just making one clone give a studied to improve knowledge.

See here too on this gringo site a basic tutorial on using the api. In it is an example showing the scanner of a Qrcode and printing on a TextView the phrase "Hello! This is a test!"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.