基于Java怎么实现二维码的生成和解析
作者:慢热型
时间:2023-04-26
来源:互联网
浏览:0
导入相关jar包com.google.zxingcore3.3.0com.google.zxingjavase3.3.3二维码工具类编创建二维码图片publicstaticBufferedImagecreateImage(StringcharSet,Stringcontent,intqrWidth,intqrHeight){Hashtablehints=newHashtable();hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel
导入相关jar包
com.google.zxing core 3.3.0 com.google.zxing javase 3.3.3
二维码工具类编
创建二维码图片
public static BufferedImage createImage(String charSet, String content, int qrWidth, int qrHeight) {
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, charSet);
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = null;
try {
bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, qrWidth, qrHeight, // 修改二维码底部高度
hints);
} catch (WriterException e) {
e.printStackTrace();
}
int width = bitMatrix.getWidth();
int height = bitMatrix.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, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
}
}
return image;
}二维码设置logo
public static void insertLogoImage(BufferedImage source, Image logo, int logoWidth, int logoHeight) {
Graphics2D graph = source.createGraphics();
int qrWidth = source.getWidth();
int qrHeight = source.getHeight();
int x = (qrWidth - logoWidth) / 2;
int y = (qrHeight - logoHeight) / 2;
graph.drawImage(logo, x, y, logoWidth, logoHeight, null);
Shape shape = new RoundRectangle2D.Float(x, y, logoWidth, logoHeight, 6, 6);
graph.setStroke(new BasicStroke(3f));
graph.draw(shape);
graph.dispose();
}将文明说明增加到二维码上
public static BufferedImage textToImage(String str, int width, int height, int fontSize) {
BufferedImage textImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D) textImage.getGraphics();
//开启文字抗锯齿
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2.setBackground(Color.WHITE);
g2.clearRect(0, 0, width, height);
g2.setPaint(Color.BLACK);
FontRenderContext context = g2.getFontRenderContext();
Font font = new Font("微软雅黑", Font.PLAIN, fontSize);
g2.setFont(font);
LineMetrics lineMetrics = font.getLineMetrics(str, context);
FontMetrics fontMetrics = FontDesignMetrics.getMetrics(font);
float offset = (width - fontMetrics.stringWidth(str)) / 2;
float y = (height + lineMetrics.getAscent() - lineMetrics.getDescent() - lineMetrics.getLeading()) / 2;
g2.drawString(str, (int) offset, (int) y);
return textImage;
}解析二维码
/*
* 解析二维码
*/
public static String decode(File file, DecodeHintType cherSet) throws Exception {
BufferedImage image;
image = ImageIO.read(file);
if (image == null) {
return null;
}
BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result;
Hashtable hints = new Hashtable();
hints.put(DecodeHintType.CHARACTER_SET, cherSet);
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
result = new MultiFormatReader().decode(bitmap, hints);
String resultStr = result.getText();
return resultStr;
}main方法测试类
public static void main(String[] args) {
String content="用户摄影作品版权信息";
BufferedImage image = QRCodeUtil.createImage( "utf-8", content, 400, 400 );
QRCodeUtil.addUpFont( image,"用户摄影作品版权信息" );
String formatName="png";
String imagePath="D:\temp\java二维码.png";
File file=new File(imagePath);
try {
ImageIO.write(image, formatName, file);
} catch (IOException e) {
e.printStackTrace();
}
String decode = null;
try {
decode = QRCodeUtil.decode(file, DecodeHintType.CHARACTER_SET);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(decode);
System.out.println("执行完成");
}
作者最新文章
网易2026年Q2财报:营收301亿元,游戏收入增10%,三款重点新游披露进展
2026-09-08 17:51
PDF怎么添加页码?页码位置和起始页怎么设置?
2026-09-03 09:11
图片文件怎么转换成PDF?多张图片如何按顺序合成?
2026-09-02 19:33
CorelDRAW绘制正弦曲线的两种方法:贝塞尔工具与变形工具
2026-09-02 16:08
Excel工作表制作教程:设计易填写、易统计的业务表
2026-09-02 12:11
上一篇:
mysql锁的两种不同状态是什么
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多
Windows 10
Windows
Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式
Windows/macOS/Linux
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















