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;
	}
	
}
相關文章
相關標籤/搜索