二维码 整理 以及豆瓣读…

1.二维码项目介绍

1.1Zxing   http://code.google.com/p/zxing/

这个就不用多说了.  应该说所有Android条形码应用的鼻祖吧.  

缺点:之前一个项目就是整合了没修改过的.   最大的确定应该就是不能竖屏吧.  还有就是如果你直接整合官方的源码 会有很多要精简的地方,  不过不说,这个项目写的真的很详细...


1.2.在EOE看到的,刚跑了一下,精简过,竖屏,够省事.所以就用这个开始整合进我的小项目吧.

         http://www.eoeandroid.com/forum.php?mod=viewthread&tid=167006

补充1:刚又试了一下,如果要显示扫描后的图片话,这个还是不理想的.还是由于竖屏的问题.  其实也中和了几家的做法,快拍(不知道是那个版本,我很不喜欢,设计的花花的好像故弄玄虚貌似高端一样,哈哈  我还是比较喜欢QR Droid  都简单.   这两家都是直接调到结果页.不在显示扫描后的图片,倒也省事了.)

补充2:看有网友评论说这个效率会低一些,说实话我是没仔细看,不知道到底影响有多大,其实如果太大,那就换回那个横屏的就是了.


1.3 在线条形码的网站:  http://barcode.tec-it.com/  我随便找的... 你可以作为测试的时候使用.


2.二维码(QRCode)及一维码(条形码)的科普.

(以前只是为了整合应用,需求也只是能扫就行,对,结果我交差也是能用就行....  这也许也是很多程序员最后都成不了大牛的一个原因吧,呵呵,猜测一下.)

刚才在使用豆瓣购书单(Android版),发现它不识别二维码,商品的条形码也不识别.以前真的没注意这个问题,还觉得,ZXing挺牛逼,什么都识别挺好.   (刚才又看了一下,靠,我高看豆瓣的程序了...原来他们也是在扫描后判断是不是ISBN...问题一下变得超级简单了.)

ISBN码是EAN_13的子集. 只是ISBN是以 978或979而已.

这个是ZXing  BarcodeFormat 下支持的种类.

  1.   
  2. public static final BarcodeFormat QR_CODE new BarcodeFormat("QR_CODE");  
  3.   
  4.   
  5. public static final BarcodeFormat DATA_MATRIX new BarcodeFormat("DATA_MATRIX");  
  6.   
  7.   
  8. public static final BarcodeFormat UPC_E new BarcodeFormat("UPC_E");  
  9.   
  10.   
  11. public static final BarcodeFormat UPC_A new BarcodeFormat("UPC_A");  
  12.   
  13.   
  14. public static final BarcodeFormat EAN_8 new BarcodeFormat("EAN_8");  
  15.   
  16.   
  17. public static final BarcodeFormat EAN_13 new BarcodeFormat("EAN_13");  
  18.   
  19.   
  20. public static final BarcodeFormat UPC_EAN_EXTENSION new BarcodeFormat("UPC_EAN_EXTENSION");  
  21.   
  22.   
  23. public static final BarcodeFormat CODE_128 new BarcodeFormat("CODE_128");  
  24.   
  25.   
  26. public static final BarcodeFormat CODE_39 new BarcodeFormat("CODE_39");  
  27.   
  28.   
  29. public static final BarcodeFormat CODE_93 new BarcodeFormat("CODE_93");  
  30.   
  31.   
  32. public static final BarcodeFormat CODABAR new BarcodeFormat("CODABAR");  
  33.   
  34.   
  35. public static final BarcodeFormat ITF new BarcodeFormat("ITF");  
  36.   
  37.   
  38. public static final BarcodeFormat RSS14 new BarcodeFormat("RSS14");  
  39.   
  40.   
  41. public static final BarcodeFormat PDF417 new BarcodeFormat("PDF417");  
  42.   
  43.   
  44. public static final BarcodeFormat RSS_EXPANDED new BarcodeFormat("RSS_EXPANDED");  
  
  public static final BarcodeFormat QR_CODE = new BarcodeFormat("QR_CODE");

  
  public static final BarcodeFormat DATA_MATRIX = new BarcodeFormat("DATA_MATRIX");

  
  public static final BarcodeFormat UPC_E = new BarcodeFormat("UPC_E");

  
  public static final BarcodeFormat UPC_A = new BarcodeFormat("UPC_A");

  
  public static final BarcodeFormat EAN_8 = new BarcodeFormat("EAN_8");

  
  public static final BarcodeFormat EAN_13 = new BarcodeFormat("EAN_13");

  
  public static final BarcodeFormat UPC_EAN_EXTENSION = new BarcodeFormat("UPC_EAN_EXTENSION");

  
  public static final BarcodeFormat CODE_128 = new BarcodeFormat("CODE_128");

  
  public static final BarcodeFormat CODE_39 = new BarcodeFormat("CODE_39");

  
  public static final BarcodeFormat CODE_93 = new BarcodeFormat("CODE_93");

  
  public static final BarcodeFormat CODABAR = new BarcodeFormat("CODABAR");

  
  public static final BarcodeFormat ITF = new BarcodeFormat("ITF");

  
  public static final BarcodeFormat RSS14 = new BarcodeFormat("RSS14");

  
  public static final BarcodeFormat PDF417 = new BarcodeFormat("PDF417");

  
  public static final BarcodeFormat RSS_EXPANDED = new BarcodeFormat("RSS_EXPANDED");

2.1 ISBN条形码.  这个是我的重点嘛 .   ISBN有新旧,这个 我就不在学习啦...

新的国际标准书号在国际上简称“ISBN-13”。而前缀码979将作为ISBN-13备用的新系列号码   

百科里看了半天,应该就是靠这句话识别条形码的区别的吧.    

EAN-13 是要扫描的格式.


3.相关类(看到哪,记哪.能力有限,勿喷,哈哈)

3.1  ViewfinderView  

扫描界面  那个小黑框,扫描的激光线,小黄点点等

3.2  BarcodeFormat 

扫描图案的类型. 二维码/一维码 就像豆瓣读书单一样,我只需要扫描EAN_13就好,不需要做的大而全.

3.3  DecodeThread,DecodeHandler 

解码过程的核心类.

3.4 ParsedResult...  这个是一堆解析类

根据二维码的Result解析相应的结果:ISBN/Tel/Email/Url 等 做人性化的话,就要多区分这些动作就是了.


4.实现方法

4.1 豆瓣书单的实现方式  应该就是直接限制为EAN_13格式,然后当得到扫描结果后

 通过 ISBNResultParser.parse(lastResult) == null 判断是不是ISBN码,如果是跳转到搜索图书页.如果不是, 则提示该条形码不是图书ISBN码.

4.2 一个小小的疑问.  刚才试了一下, com.google.zxing.client.android.DecodeHandler.decode 方法里 添加这段代码.  当解析的结果不是ISBN的时候发送一个解码失败的Message.这样就可以让程序继续运行.排除了扫描的非ISBN的EAN_13提示的那个界面.

 因为你已经禁止扫描其他码,那干脆所有错误都排除就好了.

  1.    if(ISBNResultParser.parse(rawResult) == null){  
  2.         Message message Message.obtain(activity.getHandler(), R.id.decode_failed);  
  3.         message.sendToTarget();  
  4.     return  
  5.  
     if(ISBNResultParser.parse(rawResult) == null){
          Message message = Message.obtain(activity.getHandler(), R.id.decode_failed);
          message.sendToTarget();
                        return ;
                }

5.通过生成图片和解码图片

5.1 EnCode

  1.     public static Bitmap Encode(String str, BarcodeFormat barcodeFormat, int Width,  
  2.             int Height) throws WriterException  
  3.         Hashtable hints new Hashtable();  
  4.         // 指定纠错等级   
  5. //      hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);   
  6.         // 指定编码格式   
  7.         hints.put(EncodeHintType.CHARACTER_SET, HTTP.UTF_8);  
  8.         // 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败   
  9.         BitMatrix matrix new MultiFormatWriter().encode(str, barcodeFormat,  
  10.                 Width, Height, hints);  
  11.         int width matrix.getWidth();  
  12.         int height matrix.getHeight();  
  13.         // 二维矩阵转为一维像素数组,也就是一直横着排了   
  14.         int[] pixels new int[width height];      
  15.         for (int 0; height; y++)      
  16.             for (int 0; width; x++)      
  17.                 if(matrix.get(x, y)){      
  18.                     pixels[y width x] 0xff000000;      
  19.                 }else{  
  20.                  pixels[y width x] 0xffffffff;  
  21.                      
  22.                       
  23.                  
  24.            
  25.         Bitmap bitmap Bitmap.createBitmap(width, height,  
  26.                 Bitmap.Config.ARGB_8888);  
  27.         // 通过像素数组生成bitmap,具体参考api   
  28.         bitmap.setPixels(pixels, 0, width, 0, 0, width, height);  
  29.         return bitmap;  
  30.      
   public static Bitmap Encode(String str, BarcodeFormat barcodeFormat, int Width,
                        int Height) throws WriterException {
                Hashtable hints = new Hashtable (); // 指定纠错等级 // hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // 指定编码格式 hints.put(EncodeHintType.CHARACTER_SET, HTTP.UTF_8); // 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败 BitMatrix matrix = new MultiFormatWriter().encode(str, barcodeFormat, Width, Height, hints); int width = matrix.getWidth(); int height = matrix.getHeight(); // 二维矩阵转为一维像素数组,也就是一直横着排了 int[] pixels = new int[width * height]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if(matrix.get(x, y)){ pixels[y * width + x] = 0xff000000; }else{ pixels[y * width + x] = 0xffffffff; } } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); // 通过像素数组生成bitmap,具体参考api bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; }
5.2 DeCode

RGBLuminanceSource 这个类好像不在ZXing Android项目里.   http://code.google.com/p/zxing/source/browse/trunk/androidtest/src/com/google/zxing/client/androidtest/RGBLuminanceSource.java?r=1344 拷贝复制一下就好了.  

  1. public static Result DeCode(Bitmap bitmap){  
  2.     Hashtable hints new Hashtable();  
  3.     hints.put(EncodeHintType.CHARACTER_SET, HTTP.UTF_8);  
  4.   
  5.        RGBLuminanceSource source new RGBLuminanceSource(bitmap);  
  6.                 BinaryBitmap bitmap1 new BinaryBitmap(new HybridBinarizer(source));   
  7.                  QRCodeReader reader2= new QRCodeReader();  
  8.                  Result result;  
  9.                try  
  10.                  result reader2.decode(bitmap1,hints);  
  11.                  return result  
  12.                 catch (NotFoundException e)  
  13.                     e.printStackTrace();  
  14.                     return null  
  15.                 catch (ChecksumException e)  
  16.                     e.printStackTrace();  
  17.                     return null  
  18.                 catch (FormatException e)  
  19.                     e.printStackTrace();  
  20.                     return null  
  21.                  
  22.  
   public static Result DeCode(Bitmap bitmap){
                Hashtable hints = new Hashtable();
                hints.put(EncodeHintType.CHARACTER_SET, HTTP.UTF_8);

        RGBLuminanceSource source = new RGBLuminanceSource(bitmap);
                 BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source)); 
                  QRCodeReader reader2= new QRCodeReader();
                  Result result;
                try {
                  result = reader2.decode(bitmap1,hints);
                  return result ;
                 } catch (NotFoundException e) {
                     e.printStackTrace();
                     return null ;
                 } catch (ChecksumException e) {
                     e.printStackTrace();
                     return null ;
                 } catch (FormatException e) {
                     e.printStackTrace();
                     return null ;
                 }
        }


5.3 这里又出现了一个国人很悲剧的问题...  编码...  刚才也是在网站看到网友写才发现,有些用的是GBK,我是习惯用UTF-8.  这个应该只能看需要,或者多着编码选择设置了那。  


OK,二维码工作应该就这样结束了.   拼一个简单的二维码应用完全木有问题啦.



 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值