上一篇博客中介绍了条码的使用示例,这一篇继续介绍如何使用JAVA生成二维码。
package com.hq.util;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
/**
* 二维码创建,需添加jar包:zxing-core.jar
*
* @author jianggujin
*
*/
public class QRCreater
{
/**
* 生成二维码文件
*
* @param content
* 二维码内容
* @param size
* 二维码大小
* @param file
* 生成文件
* @throws IOException
* @throws WriterException
*/
public void write(String content, int size, File file) throws IOException,
WriterException
{
ImageIO.write(
toBufferedImage(new MultiFormatWriter().encode(content,
BarcodeFormat.QR_CODE, size, size), null, 0), "JPEG", file);
}
/**
* 生成二维码并写入指定输出流
*
* @param content
* 二维码内容
* @param size
* 二维码大小
* @param os
* 输出流
* @throws IOException
* @throws WriterException
*/
public void write(String content, int size, OutputStream os)
throws IOException, WriterException
{
ImageIO.write(
toBufferedImage(new MultiFormatWriter().encode(content,
BarcodeFormat.QR_CODE, size, size), null, 0), "JPEG", os);
}
/**
* 生成带图标的二维码文件
*
* @param content
* 二维码内容
* @param size
* 二维码大小
* @param icon
* 图标文件
* @param iconSize
* 图标大小
* @param file
* 生成文件
* @throws IOException
* @throws WriterException
*/
public void write(String content, int size, File icon, int iconSize,
File file) throws IOException, WriterException
{
ImageIO.write(
toBufferedImage(new MultiFormatWriter().encode(content,
BarcodeFormat.QR_CODE, size, size), icon, iconSize), "JPEG",
file);
}
/**
* 生成带图标的二维码并写入指定输出流
*
* @param content
* 二维码内容
* @param size
* 二维码大小
* @param icon
* 图标文件
* @param iconSize
* 图标大小
* @param os
* 输出流
* @throws IOException
* @throws WriterException
*/
public void write(String content, int size, File icon, int iconSize,
OutputStream os) throws IOException, WriterException
{
ImageIO.write(
toBufferedImage(new MultiFormatWriter().encode(content,
BarcodeFormat.QR_CODE, size, size), icon, iconSize), "JPEG",
os);
}
/**
* 创建二维码的BufferedImage图像
*
* @param content
* 二维码内容
* @param size
* 二维码大小
* @return image
* @throws IOException
* @throws WriterException
*/
public BufferedImage toBufferedImage(String content, int size)
throws IOException, WriterException
{
return toBufferedImage(new MultiFormatWriter().encode(content,
BarcodeFormat.QR_CODE, size, size), null, 0);
}
/**
* 创建带图标的二维码的BufferedImage图像
*
* @param content
* 二维码内容
* @param size
* 二维码大小
* @param icon
* 图标文件
* @param iconSize
* 图标大小
* @return image
* @throws IOException
* @throws WriterException
*/
public BufferedImage toBufferedImage(String content, int size, File icon,
int iconSize) throws IOException, WriterException
{
return toBufferedImage(new MultiFormatWriter().encode(content,
BarcodeFormat.QR_CODE, size, size), icon, iconSize);
}
/**
* 创建二维码的BufferedImage图像
*
* @param matrix
* @param icon
* 图标文件
* @param iconSize
* 图标尺寸
* @return image
* @throws IOException
*/
public BufferedImage toBufferedImage(BitMatrix matrix, File icon,
int iconSize) throws IOException
{
int color = 0xFF000000;
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) ? color : 0xFFFFFFFF);
}
}
Graphics2D graphics = image.createGraphics();
if (icon != null && icon.exists())
{
BufferedImage bufferedImage = ImageIO.read(icon);
if (iconSize <= 0)
{
iconSize = 50;
}
graphics.drawImage(bufferedImage, (image.getWidth() - iconSize) / 2,
(image.getHeight() - iconSize) / 2, iconSize, iconSize, null);
}
graphics.dispose();
image.flush();
return image;
}
}