将条形码编号发送到没有黑屏的活动对话框

时间:2017-06-09 01:49:29

标签: java android surfaceview

我制作了一个应用程序来读取条形码数字,我使用了带有相机源的表面视图。应用程序将值发送到另一个显示为对话框的活动。 我将其他活动作为对话框,但表面视图停止的问题。我尝试了很多解决方案,直到将这些行放在onCreate方法中。

 setTheme(android.R.style.Theme_DeviceDefault_Dialog);        
 requestWindowFeature(Window.FEATURE_NO_TITLE);

在onCreate方法中添加这两行,表面视图没有停止并且它可以工作,但问题是黑屏中显示的对话框也显示对话框时显示对话框全部为黑色。如何解决这个问题。

我还修改了清单来处理它,并添加以下内容

 <activity android:name=".InfoActivity"
            android:excludeFromRecents="true"
            android:label="@string/app_name"
            android:launchMode="singleInstance"
            android:theme="@style/Theme.AppCompat.Light.Dialog">
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>

                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>

这些类的整个代码

   public class MainActivity extends AppCompatActivity {

    SurfaceView surfaceView;
    BarcodeDetector barcodeDetector;
    CameraSource cameraSource;
    TextView txtShow, itemName, itemPrice, itemQuantity;
    TextView txtNameDialog, txtPriceDialog, txtQuantityDialog;
    final int RequestCameraPermissionId = 1001;
    String code;
    RequestQueue requestQueue;

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case RequestCameraPermissionId: {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {

                        return;
                    }
                    try {
                        cameraSource.start(surfaceView.getHolder());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            break;
        }
    }

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

        surfaceView = (SurfaceView) findViewById(R.id.cameraPreview);
        txtShow = (TextView) findViewById(R.id.txtShow);
        itemName = (TextView) findViewById(R.id.itemName);
        itemPrice = (TextView) findViewById(R.id.itemPrice);
        itemQuantity = (TextView) findViewById(R.id.itemQuantity);

        startBarcode();

    }

    public void startBarcode() {
        barcodeDetector = new BarcodeDetector.Builder(this)
                .setBarcodeFormats(Barcode.ALL_FORMATS)
                .build();
        cameraSource = new CameraSource.Builder(this, barcodeDetector)
                .setRequestedPreviewSize(800, 600)
                .setAutoFocusEnabled(true)
                .build();

        //Events

        surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                if (ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                    //Make Request Runtime Permission
                    ActivityCompat.requestPermissions(MainActivity.this,
                            new String[]{Manifest.permission.CAMERA}, RequestCameraPermissionId);
                    return;
                }
                try {
                    cameraSource.start(surfaceView.getHolder());

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {

                    //Make Request Runtime Permission
                    ActivityCompat.requestPermissions(MainActivity.this,
                            new String[]{Manifest.permission.CAMERA}, RequestCameraPermissionId);
                    return;
                }
                try {
                    cameraSource.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

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

        barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {

            @Override
            public void release() {

            }

            @Override
            public void receiveDetections(Detector.Detections<Barcode> detections) {
                final SparseArray<Barcode> qrCodes = detections.getDetectedItems();
                if (qrCodes.size() != 0) {
                    txtShow.post(new Runnable() {
                        @Override
                        public void run() {
                            //stop camera
                            cameraSource.stop();

                            //create vibrate
                            Vibrator vibrator = (Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
                            vibrator.vibrate(500);

                            //set result for Text View
                            txtShow.setText(qrCodes.valueAt(0).displayValue);
                            final String code = txtShow.getText().toString();
                            // surfaceView.setTop(200);


                            final MediaPlayer mp = MediaPlayer.create(MainActivity.this, R.raw.barcode);
                            mp.start();
                            Button getInfo = (Button) findViewById(R.id.getInfo);
                            Intent intent = new Intent(MainActivity.this,InfoActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);

0 个答案:

没有答案