Android cameraSource.top()导致应用程序冻结

Android cameraSource.stop() causing app to freeze(Android cameraSource.top()导致应用程序冻结)

本文介绍了Android cameraSource.top()导致应用程序冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,有一个使用谷歌视觉API的二维码扫描仪。在读取二维码后,我无法停止摄像头。流程为MainActivity -> QrActivity 收到二维码检测后,应用程序应返回主活动。

如果我不调用cameraSource.release(),它工作得很好,但设备会发热很多,并且对电池耗尽有很大影响。然而,如果我释放摄像头信号源,mainActivity将变得没有响应,应用程序将崩溃。

为什么它变得无响应?释放摄像信号源的正确位置在哪里?

季度活动

@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_qr);
    cancelBtn = (Button) findViewById(R.id.cancel_button);
    cancelBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onBackPressed();
        }
    });

    new QrReader(this);
}

QrReader类

public class QrReader {

    private static final String TAG = "QrReader";

    private SurfaceView cameraView;
    private TextView barcodeInfo;
    private BarcodeDetector barcodeDetector;
    private CameraSource cameraSource;
    private Activity mActivity;
    private AccessPointCredentials barCodeData;

    public QrReader(Activity activity) {
        this.mActivity = activity;

        cameraView = (SurfaceView) mActivity.findViewById(R.id.camera_view);
        barcodeInfo = (TextView) mActivity.findViewById(R.id.code_info);

        barcodeDetector =
                new BarcodeDetector.Builder(mActivity)
                        .setBarcodeFormats(Barcode.QR_CODE)
                        .build();

        cameraSource = new CameraSource
                .Builder(mActivity, barcodeDetector)
                .setAutoFocusEnabled(true)
                .build();

        cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {

            @Override
            public void surfaceCreated(SurfaceHolder holder) {

                cameraSource = new CameraSource
                        .Builder(mActivity, barcodeDetector)
                        .setAutoFocusEnabled(true)
                        .setFacing(0)
                        .build();
                try {                  
                    cameraSource.start(cameraView.getHolder());

                } catch (Exception ioe) {
                    ioe.printStackTrace();
                }
            }

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

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                  // Log.i(TAG, "surfaceDestroyed: stopping camera Source");

                  // cameraSource.release();
            }
        });

        barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
            @Override
            public void release() {
                Log.i(TAG, "release: ");
            }

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

                if (barCodes.size() != 0) {

                    Log.i(TAG, "received a Barcode");
                    barcodeInfo.post(new Runnable() {    // Use the post method of the TextView
                        public void run() {
                            barcodeInfo.setText(barCodes.valueAt(0).displayValue);

                        }
                    });
                    Gson g = new Gson();
                    try {
                        barCodeData = g.fromJson(barCodes.valueAt(0).rawValue, AccessPointCredentials.class);
                    } catch (Exception e) {
                        barCodeData = new AccessPointCredentials();
                        barCodeData.setSsid(barCodes.valueAt(0).rawValue);
                        barCodeData.setPass(null);
                        e.printStackTrace();
                    }

                    connectToWifi(barCodeData);

                    // CameraSource.release causes app to freeze

                    // cameraSource.release();
                }
            }
        });
    }

    private void connectToWifi(final AccessPointCredentials credentials) {

                //wificonnect code

    }

}

推荐答案

已经3个月了,但我只是偶然发现了同样的问题并解决了它。ReceiveDettions方法中的代码在不同的线程上运行,因此,如果您想要执行需要UI线程的操作,则需要从处理程序发送它:

Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
     @Override
     public void run() {
         cameraSource.release();
     }
});

这篇关于Android cameraSource.top()导致应用程序冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Android cameraSource.top()导致应用程序冻结

基础教程推荐