QR码扫描仪,receiveDetections方法

时间:2017-11-02 20:49:11

标签: java android

这是我的代码并且完美地运作

public class MainActivity extends AppCompatActivity {

private CameraSource cameraSource;
private SurfaceView cameraView;
private final int MY_PERMISSIONS_REQUEST_CAMERA = 1;
private String token = "";
private String tokenanterior = "";

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

    cameraView = (SurfaceView) findViewById(R.id.camera_view);
    initQR();
}

public void initQR() {

    // creo el detector qr
    BarcodeDetector barcodeDetector =
            new BarcodeDetector.Builder(this)
                    .setBarcodeFormats(Barcode.ALL_FORMATS)
                    .build();


    cameraSource = new CameraSource
            .Builder(this, barcodeDetector)
            .setRequestedPreviewSize(1600, 1024)
            .setAutoFocusEnabled(true) //you should add this feature
            .build();


    cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {


            if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA)
                    != PackageManager.PERMISSION_GRANTED) {

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

                    if (shouldShowRequestPermissionRationale(
                            Manifest.permission.CAMERA)) ;
                    requestPermissions(new String[]{Manifest.permission.CAMERA},
                            MY_PERMISSIONS_REQUEST_CAMERA);
                }
                return;
            } else {
                try {
                    cameraSource.start(cameraView.getHolder());
                } 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();
        }
    });

    // preparo el detector de QR
    barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
        @Override
        public void release() {
        }


        @Override
        public void receiveDetections(Detector.Detections<Barcode> detections) {
            final SparseArray<Barcode> barcodes = detections.getDetectedItems();

            if (barcodes.size() > 0) {

                // get token
                token = barcodes.valueAt(0).displayValue.toString();



                if (!token.equals(tokenanterior)) {

                    //save last token procesed
                    tokenanterior = token;
                    Log.i("token", token);

                    if (URLUtil.isValidUrl(token)) {
                        // if is a URL open browser
                        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(token));
                        startActivity(browserIntent);
                    } else {
                        // share with other apps
                        Intent shareIntent = new Intent();
                        shareIntent.setAction(Intent.ACTION_SEND);
                        shareIntent.putExtra(Intent.EXTRA_TEXT, token);
                        shareIntent.setType("text/plain");
                        startActivity(shareIntent);
                    }

                    new Thread(new Runnable() {
                        public void run() {
                            try {
                                synchronized (this) {
                                    wait(5000);
                                    // clear token
                                    tokenanterior = "";
                                }
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                Log.e("Error", "Waiting didnt work!!");
                                e.printStackTrace();
                            }
                        }
                    }).start();

                }
            }
        }
    });

}

}

但我在代码中不能做任何其他事情,例如我在这里添加了一个吐司:

   if (URLUtil.isValidUrl(token)) {
                        // si es una URL valida abre el navegador
                        Intent browserIntent = new 
Intent(Intent.ACTION_VIEW, Uri.parse(token));
                        startActivity(browserIntent);
                    } else {
                        // comparte en otras apps
                        //EnviarCodigo(token);
                        Toast.makeText(getApplicationContext(),"Se registró correctamente",Toast.LENGTH_SHORT).show();
                        Intent shareIntent = new Intent();
                        shareIntent.setAction(Intent.ACTION_SEND);
                        shareIntent.putExtra(Intent.EXTRA_TEXT, token);
                        shareIntent.setType("text/plain");
                        startActivity(shareIntent);
                    }

toast不显示,app不与其他应用共享QR码,或者例如我在这里添加了祝酒词:

@Override
        public void receiveDetections(Detector.Detections<Barcode> detections) {
            final SparseArray<Barcode> barcodes = detections.getDetectedItems();

            if (barcodes.size() > 0) {

                // get token
                token = barcodes.valueAt(0).displayValue.toString();
                Toast.makeText(getApplicationContext(),"Se registró correctamente",Toast.LENGTH_SHORT).show();

,该应用程序也无法正常工作。我究竟做错了什么?请帮帮我。

源代码:http://www.desarrollolibre.net/blog/tema/267/android/como-crear-un-lector-de-codigos-qr-en-android-con-android-studio#.WftoDteWZPY

0 个答案:

没有答案