关闭

使用zxing 生成和解析二维码

标签: 二维码 zxing二维码
1570人阅读 评论(0) 收藏 举报
package cn.wyj;

import java.io.File;
import java.io.IOException;
import java.util.Hashtable;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

public class test
{

	/**
	 * @param args
	 * @throws WriterException 
	 * @throws IOException 
	 */
	public static void main(String[] args) throws WriterException, IOException
	{
		String text = "你们好好学习";
		int width = 200;
		int height = 200;
		String format = "png";
		Hashtable hints = new Hashtable();
		hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
		BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height,hints);  
		File outputFile = new File("d:\\new.bmp");
		MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);
	}

}
package cn.wyj;

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.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.ReaderException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;

public class Test2
{
	public static void main(String[] args)
	{
         String imgPath = "D:\\new.bmp";  
         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");  
             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());  
         }  

	}
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:60075次
    • 积分:1226
    • 等级:
    • 排名:千里之外
    • 原创:57篇
    • 转载:1篇
    • 译文:0篇
    • 评论:8条