项目集成zxing扫描二维码并跳转页面

来源:互联网 发布:哪个网游有mac版 编辑:程序博客网 时间:2024/06/01 07:24

1.把需要的activity放在src里面
主要是这些,编码和解码都有,我就都放进来了
2.权限和注册
zxing需要的权限,主要是开摄像头和震动
因为我没有用到编码,所以只有一个扫描的界面需要注册
3.放置各种资源文件,图片,声音,id,布局都放到相应的文件夹中就可以
4.放置jar包
就是这个
5.有个东西一开始没有导进来,结果打开摄像头时一直闪退
不太清楚这个东西的作用
6.最后需要修改一点点代码来实现页面跳转

因为zxing的demo默认是不跳转页面的,只把扫描结果解析出来显示一个地址,主要是在captureactivity.java中找到处理扫描结果的函数!记住要把fnish注释掉~ OK啦!测试成功~很开心

public void handleDecode(Result result, Bitmap barcode) {        inactivityTimer.onActivity();        playBeepSoundAndVibrate();        String resultString = result.getText();        //FIXME        if (resultString.equals("")) {            Toast.makeText(CaptureActivity.this, "Scan failed!", Toast.LENGTH_SHORT).show();        }else {//          System.out.println("Result:"+resultString);            Intent resultIntent = new Intent();            Bundle bundle = new Bundle();            bundle.putString("result", resultString);            resultIntent.putExtras(bundle);            this.setResult(RESULT_OK, resultIntent);            //网上查到的方法:扫描后直接简单跳转到页面            Pattern pattern=Pattern.compile("http://(([a-zA-z0-9]|-){1,}\\.){1,}[a-zA-z0-9]{1,}-*");            Matcher matcher=pattern.matcher(resultString);            if(matcher.find()){ //判断扫描的结果是否为url、是url直接跳转到网页中                                 Intent intent = new Intent();                           intent.setAction("android.intent.action.VIEW");                       Uri content_url = Uri.parse(resultString);                      intent.setData(content_url);                     startActivity(intent);            }else{//不是直接显示            //创建dialog对象              AlertDialog alertDialog = new AlertDialog.Builder(CaptureActivity.this).create();            alertDialog.setTitle("扫描结果");            //显示扫描的信息            alertDialog.setMessage(resultString);            //设置按钮            alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialog, int which) {            // TODO Auto-generated method stub            dialog.dismiss();            }            });             alertDialog.show();            }        }//      CaptureActivity.this.finish();    }

存在的一点小问题是,空指针异常加载不出布局文件,不过不影响项目运行,这个问题后面再研究吧
搜了一下解决方法,好麻烦

开发中参考的博客连接点这里,感谢!

0 0
原创粉丝点击