由于公司要做一个仓库管理系统,通过二维码增删库存,先看下二维码图片显示效果如下:
接下来先把相对应的jar包导入进去,
链接:https://pan.baidu.com/s/1OooqJFUIxWx3p0q0g3AyIA
提取码:mdk4
或者直接maven
com.google.zxing
core
3.4.0
com.google.zxing
javase
3.4.0
下面直接上代码
package com.heiniao.pbuy.util.qrcode;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class QrcodeUtils {
/**
* 画二维码
*
* @param qrcodePath 存放二维码路径
* @param content 二维码内容
* @param height 二维码高度
* @param width 二维码宽度
*/
public static void drawQrcode(String qrcodePath, String content, int height, int width) {
try {
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
Map hints = new HashMap();
//设置UTF-8, 防止中文乱码
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
//设置二维码四周白色区域的大小
hints.put(EncodeHintType.MARGIN, 1);
//设置二维码的容错性
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
//画二维码,记得调用multiFormatWriter.encode()时最后要带上hints参数,不然上面设置无效
BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
//qrcFile用来存放生成的二维码图片(无logo,无文字)
File qrcodeFile = QrcodeUtils.findFile(qrcodePath);
//开始画二维码
MatrixToImageWriter.writeToFile(bitMatrix, "jpg", qrcodeFile);
} catch (WriterException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 给二维码图片添加Logo
*
* @param qrCodePath 二维码路径
* @param logoPath logo路径
* @param logoConfig logo 配置文件
*/
public static void addLogoToQRCode(String qrCodePath, String logoPath, String logoQrcodePath, LogoConfig logoConfig) {
try {
File qrCodeFile = new File(qrCodePath);
File logoFile = new File(logoPath);
if (!qrCodeFile.isFile() || !logoFile.isFile()) {
System.out.print("file not find origial file !");
System.exit(0);
}
//读取二维码图片,并构建绘图对象
BufferedImage image = ImageIO.read(qrCodeFile);
Graphics2D g = image.createGraphics();
//读取logo文件
BufferedImage logo = ImageIO.read(logoFile);
//设置logo的位置
int widthLogo = image.getWidth() / logoConfig.getLogoPart();
int heightLogo = image.getWidth() / logoConfig.getLogoPart(); // 保持二维码是正方形的
// 计算图片放置位置(居中)
int x = (image.getWidth() - widthLogo) / 2;
int y = (image.getHeight() - heightLogo) / 2;
// 开始绘制图片
g.drawImage(logo, x, y, widthLogo, heightLogo, null);
g.drawRoundRect(x, y, widthLogo, heightLogo, 10, 10);
g.setStroke(new BasicStroke(logoConfig.getBorder()));
g.setColor(logoConfig.getBorderColor());
g.drawRect(x, y, widthLogo, heightLogo);
g.dispose();
ImageIO.write(image, "jpeg", new File(logoQrcodePath));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param qrcodePath 二维码路径
* @param textQrcodePath 存放文字二维码路径
* @param width 文字二维码宽度
* @param height 文字二维码高度
* @param qrcodeFontList 文字内容集合
* @给二维码添加文字
*/
public static void pressText(String qrcodePath, String textQrcodePath, int width, int height, List qrcodeFontList) {
try {
File textQrcodeFile = findFile(qrcodePath);
System.out.println(textQrcodeFile.length());
Image src = ImageIO.read(textQrcodeFile);
int imageW = src.getWidth(null);
int imageH = src.getHeight(null);
BufferedImage image = new BufferedImage(imageW, imageH, BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.setColor(Color.WHITE);//设置笔刷白色
g.fillRect(0, 0, 600, 600);//填充整个屏幕
g.drawLine(0, 0, imageW, imageH);
g.drawImage(src, 100, 150, imageW / 2, imageH / 2, null);
for (QrcodeFont qrcodeFont : qrcodeFontList) {
g.setColor(qrcodeFont.getColor());
g.setFont(new Font("粗体", qrcodeFont.getFontStyle(), qrcodeFont.getFontSize()));
g.drawString(qrcodeFont.getText(), qrcodeFont.getStartX(), qrcodeFont.getStartY());
}
g.dispose();
FileOutputStream out = new FileOutputStream(textQrcodePath);
ImageIO.write(image, "JPEG", out);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
System.out.println("image press success");
} catch (Exception e) {
System.out.println(e);
}
}
/**
* @param filepath
* @return
* @查找文件(没有则创建文件)
*/
public static File findFile(String filepath) {
File file = new File(filepath);
if (!file.exists()) {
file.mkdir();
}
return file;
}
}
package com.heiniao.pbuy.util.qrcode;
import java.awt.Color;
public class LogoConfig {
// logo默认边框颜色
public static final Color DEFAULT_BORDERCOLOR = Color.WHITE;
// logo默认边框宽度
public static final int DEFAULT_BORDER = 2;
// logo大小默认为照片的1/6
public static final int DEFAULT_LOGOPART = 6;
private final int border = DEFAULT_BORDER;
private final Color borderColor;
private final int logoPart;
/**
* Creates a default config with on color {@link #BLACK} and off color
* {@link #WHITE}, generating normal black-on-white barcodes.
*/
public LogoConfig() {
this(DEFAULT_BORDERCOLOR, DEFAULT_LOGOPART);
}
public LogoConfig(Color borderColor, int logoPart) {
this.borderColor = borderColor;
this.logoPart = logoPart;
}
public Color getBorderColor() {
return borderColor;
}
public int getBorder() {
return border;
}
public int getLogoPart() {
return logoPart;
}
}
package com.heiniao.pbuy.util.qrcode;
import java.awt.Color;
import java.awt.Font;
public class QrcodeFont {
private int startX;//文字显示x坐标
private int startY;//文字显示y坐标
private String text;//显示的文字内容
private int fontSize = 30;//字体大小
private int fontStyle = Font.BOLD;//字体风格
private Color color = Color.BLUE;//文字显示颜色
public int getStartX() {
return startX;
}
public void setStartX(int startX) {
this.startX = startX;
}
public int getStartY() {
return startY;
}
public void setStartY(int startY) {
this.startY = startY;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public int getFontSize() {
return fontSize;
}
public void setFontSize(int fontSize) {
this.fontSize = fontSize;
}
public int getFontStyle() {
return fontStyle;
}
public void setFontStyle(int fontStyle) {
this.fontStyle = fontStyle;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
}
package com.heiniao.pbuy.util.qrcode;
import java.util.ArrayList;
import java.util.List;
public class QrcodeTest {
public static final String content = "http://www.baidu.com";//二维码里面的内容
public static final String logoPath = "d:/logo.jpg";//logo路径
public static final String qrcodePath = "D://qrcode.jpg";//单纯二维码存放地址
public static final String logoQrcodePath = "D://logoQrcode.jpg";//有logo二维码存放地址
public static final String textQrcodePath = "D://textQrcode.jpg";//有文字有logo二维码存放地址
public static void main(String args[]) {
try {
QrcodeUtils.drawQrcode(qrcodePath, content, 400, 400);
QrcodeUtils.addLogoToQRCode(qrcodePath, logoPath, logoQrcodePath, new LogoConfig());
//二维码文字的位置
List qrCodeFontList = new ArrayList();
QrcodeFont font1 = new QrcodeFont();
font1.setStartX(10);
font1.setStartY(370);
font1.setText("仓库号:四号仓库106号箱");
qrCodeFontList.add(font1);
QrcodeFont font2 = new QrcodeFont();
font2.setStartX(10);
font2.setStartY(50);
font2.setText("品牌:苹果");
qrCodeFontList.add(font2);
QrcodeFont font3 = new QrcodeFont();
font3.setStartX(10);
font3.setStartY(100);
font3.setText("型号:iPhone XS max");
qrCodeFontList.add(font3);
QrcodeFont font4 = new QrcodeFont();
font4.setStartX(10);
font4.setStartY(150);
font4.setText("规格:黑色");
qrCodeFontList.add(font4);
QrcodeUtils.pressText(logoQrcodePath, textQrcodePath, 400, 450, qrCodeFontList);
} catch (Exception e) {
e.printStackTrace();
}
}
}