android直接从图片中对二维码编码解码

转载 2012年03月29日 11:30:09
001 package com.google.zxing.client.android;
002   
003 import java.util.Hashtable;
004   
005 import android.app.Activity;
006 import android.graphics.Bitmap;
007 import android.graphics.drawable.BitmapDrawable;
008 import android.os.Bundle;
009 import android.util.Log;
010 import android.view.View;
011 import android.view.View.OnClickListener;
012 import android.widget.Button;
013 import android.widget.EditText;
014 import android.widget.ImageView;
015 import android.widget.TextView;
016   
017 import com.google.zxing.BarcodeFormat;
018 import com.google.zxing.BinaryBitmap;
019 import com.google.zxing.ChecksumException;
020 import com.google.zxing.EncodeHintType;
021 import com.google.zxing.FormatException;
022 import com.google.zxing.NotFoundException;
023 import com.google.zxing.Result;
024 import com.google.zxing.WriterException;
025 import com.google.zxing.common.BitMatrix;
026 import com.google.zxing.common.HybridBinarizer;
027 import com.google.zxing.qrcode.QRCodeReader;
028 import com.google.zxing.qrcode.QRCodeWriter;
029 import com.test.RGBLuminanceSource;
030   
031 public class TestActivity extends Activity {
032     private static final String TAG = "TestActivity";
033       
034     String path;
035     private final static String pFormat = "png";
036   
037           
038   
039     protected void onCreate(Bundle savedInstanceState) {
040         super.onCreate(savedInstanceState);
041         setContentView(R.layout.testactivity);
042         //set
043         Button btn2 = (Button)findViewById(R.id.Button03);
044         btn2.setOnClickListener(new OnClickListener() {
045               
046             @Override
047             public void onClick(View view) {
048                 ImageView imgView = (ImageView)findViewById(R.id.ImageView01);
049                 QRCodeReader reader = new QRCodeReader(); 
050                 int width = 200, height = 200;  
051                 QRCodeWriter writer = new QRCodeWriter();
052                 try {
053                     EditText edit = (EditText)findViewById(R.id.EditText01);
054                     //edit.getText();
055                     Log.i(TAG, "编辑框中的内容: " + edit.getText().toString());
056 //                  System.out.println(edit.getText().toString());
057                     if(edit.getText().toString().length()<1)
058                     {
059                         return;
060                     }
061                       
062                     BitMatrix martix = writer.encode(edit.getText().toString(), BarcodeFormat.QR_CODE, width, height);
063                     System.out.println("w:"+martix.width+"h:"+martix.height); 
064                     String imageFormat = "png"
065                     Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
066                     hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
067                 BitMatrix bitMatrix = new QRCodeWriter().encode(edit.getText().toString(),BarcodeFormat.QR_CODE, width, height,hints);
068                      int[] pixels = new int[width * height];    
069                      for (int y = 0; y < height; y++) {    
070                          for (int x = 0; x < width; x++) {    
071                              if(bitMatrix.get(x, y)){    
072                                  pixels[y * width + x] = 0xff000000;    
073                              }else{
074                                  pixels[y * width + x] = 0xffffffff;
075                              }    
076                                    
077                          }    
078                      }    
079                            
080                      Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);    
081                           
082                      bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
083                      imgView.setImageBitmap(bitmap);
084 //                   RGBLuminanceSource source = new RGBLuminanceSource(bitmap);
085 //                  LuminanceSource source = new RGBLuminanceSource(path);  
086 //                  BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));  
087 //                   QRCodeReader reader2= new QRCodeReader();
088 //                   Result result = reader2.decode(bitmap1);
089 //                   System.out.println("res"+result.getText());
090                        
091                 } catch (WriterException e) {
092                     // TODO Auto-generated catch block
093                     e.printStackTrace();
094                 }
095             }
096         });
097           
098         Button btn1 = (Button)findViewById(R.id.Button01);
099         btn1.setOnClickListener(new OnClickListener() {
100               
101             @Override
102             public void onClick(View v) {
103                 // TODO Auto-generated method stub
104                 ImageView imgView =(ImageView)findViewById(R.id.ImageView01);
105                 Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
106                 hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
107                 Bitmap bitmap = ((BitmapDrawable)imgView.getDrawable()).getBitmap();
108                 RGBLuminanceSource source = new RGBLuminanceSource(bitmap);
109 //                  LuminanceSource source = new RGBLuminanceSource(path);  
110                     BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));  
111                      QRCodeReader reader2= new QRCodeReader();
112                      Result result;
113                    try {
114                      result = reader2.decode(bitmap1,hints);
115                      System.out.println("res"+result.getText());
116                      TextView text  = (TextView)findViewById(R.id.TextView01);
117                     text.setText(result.getText());
118                     } catch (NotFoundException e) {
119                         // TODO Auto-generated catch block
120                         e.printStackTrace();
121                     } catch (ChecksumException e) {
122                         // TODO Auto-generated catch block
123                         e.printStackTrace();
124                     } catch (FormatException e) {
125                         // TODO Auto-generated catch block
126                         e.printStackTrace();
127                     }
128                     // System.out.println("res"+result.getText());
129             }
130         });
131     }
132 }

Android 解析二维码图片

我们项目之前已集成了一个zxing的扫描二维码开源库。现新增需求是将用户存储到相册中的二维码图片识别并解析出来。网上也有很多这样解析的例子,我这篇博客也是在借鉴他们提供的方法之后写出来的,整个解析过程...

Android之zxing二维码生成与识别

二维码: 是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的; 在代码编制上巧妙的利用构成计算机内部逻辑基础的0和1比特流的概念,使用若干个与二进制相对应的几何...

Android快速集成Zxing扫码库

今天为大家介绍的是我的github开源项目ZxingSimplify,一个精简的安卓Zxing扫码库。有了它你将分分钟集成扫码(包括二维码、条形码等)功能。 一.扫码界面 二.使用 1.Gradl...

ZXing 相册中识别二维码和条形码(直接引用就可以了)

*百度了很久一直没有找到关于相册获取条形码的Demo,真心是醉了,只能苦逼的去自己看 闲话不说直接搞起分析:*核心 1,通过路径转换成bitmp对象 2,再bitmap对象转换成二进制图片(二值...

Android大图片之缩略图,以及对原图按照指定宽高裁剪成缩略图

 《Android大图片之变换缩略图,以及对原始大图片按照指定宽、高裁剪成缩略图》 在Android的ImageView加载图像资源过程中,出于性能和内存开销的需要,有时候需要把一个原始的超大...

自定义View显示超大图片

上一节(自定义初学5——自定义View显示图片)已经说了如何自定义View显示图片。做android时,加载图片是避免不了的,加载网络图片还需要异步加载,最烦人的就是经常出现OOM,为了避免这样的问题...

二维码生成 图片Base64编码解码

  • 2011年04月21日 13:08
  • 1007KB
  • 下载

java 二维码编码解码

  • 2013年07月25日 09:50
  • 2MB
  • 下载

条形码和二维码编码解码

/** * 条形码和二维码编码解码 * * @author stronginfo * @version 2014-02-28 */ public class ZxingHandler { ...

二维码编码解码源代码

  • 2011年01月13日 15:58
  • 6.38MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:android直接从图片中对二维码编码解码
举报原因:
原因补充:

(最多只允许输入30个字)