当前位置:操作系统 > 安卓/Android >>

Android 基于google Zxing实现对手机中的二维码进行扫描

我之前写了一篇关于google Zxing扫描二维码的文章,效果是仿微信的效果,有兴趣的朋友可以去看看基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果,有时候我们有这样子的需求,需要扫描手机中有二维码的的图片,所以今天实现的就是对手机中的二维码图片进行扫描,我这里是直接在原来的工程上面加的这个功能,下面就简单介绍下这个小功能的实现,首先我在界面上加了一个ImageButton,图片还是用的微信的图片,下面是扫描界面的title
[html]  
<?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从手机中选择图片
[java]  
//打开手机中的相册  
            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方法里面解析图片中的二维码
[java]  
@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);  
             
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,