4

I made an application for barcode detector so I used SurfaceView to handle camerasource. After camera detects barcode and returns its value I make it to stop. There is no function to restart the camera source and surface view on any event just one fuction which is camersource.start();

It starts but the problem it works in the background and I can't see anything how to fix this problem? and display surfaceview and camera source again.

here is the whole code of the activity onCreate Method

 @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);

        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() {
                            //create vibrate
                            Vibrator vibrator = (Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
                            vibrator.vibrate(500);


                            //set result for Text View
                            txtShow.setText(qrCodes.valueAt(0).displayValue);

                            // surfaceView.setTop(200);
                            cameraSource.stop();


                            final MediaPlayer mp = MediaPlayer.create(MainActivity.this, R.raw.barcode);
                            mp.start();
Abdulwahid
  • 337
  • 3
  • 14
  • If I didn't stop camera source it works continuously without stopping. – Abdulwahid May 27 '17 at 10:39
  • Try an easy hack: when you stop cameraService, remove the surfaceView; when you want to restart, simply add a new SurfaceView to your content View. – Alex Cohn May 27 '17 at 11:13
  • @AlexCohn it is a good idea but how to remove it and add new one – Abdulwahid May 27 '17 at 11:23
  • To remove, you can refer to this answer: https://stackoverflow.com/questions/6526874/call-removeview-on-the-childs-parent-first. To add the SurfaceView smoothly, you can keep the layout params of the old camera preview. – Alex Cohn May 27 '17 at 17:07
  • @AlexCohn I couldn't get the idea if you can show me the solution and I will be appreciate that for you. – Abdulwahid May 28 '17 at 03:51

2 Answers2

5

to restart after cameraSource.stop() you need:

cameraSource.start(surfaceView.getHolder());
mjames276
  • 71
  • 1
  • 7
  • I try this before it starts again but it runs in the background as you see in my question that camera source still stopped and doesn't get restarted, so camera source still stopped and nothing shown in it. – Abdulwahid Oct 18 '17 at 09:07
  • my question how to restart both surfaceview and camera source again in a specific condition like on button clicked. – Abdulwahid Oct 18 '17 at 09:23
1

After code captured you may stop camera source and restart OnCreate methode again. or you can call the activity again by using Intent like:

Intent intent= new Intent (getBaseContext,MainActivity.class); // your activity
startActivity(intent);
Amjad
  • 1,604
  • 1
  • 10
  • 14