二维码现在已经在我们的生活中大量使用,如手机支付,扫一扫登录,扫一扫加好友等。
生成二维码
添加maven依赖
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.0</version>
</dependency>
使用工具生成二维码
public class Client {
public static void main(String[] args) throws Exception {
String content = "中国";
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.MARGIN, 0);
int onColor = 0xFF000000; //前景色
int offColor = 0xBFB0A8; //背景色
MatrixToImageConfig config = new MatrixToImageConfig(onColor, offColor);
BitMatrix bitMatrix = new MultiFormatWriter()
.encode(content, BarcodeFormat.QR_CODE, 300, 300, hints);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
MatrixToImageWriter.writeToStream(bitMatrix, "png", baos, config);
System.out.println(Base64.getEncoder().encodeToString(baos.toByteArray()));
}
}
生成的二维码如下
zxing包提供了多种码的创建,二维码,条形码等。
解析二维码
public class Client {
public static void main(String[] args) throws Exception {
InputStream is = new FileInputStream("D:/a.png");
BufferedImage image = ImageIO.read(is);
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bb = new BinaryBitmap(new HybridBinarizer(source));
HashMap<DecodeHintType, String> hints = new HashMap<>();
hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
Result result = new MultiFormatReader().decode(bb, hints);
System.out.println("二维码格式类型:" + result.getBarcodeFormat());
System.out.println("二维码文本内容:" + result.getText());
}
}
解析出的文本为:中国,为生成二维码时填入的内容。
生成带logo的二维码
public class Client {
public static void main(String[] args) throws Exception {
String content = "中国";
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.MARGIN, 0);
int onColor = 0xFF000000; //前景色
int offColor = 0xBFB0A8; //背景色
MatrixToImageConfig config = new MatrixToImageConfig(onColor, offColor);
BitMatrix bitMatrix = new MultiFormatWriter()
.encode(content, BarcodeFormat.QR_CODE, 300, 300, hints);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix, config);
addLogo(image);
ImageIO.write(image, "png", baos);
System.out.println(Base64.getEncoder().encodeToString(baos.toByteArray()));
}
private static void addLogo(BufferedImage image) throws IOException {
Graphics2D g = image.createGraphics();
// 读取logo图片
InputStream logoInput = Client.class.getClassLoader().getResourceAsStream("img/wechat.png");
if (Objects.isNull(logoInput)) {
return;
}
BufferedImage logo = ImageIO.read(logoInput);
// 设置logo的大小,本人设置为二维码图片的20%,由于过大会盖掉二维码
int widthLogo = logo.getWidth(null) > image.getWidth() * 2 / 10 ? (image.getWidth() * 2 / 10)
: logo.getWidth(null);
int heightLogo = logo
.getHeight(null) > image.getHeight() * 2 / 10 ?
(image.getHeight() * 2 / 10) : logo.getWidth(null);
// 计算图片放置位置
// logo放在中心
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, 15, 15);
g.drawRect(x, y, widthLogo, heightLogo);
g.dispose();
logo.flush();
}
}
生成的二维码如下
生成条形码
public class Client {
public static void main(String[] args) throws Exception {
String content = "123456789";
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.MARGIN, 0);
int onColor = 0xFF000000; //前景色
int offColor = 0xBFB0A8; //背景色
MatrixToImageConfig config = new MatrixToImageConfig(onColor, offColor);
BitMatrix bitMatrix = new MultiFormatWriter()
.encode(content, BarcodeFormat.CODE_128, 300, 100, hints);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
MatrixToImageWriter.writeToStream(bitMatrix, "png", baos, config);
System.out.println(Base64.getEncoder().encodeToString(baos.toByteArray()));
}
}
BarcodeFormat.CODE_128 表示条形码
生成的条形码如下
解析条形码
public class Client {
public static void main(String[] args) throws Exception {
InputStream is = new FileInputStream("D:/b.png");
BufferedImage image = ImageIO.read(is);
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bb = new BinaryBitmap(new HybridBinarizer(source));
HashMap<DecodeHintType, String> hints = new HashMap<>();
hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
Result result = new MultiFormatReader().decode(bb, hints);
System.out.println("二维码格式类型:" + result.getBarcodeFormat());
System.out.println("二维码文本内容:" + result.getText());
}
}
解析出的内容为:123456789