带有ZBar库的QR码阅读器出现错误ActivityNotFound

时间:2013-11-12 08:27:26

标签: android qr-code zbar-sdk

带有ZBar库的QR码阅读器在Intent ActivityNotFound上出错并崩溃。我改为Intent.ACTION_WEB_SEARCH但没有结果。错误没有显示在所有qr代码上,这就是为什么我无法理解什么是错误的。

public class MainActivity extends Activity implements OnClickListener {

    private boolean isLighOn = false;
    private Camera mCamera;
    private CameraPreview mPreview;
    private Handler autoFocusHandler;

    TextView scanText;
    Button scanButton, flash;
    String bar;
    ImageScanner scanner;

    private boolean previewing = true;

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

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.activity_main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        autoFocusHandler = new Handler();
        mCamera = getCameraInstance();
        flash=(Button)findViewById(R.id.flash);
        flash.setOnClickListener(this);
        /* 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);

        scanText = (TextView) findViewById(R.id.scanText);

    }

    public void onPause() {
        super.onPause();
        // releaseCamera();
    }

    /** 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() { if (mCamera != null) { previewing = false;
     * mCamera.setPreviewCallback(null); 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);
            if (result != 0) {
                previewing = false;
                mCamera.setPreviewCallback(null);
                mCamera.stopPreview();
                SymbolSet syms = scanner.getResults();
                for (Symbol sym : syms) {
                    scanText.setText(sym.getData());

                    bar = scanText.getText().toString();
                    try {
                        Intent inte = new Intent(Intent.ACTION_VIEW, Uri.parse(bar));

                        startActivity(inte);


                    } catch (ActivityNotFoundException e) {
                        e.printStackTrace();
                    }
                    scanText.setVisibility(View.INVISIBLE);
                }
            }
        }

    };

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        getCameraInstance();
        scanText.setText("Scaning...");
        scanText.setVisibility(View.VISIBLE);
    }

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

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        // TODO Auto-generated method stub
        super.startActivityForResult(intent, requestCode);
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Parameters p = mCamera.getParameters();
        /*p.setFlashMode(Parameters.FLASH_MODE_TORCH);
        mCamera.setParameters(p);
        p.setFlashMode(Parameters.FLASH_MODE_OFF);
        mCamera.setParameters(p);*/
        if (isLighOn) { 
            Log.i("info", "torch is turn off!");
            p.setFlashMode(Parameters.FLASH_MODE_OFF);
            mCamera.setParameters(p);
            isLighOn = false;
        } else {
            Log.i("info", "torch is turn on!");
            p.setFlashMode(Parameters.FLASH_MODE_TORCH);
            mCamera.setParameters(p);
            isLighOn = true;

        }

    }

}

和LogCat

11-12 12:09:05.484: W/System.err(4746): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=5949033011551 }
11-12 12:09:05.484: W/System.err(4746):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1580)
11-12 12:09:05.489: W/System.err(4746):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1431)
11-12 12:09:05.489: W/System.err(4746):     at android.app.Activity.startActivityForResult(Activity.java:3446)
11-12 12:09:05.489: W/System.err(4746):     at android.app.Activity.startActivityForResult(Activity.java:3407)
11-12 12:09:05.489: W/System.err(4746):     at com.example.qrcodereader.MainActivity.startActivityForResult(MainActivity.java:148)
11-12 12:09:05.489: W/System.err(4746):     at android.app.Activity.startActivity(Activity.java:3617)
11-12 12:09:05.489: W/System.err(4746):     at android.app.Activity.startActivity(Activity.java:3585)
11-12 12:09:05.494: W/System.err(4746):     at com.example.qrcodereader.MainActivity$2.onPreviewFrame(MainActivity.java:116)
11-12 12:09:05.499: W/System.err(4746):     at android.hardware.Camera$EventHandler.handleMessage(Camera.java:841)
11-12 12:09:05.499: W/System.err(4746):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-12 12:09:05.499: W/System.err(4746):     at android.os.Looper.loop(Looper.java:137)
11-12 12:09:05.504: W/System.err(4746):     at android.app.ActivityThread.main(ActivityThread.java:4921)
11-12 12:09:05.504: W/System.err(4746):     at java.lang.reflect.Method.invokeNative(Native Method)
11-12 12:09:05.504: W/System.err(4746):     at java.lang.reflect.Method.invoke(Method.java:511)
11-12 12:09:05.509: W/System.err(4746):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
11-12 12:09:05.509: W/System.err(4746):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
11-12 12:09:05.509: W/System.err(4746):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

我终于找到了问题。扫描的代码并不总是URL地址,因此意图不知道如何处理代码(我想是这样)。所以这就是答案:

    Intent inte = new Intent(Intent.ACTION_VIEW,
 Uri.parse("https://www.google.ro/search?q="+bar));
   startActivity(inte);