用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - android代码库

用zxing生成、解析二维码

2013-02-13 作者: 神马举报

[android]代码库

 package com.easyoa.test;
 import java.awt.image.BufferedImage;
 import java.io.File;
 import java.io.IOException;
 import java.util.Hashtable;
 import javax.imageio.ImageIO;
 import com.google.zxing.BarcodeFormat;
 import com.google.zxing.BinaryBitmap;
 import com.google.zxing.DecodeHintType;
 import com.google.zxing.LuminanceSource;
 import com.google.zxing.MultiFormatReader;
 import com.google.zxing.MultiFormatWriter;
 import com.google.zxing.Reader;
 import com.google.zxing.ReaderException;
 import com.google.zxing.Result;
 import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
 import com.google.zxing.common.ByteMatrix;
 import com.google.zxing.common.HybridBinarizer;
 public class Test {
    private static final int BLACK = 0xff000000;
    private static final int WHITE = 0xFFFFFFFF;
  /**
   * @param args
   */
  public static void main(String[] args) {
   Test test=new Test();
   test.encode();
  test.decode();
  }
  //------------------------------------------------------------------------------编码-------------------------------------------------
  /**
   * 在编码时需要将com.google.zxing.qrcode.encoder.Encoder.java中的
   *  static final String DEFAULT_BYTE_MODE_ENCODING = "ISO8859-1";修改为UTF-8,否则中文编译后解析不了
   */
  public void encode(){
   try { 
    String str = "姓名:张三,性别:男,年龄:25,籍贯:中国北京,";// 二维码内容 
    String path = "D://test.png"; 
    ByteMatrix byteMatrix; 
    byteMatrix= new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, 200, 200);
    File file = new File(path); 
       writeToFile(byteMatrix, "png", file); 
   } catch (Exception e) { 
    e.printStackTrace(); 
   }
  }
  public static void writeToFile(ByteMatrix matrix, String format, File file)
  throws IOException {
 BufferedImage image = toBufferedImage(matrix);
 ImageIO.write(image, format, file);
 }
  public static BufferedImage toBufferedImage(ByteMatrix matrix) {
      int width = matrix.getWidth();
      int height = matrix.getHeight();
      BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
      for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
          image.setRGB(x, y, matrix.get(x, y) == 0 ? BLACK:WHITE);
        }
      }
      return image;
    }
  //-------------------------------------------------------------------解码----------------------------------------------------
  public void decode(){
   try{
    Reader reader = new MultiFormatReader(); 
    String imgPath = "D://test.png"; 
    File file = new File(imgPath); 
    BufferedImage image; 
    try { 
     image = ImageIO.read(file); 
     if (image == null) { 
     System.out.println("Could not decode image"); 
     } 
     LuminanceSource source = new BufferedImageLuminanceSource(image); 
     BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); 
     Result result; 
     Hashtable hints= new Hashtable(); 
     hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); 
     //解码设置编码方式为:utf-8,
     result = new MultiFormatReader().decode(bitmap,hints); 
     String resultStr = result.getText(); 
     System.out.println("解析后内容:"+resultStr);
    } catch (IOException ioe) { 
     System.out.println(ioe.toString()); 
    } catch (ReaderException re) { 
     System.out.println(re.toString()); 
    }
   }catch(Exception ex){
    System.out.println(ex.toString());
   }
  }
 }


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...