首页 > 编程知识 正文

qrcode识别二维码,java实现二维码的生成与解析

时间:2023-05-05 13:25:36 阅读:45158 作者:2321

本文介绍了如何在java中生成二维码。 展示二维码主要有两个方面。 1 .直接生成图像(直接生成图像不需要web程序。 maven工程也可以)2.将二维码转换为字节数组并在网页上显示。 web项目的目录结构基于上一篇文章的目录结构。 生成二维码的功能主要依赖于谷歌的Zxing软件包。

1.添加Zxing的依赖(maven工程为例)

ependencygroupidcom.Google.zxing/groupidartifactidcore/artifactidversion3.3.0/version/dependencydepencygroooon

名为generateQRCodeImage方法的字符串封装在二维码中,设置二维码的宽度和高度,并声明二维码存储的路径和图像的名称。

package org.thinkingingis.utils; import Java.io.bytearray output stream; import java.io.IOException; import Java.nio.file.file systems; import java.nio.file.Path; import com.Google.zxing.barcode format; import com.Google.zxing.writer exception; import com.Google.zxing.client.J2SE.matrixtoimagewriter; import com.Google.zxing.com mon.bit matrix; 输入com.Google.zxing.QR code.QR code writer; publicclassqrcodegenerator { privatestaticfinalstringqr _ code _ image _ path='/users/GIS boy/desktop/my QR code.path privatestaticvoidgenerateqrcodeimage (字符串文本,int width,int height,字符串文件路径) throws WriterException io exception bitmatrixbitmatrix=QR code writer.encode (text,BarcodeFormat.QR_CODE,width,height ); 路径路径=file systems.get default ().getpath(filepath ); matrixtoimagewriter.write to path (位矩阵,' PNG ',path ); } publicstaticvoidmain (字符串[ ] args ) try ) generateqrcodeimage (thisismyfirstqrcode ),350,350, QR _ code _ iimage } catch (writerexceptione ) system.out.println (' couldnotgenerateqrcode, writer exception 33603360 ' e.getmessage } catch (ioexceptione ) system.out.println (' couldnotgenerateqrcode,io exceptione }}上面的代码生成一个名为" This is my first QR Code "的二维码,并将其存储在桌面上,如下图所示。

上面的方法是将二维码另存为图片。 如果不想将二维码另存为图片,也可以将其另存为字节数组。 请使用zxing库提供的matrixtoimagewriter.write to stream ()方法。

公共静态字节[ ] getqrcodeimage (字符串文本,int width,int height ) throws WriterException,io exception { qrcodewriterqr bytearrayoutputstreampngoutputstream=newbytearrayoutputstream (; matrixtoimagewriter.write to stream (bit matrix,' PNG ',PNG输出流); byte [ ] png data=png output stream.to bytearray (; 返回png数据; }此方法允许将字节数组作为图像显示在网页上。 在结合Spring Boot Thymeleaf构建的web项目中,如果要在页面上查看其二维码信息,则必须在html页面和控制器上完成以下代码:

qrcode.html

! doctypehtmlhtmlxmlns 3360 th=' http://www.thyme leaf.org ' headlinkrel=' style sheet ' type=' text/CSS ' href=' webjapkrel jquery/2.2.4/jquery.min.js '/script/headbodydivth 3360 reper divdivclass=' container ' divclass=' starter-tater divdivclass=' container ' img alt=footerpathinkingingis/a 2019/p/footer/div/body/htmldefaultcontrolller.Java

@getmapping(QRcode ) ) public String qrcode ) ) return )/QRcode ); }@getmapping(value=(/QRimage ) ) publicresponseentitybyte (getqrimage ) ) /二维码中的信息stringinfo=(thisismyfirsttqrccccare try { QR code=QR code generator.getqrcodeimage (info,360,360 ); }catch(writerexceptione ) system.out.println (' couldnotgenerateqrcode,writer exception 33603360 ' e.getmessage.PPP }catch(ioexceptione ) system.out.println (' couldnotgenerateqrcode,Ioexception3360: ) e.getmessage ); //setheadersfinalhttpheadersheaders=newhttpheaders (; headers.set content type (media type.image _ png; returnnewresponseentitybyte [ (QR代码,headers,HttpStatus.CREATED ); 请注意qrcode.html中3358www.Sina.com/标记的2.保存信息为二维码图片属性的值。

启动spring boot程序并访问此页面,如下图所示运行。

源地址: https://github.com/thinkingingis/spring-boot-security

现在,您已经构建了一个简单的生成和在web上显示二维码程序的功能。

如果有问题,请给作者留言,共同研究gis知识。 thinkingingis@qq.com)

更多干货请关注微信公众号:ThinkingInGIS

如果你认为正文对你有帮助,你可以称赞作者哦

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。