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;
	}
	
}


转载于:https://my.oschina.net/u/2611678/blog/604147

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值