1、开源项目地址:
2、jar包下载地址: 可以选择版本号,然后下载。
3、导入demo: 导入android 文件夹即可。
4、网上精简版demo:
效果图:
caution:
手机横竖屏问题
位置: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 (""); 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.扫描灵敏度问题
距离二维码多远能扫描出来?
多长时间能扫出来?