Java实现二维码的生成和解析_java解析二维码

		<version>3.3.3</version>
	</dependency>


> 
> ### 二维码工具类编:
> 
> 
> 


####  创建二维码图片:



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 vo

必看视频!获取2024年最新Java开发全套学习资料 备注Java

id 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 );

分享

这次面试我也做了一些总结,确实还有很多要学的东西。相关面试题也做了整理,可以分享给大家,了解一下面试真题,想进大厂的或者想跳槽的小伙伴不妨好好利用时间来学习。学习的脚步一定不能停止!

薪酬缩水,“裸辞”奋战25天三面美团,交叉面却被吊打,我太难了

Spring Cloud实战

薪酬缩水,“裸辞”奋战25天三面美团,交叉面却被吊打,我太难了

Spring Boot实战

薪酬缩水,“裸辞”奋战25天三面美团,交叉面却被吊打,我太难了

面试题整理(性能优化+微服务+并发编程+开源框架+分布式)
Cloud实战

[外链图片转存中…(img-DBgRWurq-1716459266314)]

Spring Boot实战

[外链图片转存中…(img-QXNcrOyk-1716459266314)]

面试题整理(性能优化+微服务+并发编程+开源框架+分布式)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值