Zbar与Android:扫描摄影机视口处于非活动状态和黑色显示的网址在浏览器后, [英] Zbar with Android : Scanner camera viewport remain inactive and black after showing the url in browser

查看:398
本文介绍了Zbar与Android:扫描摄影机视口处于非活动状态和黑色显示的网址在浏览器后,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的android开发。我需要有一个QR扫描仪在我的项目。我用ZBar做这个任务。主要是我做了由ZBar git的例子给做的工作的例子code一个非常小的变化。我需要显示扫描结果在浏览器(如URL)或在对话(如果正常信息)。

I am new in android development. I need to have a QR scanner in my project. I use ZBar for doing this task. Mainly I did a very small change on the example code given by ZBar git example to do the job. I need to show the scanned result in a browser(if url) or in a dialog(if normal information).

每一件事情与我,除非我尽量表现在URL后扫描QR code。在浏览器波纹管给code运作良好。当我回来从浏览器我的应用程序的摄影机视口变为黑色,并保持无效。我就先回去了相机主动扫描另一QR code,但我失败了。我试图重新打开摄像头在我的 onResume()方法,但导致的错误,并从应用程序强制退出。所以我注释掉code我曾尝试在 onResume()方法。请帮助我在这个问题上。

Every thing working well with my code given bellow except if I try to show the url after scan a qr code in browser. When I come back to my app from the browser the camera viewport turns black and remain inactive. I tried to get back the camera active to scan another qr code but i failed. I tried to reopen the camera in my onResume() method but that causes error and forcefully exited from the app. So I comment out the code I have tried inside onResume() method. Please help me on that issue.

        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();
            }

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

                /*try {
                    if(mCamera==null){

                    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;
                }

            }

            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();
                        }

            }
        }

在此先感谢。 Sakib

Thanks in advance. Sakib

推荐答案

最后,我成功地找到了我的code中的问题,并得到了解决。当我试图重新打开摄像头在我的 onResume()方法,我错过一个组成部分。当我创建/开启在 onResume一个新的相机()的的FrameLayout仍然有我的previous相机。所以,我所要做的就是去除的onPause从的FrameLayout我的previous摄像头()方法,然后重新创建一切的onResume()方法。这解决我的问题,现在它工作很好,没有任何错误。这是我的的onPause() onResume()法修复。希望这可以帮助别人的未来。

At last I succeeded to find out the problem of my code and got the solution. When i try to reopen the camera in my onResume() method I missed a part. When I create/open a new camera in onResume(), the FrameLayout still has my previous camera. So All I do is remove my previous camera from FrameLayout on onPause() method and then recreate everything on onResume() method. That solve my problem and now it is working nicely without any error. Here is my onPause() and onResume() method with the fix. Hope this might help somebody in future.

    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;
        }

    }

谢谢, Sakib

Thanks, Sakib

这篇关于Zbar与Android:扫描摄影机视口处于非活动状态和黑色显示的网址在浏览器后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆