org.glamey.scaffold.chart.qrcode.QRCodeUtils Maven / Gradle / Ivy
The newest version!
package org.glamey.scaffold.chart.qrcode;
import com.google.common.base.Charsets;
import com.google.common.collect.Maps;
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
/**
* 二维码生成器
*
* @author zhouyang.zhou
*/
public final class QRCodeUtils {
private static final Logger logger = LoggerFactory.getLogger(QRCodeUtils.class);
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
private QRCodeUtils() {
}
public 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;
}
public static BitMatrix toBitMatrix(String text, Integer width, Integer height) {
if (width == null || width < 300) {
width = 300;
}
if (height == null || height < 300) {
height = 300;
}
Map hints = Maps.newHashMap();
hints.put(EncodeHintType.CHARACTER_SET, Charsets.UTF_8.name());
BitMatrix bitMatrix = null;
try {
bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
} catch (WriterException e) {
logger.error("toBitMatrix error", e);
}
return bitMatrix;
}
/**
* 用于保存到本地
*
* @param matrix the matrix object
* @param format 生成的图片后缀格式
* @param file 保存的图片位置
* @throws IOException {@link IOException}
*/
public static void toFile(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 the matrix object
* @param format format
* @param stream outputstream
* @throws IOException {@link IOException}
*/
public static void toStream(BitMatrix matrix, String format, OutputStream stream)
throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, stream)) {
throw new IOException("Could not write an image of format " + format);
}
}
}