import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import static com.google.zxing.client.j2se.MatrixToImageConfig.BLACK;
import static com.google.zxing.client.j2se.MatrixToImageConfig.WHITE;
/**
* Desc: 图片生成工具类
*/
public class ImageUtil {
private final static Logger logger = LoggerFactory.getLogger(ImageUtil.class);
/**
* 二维码生成方法
*
* @param content 二维码条码内容
* @param width 二维码宽
* @param height 二维码高
* @return BufferedImage
* @throws Exception
*/
public static BufferedImage QrCodeImage(String content, int width, int height) throws Exception {
Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //矫错级别
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.MARGIN, 1);
//创建比特矩阵(位矩阵)的QR码编码的字符串
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
//使Buffered勾画QRCode(matrixWidth是行二维码像素点)
int matrixWidth = bitMatrix.getWidth();
int martrixHeight = bitMatrix.getHeight();
BufferedImage image = new BufferedImage(matrixWidth, martrixHeight, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < matrixWidth; x++) {
for (int y = 0; y < martrixHeight; y++) {
image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
}
}
return image;
}
/**
* 条形码生成方法
*
* @param content 生成条码的内容
* @param width 条码高度
* @param height 条码高度
* @return BufferedImage
* @throws Exception
*/
public static BufferedImage barcodeImage(String content, int width, int height) throws Exception {
Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //矫错级别
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.MARGIN, 1);
int codeWidth = 3 + (7 * 6) + 5 + (7 * 6) + 3;
codeWidth = Math.max(codeWidth, width);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.CODE_128, codeWidth, height, hints);
int matrixWidth = bitMatrix.getWidth();
int martrixHeight = bitMatrix.getHeight();
BufferedImage image = new BufferedImage(matrixWidth, martrixHeight, BufferedImage.TYPE_INT_ARGB);
for (int x = 0; x < matrixWidth; x++) {
for (int y = 0; y < martrixHeight; y++) {
image.setRGB(x, y, bitMatrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
}
/**
* 合成图片
*
* @param qrCode 二维码图片
* @param barcode 条码图片
* @param content 填写的内容
* @param path 图片临时存放目录
* @return File
* @throws IOException
*/
public static File distmemberImage(BufferedImage qrCode, BufferedImage barcode, String content, String path) throws IOException {
File file = null;
try {
BufferedImage img = new BufferedImage(600, 800, BufferedImage.TYPE_INT_RGB);//创建背景图片
Graphics2D g = (Graphics2D) img.getGraphics();//开启画图
g.setBackground(Color.WHITE); //设置背景色
g.clearRect(0, 0, 600, 800);//通过使用当前绘图表面的背景色进行填充来清除指定的矩形。
//读取本地图片
// BufferedImage bg = ImageIO.read(new File("C:\\Users\\Administrator.N1I11YAE7VJWQJS\\Desktop\\2.jpg"));//读取本地图片
// BufferedImage logo = ImageIO.read(new URL("http://a.hiphotos.baidu.com/image/pic/item/96dda144ad3459825a7754f50ef431adcaef84dc.jpg"));//读取互联网图片
// g.drawImage(bg.getScaledInstance(533,800, Image.SCALE_DEFAULT), 0, 0, null); // 绘制缩小后的图
//在图片上写字
g.setColor(Color.black);
g.setFont(new Font("宋体", Font.BOLD, 30));
g.drawString(content, 150, 100);//绘制文字
g.drawString("请使用扫码枪扫描识别身份", 110, 160);//绘制文字
//合成图片
g.drawImage(barcode, 50, 200, null); // 绘制缩小后的图
g.drawImage(qrCode, 120, 370, null); // 绘制缩小后的图
//在图片上写字
g.setColor(Color.black);
g.setFont(new Font("宋体", Font.BOLD, 25));
g.drawString("请使用微信扫码领劵", 180, 750);//绘制文字
g.dispose(); //画完记得关闭g
//最后写到本地图片
file = new File(path);
ImageIO.write(img, "jpg", file);
return file;
} catch (Exception e) {
logger.error("生成促销员二维码图片异常, content={}", content, e);
}
return file;
}
}
图片合成工具类
猜你喜欢
转载自blog.csdn.net/huanshilang1985/article/details/82903756
今日推荐
周排行