Camera 2 error when using Vision API (barcode scan)

I don't know why I am getting Camera Error 2 when I launch the application ...

The app needs to have CameraPreview

where I can detect the barcode and once found I want to use the data (like in Toast elsewhere).

What is the problem?

Error stack

05-07 10:37:35.641 10828-10835/ibas.placeanorder W/art: Suspending all threads took: 5.398ms
05-07 10:37:36.750 10828-10828/ibas.placeanorder I/Choreographer: Skipped 74 frames!  The application may be doing too much work on its main thread.
05-07 10:37:45.463 10828-10828/ibas.placeanorder E/Camera: Error 
205-07 10:37:45.490 10828-10828/ibas.placeanorder I/InputMethodManager [IMM] startInputInner - mService.startInputOrWindowGainedFocus

      

activity

public class WelcomeActivity extends CaptureActivity {

//Apache License for Scanner:
//http://www.apache.org/licenses/LICENSE-2.0

SurfaceView cameraPreview;
BarcodeDetector barcodeDetector;
CameraSource cameraSource;
final int REQUEST_CAMERA_PERSMISSION = 1001;

RequestQueue queue;

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch(requestCode){
        case REQUEST_CAMERA_PERSMISSION:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
                if(ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
                    return;
                }
                try{
                    cameraSource.start(cameraPreview.getHolder());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome);

    queue = Volley.newRequestQueue(this);

    cameraPreview = (SurfaceView) findViewById(R.id.cameraPreview);

    barcodeDetector = new BarcodeDetector.Builder(this)
            .setBarcodeFormats(Barcode.QR_CODE)
            .build();

    cameraSource = new CameraSource
            .Builder(this, barcodeDetector)
            .setRequestedPreviewSize(640, 480)
            .build();

    //Add Event
    cameraPreview.getHolder().addCallback(new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder surfaceHolder) {
            if (ActivityCompat.checkSelfPermission(WelcomeActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                //Request Permission
                ActivityCompat.requestPermissions(WelcomeActivity.this,
                        new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERSMISSION);
                return;
            }
            try {
                cameraSource.start(cameraPreview.getHolder());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

        }

        @Override
        public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
            cameraSource.stop();
        }
    });

    barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
        @Override
        public void release() {

        }

        @Override
        public void receiveDetections(Detector.Detections<Barcode> detections) {
            SparseArray<Barcode> qrcodes = detections.getDetectedItems();
            if(qrcodes.size() != 0){
                Toast.makeText(WelcomeActivity.this, "Qr-Code gefunden: " + qrcodes.valueAt(0).displayValue, Toast.LENGTH_SHORT).show();
                Log.i("111111", "Qr-Code gefunden: " + qrcodes.valueAt(0).displayValue);
            }
        }
    });
}

// Get the results:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if(result != null) {
        if(result.getContents() == null) {
            Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
            Log.i("scanned ", result.getContents());

            CharSequence qrCodeBedingung = "qrc.sx";
            if(result.getContents().contains(qrCodeBedingung)){
                Log.i("qrCode? ", "Ja, QR-Code ist korrekt: " + result.getContents());

                Log.i("code", result.getContents().substring(result.getContents().length()-6));

            }else{

                new AlertDialog.Builder(WelcomeActivity.this)
                        .setTitle("Information")
                        .setMessage("Kein gΓΌltiger QR-Code.")
                        .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                                dialogInterface.cancel();
                            }
                        }).show();
            }
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}}

      

Thank.

+3


source to share





All Articles