二維碼現在已經在我們的生活中大量使用,如手機支付,掃一掃登錄,掃一掃加好友等。
生成二維碼
添加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