首页 技术 正文
技术 2022年11月15日
0 收藏 755 点赞 4,290 浏览 4445 个字

原文网址:http://blog.csdn.net/xiaanming/article/details/14450809

转载请注明出处:http://blog.csdn.net/xiaanming/article/details/14450809

我之前写了一篇关于google Zxing扫描二维码的文章,效果是仿微信的效果,有兴趣的朋友可以去看看基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果,有时候我们有这样子的需求,需要扫描手机中有二维码的的图片,所以今天实现的就是对手机中的二维码图片进行扫描,我这里是直接在原来的工程上面加的这个功能,下面就简单介绍下这个小功能的实现,首先我在界面上加了一个ImageButton,图片还是用的微信的图片,下面是扫描界面的title

  1. <?xml version=”1.0″ encoding=”utf-8″?>
  2. <RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
  3. android:layout_width=”fill_parent”
  4. android:layout_height=”wrap_content”
  5. android:background=”@drawable/mmtitle_bg_alpha” >
  6. <Button
  7. android:id=”@+id/button_back”
  8. android:layout_width=”75.0dip”
  9. android:layout_height=”wrap_content”
  10. android:layout_alignParentLeft=”true”
  11. android:background=”@drawable/mm_title_back_btn”
  12. android:text=”返回”
  13. android:textColor=”@android:color/white” />
  14. <TextView
  15. android:id=”@+id/textview_title”
  16. android:layout_width=”wrap_content”
  17. android:layout_height=”wrap_content”
  18. android:layout_centerHorizontal=”true”
  19. android:layout_centerVertical=”true”
  20. android:gravity=”center_vertical”
  21. android:text=”二维码扫描”
  22. android:textColor=”@android:color/white”
  23. android:textSize=”18sp” />
  24. <ImageButton
  25. android:id=”@+id/button_function”
  26. android:layout_width=”wrap_content”
  27. android:layout_height=”wrap_content”
  28. android:layout_alignParentRight=”true”
  29. android:layout_marginRight=”2dip”
  30. android:background=”@drawable/mm_title_right_btn”
  31. android:minWidth=”70dip”
  32. android:src=”@drawable/mm_title_btn_menu_normal” />
  33. </RelativeLayout>

在扫描界面MipcaActivityCapture对ImageButton对其点击监听,点击ImageButton从手机中选择图片

  1. //打开手机中的相册
  2. Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT); //”android.intent.action.GET_CONTENT”
  3. innerIntent.setType(“image/*”);
  4. Intent wrapperIntent = Intent.createChooser(innerIntent, “选择二维码图片”);
  5. this.startActivityForResult(wrapperIntent, REQUEST_CODE);

在这里使用了startActivityForResult来跳转界面,当我们选中含有二维码的图片的时候会回调MipcaActivityCapture的onActivityResult方法,我们需要在onActivityResult方法里面解析图片中的二维码

  1. @Override
  2. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  3. super.onActivityResult(requestCode, resultCode, data);
  4. if(resultCode == RESULT_OK){
  5. switch(requestCode){
  6. case REQUEST_CODE:
  7. //获取选中图片的路径
  8. Cursor cursor = getContentResolver().query(data.getData(), null, null, null, null);
  9. if (cursor.moveToFirst()) {
  10. photo_path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
  11. }
  12. cursor.close();
  13. mProgress = new ProgressDialog(MipcaActivityCapture.this);
  14. mProgress.setMessage(“正在扫描…”);
  15. mProgress.setCancelable(false);
  16. mProgress.show();
  17. new Thread(new Runnable() {
  18. @Override
  19. public void run() {
  20. Result result = scanningImage(photo_path);
  21. if (result != null) {
  22. Message m = mHandler.obtainMessage();
  23. m.what = PARSE_BARCODE_SUC;
  24. m.obj = result.getText();
  25. mHandler.sendMessage(m);
  26. } else {
  27. Message m = mHandler.obtainMessage();
  28. m.what = PARSE_BARCODE_FAIL;
  29. m.obj = “Scan failed!”;
  30. mHandler.sendMessage(m);
  31. }
  32. }
  33. }).start();
  34. break;
  35. }
  36. }
  37. }

我们先通过图片的Uri获取图片的路径,然后根据图片的路径扫描出图片里面的二维码内容,这将解码图片放在了一个子线程中,主要是防止因为解析太久而出现ARN的情况

接下来看scanningImage(String path) 方法,zxing.jar中提供了对二维码进行解析的类QRCodeReader.java,使用decode(BinaryBitmap image, Map<DecodeHintType, ?> hints)方法就能解析出图片里面的二维码信息,下面是通过图片的路径解析出里面的二维码内容

  1. /**
  2. * 扫描二维码图片的方法
  3. * @param path
  4. * @return
  5. */
  6. public Result scanningImage(String path) {
  7. if(TextUtils.isEmpty(path)){
  8. return null;
  9. }
  10. Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();
  11. hints.put(DecodeHintType.CHARACTER_SET, “UTF8”); //设置二维码内容的编码
  12. BitmapFactory.Options options = new BitmapFactory.Options();
  13. options.inJustDecodeBounds = true; // 先获取原大小
  14. scanBitmap = BitmapFactory.decodeFile(path, options);
  15. options.inJustDecodeBounds = false; // 获取新的大小
  16. int sampleSize = (int) (options.outHeight / (float) 200);
  17. if (sampleSize <= 0)
  18. sampleSize = 1;
  19. options.inSampleSize = sampleSize;
  20. scanBitmap = BitmapFactory.decodeFile(path, options);
  21. RGBLuminanceSource source = new RGBLuminanceSource(scanBitmap);
  22. BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
  23. QRCodeReader reader = new QRCodeReader();
  24. try {
  25. return reader.decode(bitmap1, hints);
  26. } catch (NotFoundException e) {
  27. e.printStackTrace();
  28. } catch (ChecksumException e) {
  29. e.printStackTrace();
  30. } catch (FormatException e) {
  31. e.printStackTrace();
  32. }
  33. return null;
  34. }

Result是封装了解码的条码图像内的结果,我们只需要通过Result的getText()方法就能取出里面的二维码内容,这样子我们就搞定了扫描手机中的二维码图片的小功能,接下来我们运行下项目,看看效果

【转】 Android 基于google Zxing实现对手机中的二维码进行扫描–不错【转】 Android 基于google Zxing实现对手机中的二维码进行扫描–不错【转】 Android 基于google Zxing实现对手机中的二维码进行扫描–不错

有疑问的朋友可以在下面留言,我会为大家解答,源码里是在之前的效果里面新添加的功能,有兴趣的朋友可以下载源码看看

项目源码,点击下载

很多朋友下了demo发现出现Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define Lcom/google/zxing/BarcodeFormat;这个错误,是因为刚开始的时候我放了两个JAR包进去,删除一个就行了,大家自行修改

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919