5

我的项目中需要有一个 QR 扫描仪。我使用 ZBar 来完成这项任务。主要是我对 ZBar git example 给出的示例代码做了一个非常小的改动来完成这项工作。我需要在浏览器(如果是 url)或对话框中(如果是正常信息)显示扫描结果。

除非我在浏览器中扫描二维码后尝试显示 url,否则我的代码在下面给出的每件事都可以正常工作。当我从浏览器返回我的应用程序时,相机视口变黑并保持非活动状态。我试图让相机恢复活动以扫描另一个二维码,但我失败了。我试图在我的 onResume() 函数中重新打开相机,但这会导致错误并强制退出应用程序。请在那个问题上帮助我。

    package com.myapp;
    import net.sourceforge.zbar.Config;
    import net.sourceforge.zbar.Image;
    import net.sourceforge.zbar.ImageScanner;
    import net.sourceforge.zbar.Symbol;
    import net.sourceforge.zbar.SymbolSet;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.content.pm.ActivityInfo;
    import android.hardware.Camera;
    import android.hardware.Camera.AutoFocusCallback;
    import android.hardware.Camera.PreviewCallback;
    import android.hardware.Camera.Size;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Handler;
    import android.webkit.URLUtil;
    import android.widget.FrameLayout;

    import com.myapp.CameraPreview;
    /* Import ZBar Class files */

    public class QRScannerActivity extends Activity
    {
        private Camera mCamera;
        private CameraPreview mPreview;
        private Handler autoFocusHandler;

        ImageScanner scanner;

        private boolean barcodeScanned = false;
        private boolean previewing = true;

        static {
            System.loadLibrary("iconv");
        } 

        @Override
        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.qr_scanner_main);

            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

            autoFocusHandler = new Handler();
            mCamera = getCameraInstance();

            /* Instance barcode scanner */
            scanner = new ImageScanner();
            scanner.setConfig(0, Config.X_DENSITY, 3);
            scanner.setConfig(0, Config.Y_DENSITY, 3);

            mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB);
            FrameLayout preview = (FrameLayout)findViewById(R.id.cameraPreview);
            preview.addView(mPreview);
        }

        @Override
        protected void onPause() {
            super.onPause();
            releaseCamera();
            //finish();
        }

        /** A safe way to get an instance of the Camera object. */
        public static Camera getCameraInstance(){
            Camera c = null;
            try {
                c = Camera.open();
            } catch (Exception e){
            }
            return c;
        }

        private void releaseCamera() {
            //Toast.makeText(QRScannerActivity.this, "Paused State", Toast.LENGTH_SHORT).show();
            if (mCamera != null) {
                previewing = false;
                mCamera.setPreviewCallback(null);
                mPreview.getHolder().removeCallback(mPreview);
                mCamera.release();
                mCamera = null;
            }

        }

        private Runnable doAutoFocus = new Runnable() {
                public void run() {
                    if (previewing)
                        mCamera.autoFocus(autoFocusCB);
                }
            };

        PreviewCallback previewCb = new PreviewCallback() {
                public void onPreviewFrame(byte[] data, Camera camera) {
                    Camera.Parameters parameters = camera.getParameters();
                    Size size = parameters.getPreviewSize();

                    Image barcode = new Image(size.width, size.height, "Y800");
                    barcode.setData(data);

                    int result = scanner.scanImage(barcode);
                    String QRScannerResult;

                    if (result != 0) {
                        previewing = false;
                        mCamera.setPreviewCallback(null);
                        mCamera.stopPreview();

                        SymbolSet syms = scanner.getResults();
                        for (Symbol sym : syms) {
                            QRScannerResult = sym.getData();
                            showResultAction(QRScannerResult);
                            barcodeScanned = true;

                        }
                    }
                }
            };

        // Mimic continuous auto-focusing
        AutoFocusCallback autoFocusCB = new AutoFocusCallback() {
                public void onAutoFocus(boolean success, Camera camera) {
                    autoFocusHandler.postDelayed(doAutoFocus, 1000);
                }
            };

        private void showResultAction(String QRScannerResult){
                if (URLUtil.isValidUrl(QRScannerResult)) {

                    if (barcodeScanned) {
                        barcodeScanned = false;
                        mCamera.setPreviewCallback(previewCb);
                        mCamera.startPreview();
                        previewing = true;
                        mCamera.autoFocus(autoFocusCB);
                    }

                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(QRScannerResult));
                    startActivity(i);

                    }else {
                        new AlertDialog.Builder(this)
                        .setTitle("QR Data")
                        .setMessage(QRScannerResult)
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) { 
                                if (barcodeScanned) {
                                    barcodeScanned = false;
                                    mCamera.setPreviewCallback(previewCb);
                                    mCamera.startPreview();
                                    previewing = true;
                                    mCamera.autoFocus(autoFocusCB);
                                }
                            }
                         })
                         .show();
                    }

        }
    }

提前致谢。萨基布

4

1 回答 1

10

最后我成功地找出了我的代码的问题并得到了解决方案。当我尝试以我的onResume()方法重新打开相机时,我错过了一部分。当我在 中创建/打开一个新相机时onResume(),FrameLayout 仍然有我以前的相机。所以我所做的就是从 FrameLayout ononPause()方法中删除我以前的相机,然后在 onResume() 方法上重新创建所有内容。这解决了我的问题,现在它运行良好,没有任何错误。这是我onPause()onResume()修复方法。希望这可能对将来的人有所帮助。

    public void onPause() {
        super.onPause();
        releaseCamera();
        FrameLayout preview = (FrameLayout)findViewById(R.id.cameraPreview);
        preview.removeView(mPreview);
    }

    public void onResume(){
        super.onResume();

        try {
            if(mCamera==null){

            //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            autoFocusHandler = new Handler();
            mCamera = getCameraInstance();
            this.getWindowManager().getDefaultDisplay().getRotation();

            scanner = new ImageScanner();
            scanner.setConfig(0, Config.X_DENSITY, 3);
            scanner.setConfig(0, Config.Y_DENSITY, 3);

            mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB);
            FrameLayout preview = (FrameLayout)findViewById(R.id.cameraPreview);
            preview.addView(mPreview);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block

        }
    }

    /** A safe way to get an instance of the Camera object. */
    public static Camera getCameraInstance(){
        Camera c = null;
        try {
            c = Camera.open();
        } catch (Exception e){
        }
        return c;
    }

    private void releaseCamera() {
        //Toast.makeText(QRScannerActivity.this, "Paused State", Toast.LENGTH_SHORT).show();
        if (mCamera != null) {
            previewing = false;
            mCamera.setPreviewCallback(null);
            mPreview.getHolder().removeCallback(mPreview);
            mCamera.release();
            mCamera = null;
            mPreview= null;
        }

    }

谢谢,萨基布

于 2013-07-24T07:59:04.630 回答