• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Java实现的生成二维码和解析二维码URL操作示例

java 搞代码 4年前 (2022-01-05) 24次浏览 已收录 0个评论

这篇文章主要介绍了Java实现的生成二维码和解析二维码URL操作,结合实例形式分析了Java创建与解析二维码,以及文件读写等相关操作技巧,需要的朋友可以参考下

本文实例讲述了Java实现的生成二维码和解析二维码URL操作。分享给大家供大家参考,具体如下:

二维码依赖jar包,zxing

 <!-- 二维码依赖 start --> com.google.zxingjavase3.0.0

createQRCode生成二维码,anlysisQRCode解析二维码

 package com.xgt.util; import com.google.zxing.*; import com.google.zxing.client.j2se.BufferedImageLuminanceSource; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.common.HybridBinarizer; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import sun.misc.BASE64Encoder; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.*; import java.nio.file.Path; import java.util.Hashtable; public class QRCodeUtil { private static final Logger logger = LoggerFactory.getLogger(QRCodeUtil.class); /** * 创建二维码 * @param url * @param fileName * @return * @throws IOException */ public static String createQRCode(String url,String fileDirectory,String fileName) throws IOException { int width = 500; int height = 500; String format = "png"; Hashtable hints = new Hashtable(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); hints.put(EncodeHintType.MARGIN, 2); try { BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, width, height, hints); File fileDir = new File(fileDirectory); FileToolUtil.judeDirExists(fileDir); Path file = new File(fileDirectory,fileName+".png-600").toPath();//在D盘生成二维码图片 MatrixToImageWriter.writeToPath(bitMatrix, format, file); BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix); ByteArrayOutputStream os = new ByteArrayOutputStream();//新建流。 ImageIO.write(image, format, os);//利用ImageIO类提供的write方法,将bi以png图片的数据模式写入流。 byte b[] = os.toByteArray();//从流中获取数据数组。 String str = new BASE64Encoder().encode(b); IOUtils.closeQuietly(os); return str; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { //DeleteFileUtil.delete(fileDirectory); } return "NULL"; } /** * 解析出二维码的url * 无用 * @param file * @param fileDirectory * @throws NotFoundException */ public static void anlaysisQRCode(File file,String fileDirectory) throws NotFoundException { MultiFormatReader formatReader=new MultiFormatReader(); BufferedImage image=null; try { image = ImageIO.read(file); BinaryBitmap binaryBitmap =new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image))); Hashtable hints=new Hashtable(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); Result result=formatReader.decode(binaryBitmap,hints); logger.info("解析结果:"+result.toString()); logger.info("解析格式:"+result.getBarcodeFormat()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { DeleteFileUtil.delete(fileDirectory); } } } 

PS:这里再为大家推荐两款二维码相关在线工具供大家参考使用:

在线生成二维码工具(加强版)
http://tools.gaodaima.com/transcoding/jb51qrcode

在线二维码解码识别工具
http://tools.gaodaima.com/transcoding/trans_qrcode

更多关于java相关内容感兴趣的读者可查看本站专题:《Java编码操作技巧总结》、《Java数学运算技巧总结》、《Java数据结构与算法教程》、《Java字符与字符串操作技巧总来源gaodai#ma#com搞*!代#%^码网结》、《Java操作DOM节点技巧总结》和《Java缓存操作技巧汇总》

希望本文所述对大家java程序设计有所帮助。

以上就是Java实现的生成二维码和解析二维码URL操作示例的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Java实现的生成二维码和解析二维码URL操作示例

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址