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

0

Good morning,

I'm trying to integrate a barcode reader into my application but I can not get 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 because I have already added all the 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 at how the camera looks:

I tested with QRCode because I think the focus does not make much difference in this case, but without the focus it is impossible to read barcode

    
asked by anonymous 31.01.2017 / 13:08

2 answers

0

The class responsible for setting focus on your camera is the Camera in 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 look 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're not sure about using api at Github has a starter justation using barcode-reader . It's worth just making a clone give a study to improve knowledge.

See also on this gringo site (en) a basic tutorial on using api. It has an example showing how to run the scanner of a QRCode and printing a TextView to the phrase "Hello! This is a test!"     

31.01.2017 / 16:57
0

Attempts to set .setAutoFocusEnabled(true) and .setRequestedFps(15.0f) to 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();
    
07.11.2018 / 21:39