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

有时候我们有这样子的需求,需要扫描手机中有二维码的的图片,所以今天实现的就是对手机中的二维码图片进行扫描,我这里是直接在原来的工程上面加的这个功能,下面就简单介绍下这个小功能的实现,首先我在界面上加了一个ImageButton,图片还是用的微信的图片,下面是扫描界面的title

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/mmtitle_bg_alpha" > <Button
android:id="@+id/button_back"
android:layout_width="75.0dip"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="@drawable/mm_title_back_btn"
android:text="返回"
android:textColor="@android:color/white" /> <TextView
android:id="@+id/textview_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:text="二维码扫描"
android:textColor="@android:color/white"
android:textSize="18sp" /> <ImageButton
android:id="@+id/button_function"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="2dip"
android:background="@drawable/mm_title_right_btn"
android:minWidth="70dip"
android:src="@drawable/mm_title_btn_menu_normal" /> </RelativeLayout>

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

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

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

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
switch(requestCode){
case REQUEST_CODE:
//获取选中图片的路径
Cursor cursor = getContentResolver().query(data.getData(), null, null, null, null);
if (cursor.moveToFirst()) {
photo_path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
}
cursor.close(); mProgress = new ProgressDialog(MipcaActivityCapture.this);
mProgress.setMessage("正在扫描...");
mProgress.setCancelable(false);
mProgress.show(); new Thread(new Runnable() {
@Override
public void run() {
Result result = scanningImage(photo_path);
if (result != null) {
Message m = mHandler.obtainMessage();
m.what = PARSE_BARCODE_SUC;
m.obj = result.getText();
mHandler.sendMessage(m);
} else {
Message m = mHandler.obtainMessage();
m.what = PARSE_BARCODE_FAIL;
m.obj = "Scan failed!";
mHandler.sendMessage(m);
} }
}).start(); break; }
}
}

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

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

    /**
* 扫描二维码图片的方法
* @param path
* @return
*/
public Result scanningImage(String path) {
if(TextUtils.isEmpty(path)){
return null;
}
Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();
hints.put(DecodeHintType.CHARACTER_SET, "UTF8"); //设置二维码内容的编码 BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; // 先获取原大小
scanBitmap = BitmapFactory.decodeFile(path, options);
options.inJustDecodeBounds = false; // 获取新的大小
int sampleSize = (int) (options.outHeight / (float) 200);
if (sampleSize <= 0)
sampleSize = 1;
options.inSampleSize = sampleSize;
scanBitmap = BitmapFactory.decodeFile(path, options);
RGBLuminanceSource source = new RGBLuminanceSource(scanBitmap);
BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
QRCodeReader reader = new QRCodeReader();
try {
return reader.decode(bitmap1, hints); } catch (NotFoundException e) {
e.printStackTrace();
} catch (ChecksumException e) {
e.printStackTrace();
} catch (FormatException e) {
e.printStackTrace();
}
return null;
}

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

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

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

源码下载

Android 基于google Zxing实现对手机中的二维码进行扫描的更多相关文章

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

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

  2. C#+ZXing.dll生成手机路径导航二维码

    1.原谅我先写点废话哈 这两天用C#写一个C端的软件,甲方提出一个很无理的需求(在C端的程序中实现路径导航,关键是这个程序最终是运行在物理隔绝的电脑上的……),头疼了好几天,领导突然想到可以把坐标+百 ...

  3. Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果

      Android 高手进阶(21)  版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明出处:http://blog.csdn.net/xiaanming/article/detail ...

  4. 【转】Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果--不错

    原文网址:http://blog.csdn.net/xiaanming/article/details/10163203 转载请注明出处:http://blog.csdn.net/xiaanming/ ...

  5. Android高级控件(三)—— 使用Google ZXing实现二维码的扫描和生成相关功能体系

    Android高级控件(三)-- 使用Google ZXing实现二维码的扫描和生成相关功能体系 摘要 现在的二维码可谓是烂大街了,到处都是二维码,什么都是二维码,扫一扫似乎已经流行到习以为常了,今天 ...

  6. Android高级控件(三)——&#160;使用Google ZXing实现二维码的扫描和生成相关功能体系

    Android高级控件(三)-- 使用Google ZXing实现二维码的扫描和生成相关功能体系 摘要 如今的二维码可谓是烂大街了.到处都是二维码.什么都是二维码,扫一扫似乎已经流行到习以为常了,今天 ...

  7. 【转】Android手机客户端关于二维码扫描的源码--不错

    原文网址:https://github.com/SkillCollege/QrCodeScan QrCodeScan 这是Android手机客户端关于二维码扫描的源码,使用了高效的ZBar解码库,并修 ...

  8. 基于zxing的二维码(网格)扫描

    基于zxing的二维码(网格)扫描 前言:对于二维码扫描我们使用的是开源框架Zxing或者Zbar,这里使用基于zxing的二维码扫描,类似支付宝网格扫描, 二维码原理介绍: 二维码是用某种特定的几何 ...

  9. android 使用开源库zxing生成二维码,扫描二维码【转】

    转自:http://blog.csdn.net/qq_16064871/article/details/52422723 zxing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库 ...

随机推荐

  1. (转)Android强制设置横屏或竖屏

    全屏 在Activity的onCreate方法中的setContentView(myview)调用之前添加下面代码 requestWindowFeature(Window.FEATURE_NO_TIT ...

  2. HTTP-POST

    POST方式:用来向目的服务器发出请求,要求它接受被附在请求后的实体,并把它当作请求队列中请求URI所指定资源的附加新子项,Post被设计成用统一的方法实现下列功能: 1:对现有资源的解释: 2:向电 ...

  3. MinGW下载并配置gcc/g++编译环境

    本文将讲解如何下载MinGW并配置gcc\g++编译环境 一.下载MinGW 在MinGW官网中下载“mingw-get-setup.exe” 官网传送门:http://www.mingw.org/  ...

  4. PHP字符串的处理(四)-HTML标签的字符串格式化

    和html标签相关的字符串格式化 nl2br()  //在字符串中每个新行"\n"之前插入html换行符"<br />" <?php echo ...

  5. Patator-一款很好用的爆破工具

    项目地址:https://github.com/lanjelot/patator 打开文件夹 运行一下文件查看帮助 python patator.py --help 这里有很多的爆破选项,就不一一截图 ...

  6. Python Twisted系列教程12:改进诗歌下载服务器

    作者:dave@http://krondo.com/a-poetry-transformation-server/  译者:杨晓伟(采用意译) 你可以从这里从头阅读这个系列. 新的服务器实现 这里我们 ...

  7. PCA主成分分析 ICA独立成分分析 LDA线性判别分析 SVD性质

    机器学习(8) -- 降维 核心思想:将数据沿方差最大方向投影,数据更易于区分 简而言之:PCA算法其表现形式是降维,同时也是一种特征融合算法. 对于正交属性空间(对2维空间即为直角坐标系)中的样本点 ...

  8. leetcode690

    class Solution { public: int getImportance(vector<Employee*> employees, int id) { ; map<int ...

  9. Python:cmd传参

    假如你写了一个文件test.py,你需要三个参数,你运行时: python test.py arg1 arg2 arg3 在test.py中读取这几个参数: import sys print 'Num ...

  10. Log4php使用指南

      一.Log4php简介 Log4php是Log4xx系列日志组件之一,是Log4j迁移到php的版本,主要用来记录日志信息,支持多种输入目的地,包括:日志文件.日志回滚文件.数据库.日志服务器等等 ...