Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
517 views
in Technique[技术] by (71.8m points)

methods - How to stop and re-start java code in mainactivity (QRcode, Android, Vision )

I found a nice code for QRcode/Barcode recognition - it is working fine BUT ! It is working non-stop. Detected code is displayed in barcodeText but the method/proces/camera (initialiseDetectorsAndSources()) is still working. I tried a few things to stop it and found that cameraSource.release() is somewhat working : camera stops but I'm not sure if the detector process is still running somewhere in background ?. Then I added a button to start the initialiseDetectorsAndSources() method again => to start the process of QRCode recognition - but it is not starting. The camera is not working.

How can I stop the process or recognition of a code after founding the code ?

How can I start again the process to read next qrcode ?

Can I move the method initialiseDetectorsAndSources() to a new class, outside the mainactivity ? ... this method starts after the program is launched - I would like to start the process of code recognition after ...for example I press a button.

Thanks for help :)

here is my MainActivity.java :

package net.ginekolog.qrtest01;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.media.ToneGenerator;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.vision.CameraSource;
import com.google.android.gms.vision.Detector;
import com.google.android.gms.vision.barcode.Barcode;
import com.google.android.gms.vision.barcode.BarcodeDetector;

import java.io.IOException;
import java.util.Objects;

public class MainActivity extends AppCompatActivity {

    private SurfaceView surfaceView;
    private BarcodeDetector barcodeDetector;
    private CameraSource cameraSource;
    private static final int REQUEST_CAMERA_PERMISSION = 201;
    private ToneGenerator toneGen1;
    private TextView barcodeText;
    private String barcodeData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toneGen1 = new ToneGenerator(AudioManager.STREAM_MUSIC, 100);
        surfaceView = findViewById(R.id.surface_view);
        barcodeText = findViewById(R.id.barcode_text);

        Button button = (Button) findViewById(R.id.startQRscanButton);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Do something in response to button click
                initialiseDetectorsAndSources();
                Toast.makeText(getApplicationContext(), "button clicked", Toast.LENGTH_LONG).show();
            }
                    });

        // initialiseDetectorsAndSources();
        Toast.makeText(getApplicationContext(), "onCreate : end", Toast.LENGTH_LONG).show();
    }

    private void initialiseDetectorsAndSources() {

        Toast.makeText(getApplicationContext(), "iD&S : Barcode scanner started", Toast.LENGTH_SHORT).show();

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

        cameraSource = new CameraSource.Builder(this, barcodeDetector)
                .setRequestedPreviewSize(1024, 768)//1920x1080
                .setAutoFocusEnabled(true)//you should add this feature
                //.setRequestedFps(10)//default is 30
                .build();

        surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                try {
                    if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
                        cameraSource.start(surfaceView.getHolder());
                    } else {
                        ActivityCompat.requestPermissions(MainActivity.this, new
                                String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            }

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

        barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
            @Override
            public void release() {
                Toast.makeText(getApplicationContext(), "release : To prevent memory leaks barcode scanner has been stopped", Toast.LENGTH_SHORT).show();
            }

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

                    barcodeText.post(new Runnable() {

                        @Override
                        public void run() {

                            if (barcodes.valueAt(0).email != null)
                            {
                                barcodeText.removeCallbacks(null);
                                barcodeData = barcodes.valueAt(0).email + " - email ";
                                // barcodeData = barcodes.valueAt(0).email.address;
                                // barcodeData = barcodes.valueAt(0).rawValue;
                                barcodeText.setText(barcodeData);
                                toneGen1.startTone(ToneGenerator.TONE_CDMA_PIP, 1150);
                                // stop kamera
                                cameraSource.release();
                            }
                            else
                            {
                                barcodeData = barcodes.valueAt(0).displayValue + " display value ";
                                barcodeText.setText(barcodeData);
                                toneGen1.startTone(ToneGenerator.TONE_CDMA_PIP, 1150);
                                // stop ... how ?
                                // cameraSource.stop();
                                // barcodes.clear();
                                cameraSource.release();
                                // barcodeDetector.release();
                                // surfaceView.releasePointerCapture();
                                // surfaceView.clearFocus();
                                // release();
                            }
                        }
                    });

                }
            }
        });
    };

    @Override
    protected void onPause() {
        super.onPause();
        Objects.requireNonNull(getSupportActionBar()).show();
        cameraSource.release();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Objects.requireNonNull(getSupportActionBar()).show();
        initialiseDetectorsAndSources();
    }
}
question from:https://stackoverflow.com/questions/65941574/how-to-stop-and-re-start-java-code-in-mainactivity-qrcode-android-vision

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

So far no one tried to answer this question so I tried to find some solutions. What I found : part of this activity is run in background so I think the best idea is to put the whole class into separate activity and then call it from main activity. When this activity is finished I will go back to the main activity thus finishing background activity of the camera and barcodeDetector. Good link how to stop activity : How to close activity and go back to previous activity in android


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...