Zxing扫描二维码

 
摘要
android Zxing扫描二维码 横竖屏 扫描灵敏度

1、开源项目地址:https://github.com/zxing/zxing

2、jar包下载地址:http://repo1.maven.org/maven2/com/google/zxing/core/ 可以选择版本号,然后下载。

3、导入demo:  导入android 文件夹即可。

4、网上精简版demo:  http://blog.csdn.net/xiaanming/article/details/10163203 

    http://www.cnblogs.com/dolphin0520/p/3355728.html 

效果图:

Zxing扫描二维码

Zxing扫描二维码

Zxing扫描二维码

Zxing扫描二维码

Zxing扫描二维码

caution:

  1. 手机横竖屏问题

    位置:CameraConfigurationManager.initFromCameraParameters

    修改代码:if (width < height) {
        camera.setDisplayOrientation(90);//添加的代码
       int temp = width;
       width = height;
       height = temp;
      }

    位置:CameraManager.getFramingRectInPreview

    修改代码: WindowManager manager = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE);
      Display display = manager.getDefaultDisplay();
      int width = display.getWidth();
      int height = display.getHeight();
      if (width<height) {
       System.out.println("竖屏");
       rect.left = rect.left * cameraResolution.y / screenResolution.x;
       rect.right = rect.right * cameraResolution.y / screenResolution.x;
       rect.top = rect.top * cameraResolution.x / screenResolution.y;
       rect.bottom = rect.bottom * cameraResolution.x / screenResolution.y;
      }
      if (width>height) {
       System.out.println("横屏");
       rect.left = rect.left * cameraResolution.x / screenResolution.x;
       rect.right = rect.right * cameraResolution.x / screenResolution.x;
       rect.top = rect.top * cameraResolution.y / screenResolution.y;
       rect.bottom = rect.bottom * cameraResolution.y / screenResolution.y;
      }

2.扫描结果对话框

   位置:CaptureActivity.handleDecode

  修改代码:if (!TextUtils.isEmpty(result)) {
   AlertDialog.Builder builder = new AlertDialog.Builder(this);
         builder.setMessage(result)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                     //请求网络,发送数据
                     new AsyncTask<String, Void, String>() {
                    @Override
                     protected String doInBackground(String... params) {
                          AccessToServer accessToServer=new AccessToServer  ("http://192.168.254.1:8080/ZxingWeb/sendScancode");
                         return   accessToServer.doPost(new String[]{"content"}, new String[]{result});
                     }
                       protected void onPostExecute(String result) {
                        //System.out.println(result);
                   };
             }.execute();
          //页面跳转打开网页
             Intent mIntent=new Intent(CaptureActivity.this,WebViewActivity.class);
            mIntent.putExtra("url", result);
             startActivity(mIntent);
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                    finish();              //这个地方非常关键,如果不finish()的话,第二次扫描可能要好长时间,或者扫不出来
                    }
                });
        builder.create().show();
          
   /*Intent intent = new Intent();
   intent.putExtra("scan_result", rawResult.getText());
   setResult(RESULT_OK, intent);*/
  } else {
   setResult(RESULT_CANCELED);
  }
  // finish();           //记得要注释掉

3.扫描灵敏度问题

  距离二维码多远能扫描出来?

  多长时间能扫出来?

相关推荐