ZXing生成二维码的工具类

**********引入使用ZXing须要的jar包:core-3.1.0.jar


package check.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;

/**
 * 将生成的二维码写入文件或者写入输出流的工具类
 */
public class MatrixToImageWriter 
{
	//定义黑色
	private static final int BLACK = 0xFF000000;
	
	//定义全白
	private static final int WHITE = 0xFFFFFFFF;
	
	/**
	 * 生成和text对应的二维码,并存入到filePath指定的路径文件中
	 * @param text 要生成二维码的内容文本    例如: http://www.baidu.com
	 * @param width 二维码图片的宽度  
	 * @param height 二维码图片的高度
	 * @param format  二维码图片的格式      例如: jpg,jpeg,png...
	 * @param fileName  文件名的全路径,后缀必须和format保持一致      例如:"D:\\zcl.jpg"
	 * @throws WriterException 
	 * @throws IOException 
	 */
	public static void generateBarCode2File(String text,int width,int height,String format,String fileName) 
			throws WriterException, IOException
	{
		if (!fileName.endsWith(format))
		{
			throw new RuntimeException(fileName + "文件名后缀和" + format + "不一致");
		}
		
		Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
		hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
		BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
		
		File file = new File(fileName);
		writeToFile(bitMatrix, format, file);
	}
	
	/**
	 * 生成和text对应的二维码,并用输出流输出
	 * @param text 要生成二维码的内容文本    例如: http://www.baidu.com
	 * @param width 二维码图片的宽度  
	 * @param height 二维码图片的高度
	 * @param format  二维码图片的格式      例如: jpg,jpeg,png...
	 * @param str 输出流
	 * @throws WriterException 
	 * @throws IOException 
	 */
	public static void generateBarCode2Stream(String text,int width,int height,String format,OutputStream str) 
			throws WriterException, IOException
	{
		Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
		hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
		BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
		
		writeToStream(bitMatrix,format,str);
	}
	
	/**
	 * 将二维码图片写入到对应的文件中
	 * @param matrix 二维码生成主类
	 * @param format  文件格式
	 * @param file 文件全路径名
	 * @throws IOException 
	 */
	private static void writeToFile(BitMatrix matrix,String format,File file) throws IOException
	{
		BufferedImage image = toBufferedImage(matrix);
		if (!ImageIO.write(image, format, file))
		{
			throw new IOException("Could not write an image of format " + format + " to " + file);
		}
	}
	
	/**
	 * 将二维码图案写入到输出流中
	 * @param matrix matrix 二维码生成主类
	 * @param format 文件格式
	 * @param str 构建的输出流
	 * @throws IOException 
	 */
	private static void writeToStream(BitMatrix matrix,String format,OutputStream str) throws IOException
	{
		BufferedImage image = toBufferedImage(matrix);
		if (!ImageIO.write(image, format, str));
		{
			throw new IOException("Could not write an image of format " + format + " to " + str);
		}
	}
	
	/**
	 * 
	 * 生成二维码
	 * @param matrix
	 * @return
	 */
	private static BufferedImage toBufferedImage(BitMatrix matrix)
	{
		int width = matrix.getWidth();
		int height = matrix.getHeight();
		BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
		for (int x = 0; x < width; x++)
		{
			for (int y = 0; y < height; y++)
			{
				image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
			}
		}
		return image;
	}
	
}
相关文章
相关标签/搜索