停止相机并刷新

时间:2017-07-12 09:06:19

标签: android barcode vision

此对话框在扫描给定的Qr代码时会多次显示。怎么做读取给定的QR码的值停止了相机和如何按下确定按钮,相机刷新。

这是我的代码

private CameraSource mCameraSource;
private CameraSourcePreview mPreview;

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_activity2);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.ic_restart);

        Typeface custom_fonts = Typeface.createFromAsset(getAssets(), "fonts/OpenSans-Regular.ttf");
        title_app = (TextView) findViewById(R.id.title_application_scan);
        title_app.setTypeface(custom_fonts);

        helper = new DatabaseHandler(this);

        Default app = (Default) getApplicationContext();
        passwordString = app.getUuidString();
        eightChars = getIntent().getStringExtra("eightChars");

        scanQrLayout = (LinearLayout) findViewById(R.id.topLayout);
        mPreview = (CameraSourcePreview) findViewById(R.id.preview);
        mGraphicOverlay = (GraphicOverlay<BarcodeGraphic>) findViewById(R.id.graphicOverlay);

        int rc = ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
        if (rc == PackageManager.PERMISSION_GRANTED) {
            createCamera();
        } else {
            requestCameraPermission();
        }
    }

   private void createCamera() {
        boolean autoFocus = false;
        boolean useFlash = false;

        Context context = getApplicationContext();
        BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(context).build();
        BarcodeTrackerFactory barcodeFactory = new BarcodeTrackerFactory(mGraphicOverlay);
        barcodeDetector.setProcessor(
                new MultiProcessor.Builder<>(barcodeFactory).build());

        if (!barcodeDetector.isOperational()) {
            IntentFilter lowstorageFilter = new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW);
            boolean hasLowStrage = registerReceiver(null, lowstorageFilter) != null;
            if (hasLowStrage) {
                Toast.makeText(this, R.string.low_strange_error, Toast.LENGTH_SHORT).show();
            }
        }

        CameraSource.Builder builder = new CameraSource.Builder(getApplicationContext(), barcodeDetector)
                .setFacing(CameraSource.CAMERA_FACING_BACK)
                .setRequestedPreviewSize(1600, 1024)
                .setRequestedFps(15.0f);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            builder = builder.setFocusMode(
                    autoFocus ? Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE : null);
        }
        mCameraSource = builder
                .setFlashMode(useFlash ? Camera.Parameters.FLASH_MODE_TORCH : null)
                .build();

        barcodeFactory.setNewBarcodeListener(new BarcodeTrackerFactory.OnNewBarcodeListener() {
            @Override
            public void onNewItem(final Barcode item) {
                Log.d("BarcodeFound", "Found new barcode! " + item.rawValue);
                title_app.post(new Runnable() {
                    @Override
                    public void run() {

                        String[] dataCard = new String[5];



                        Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                        vibrator.vibrate(400);
                        barcodeString = item.displayValue;


                        if ( barcodeString == null) {
                            mPreview.stop();
                            showAlterDialog("Błędny QR Kod", "Podany kod QR code jest błędny. Zeskanuj go ponownie.");

                        }
}

0 个答案:

没有答案