本文实例讲述了java实现的生成二维码和解析二维码url操作。分享给大家供大家参考,具体如下:
二维码依赖jar包,zxing
<!-- 二维码依赖 start --> <dependency> <groupid>com.google.zxing</groupid> <artifactid>javase</artifactid> <version>3.0.0</version> </dependency>
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").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:这里再为大家推荐两款二维码相关在线工具供大家参考使用:
在线生成二维码工具(加强版)
在线二维码解码识别工具
更多关于java相关内容感兴趣的读者可查看本站专题:《java编码操作技巧总结》、《java数学运算技巧总结》、《java数据结构与算法教程》、《java字符与字符串操作技巧总结》、《java操作dom节点技巧总结》和《java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。
如对本文有疑问, 点击进行留言回复!!
springmvc之ResponseBody响应json数据遇到的错误及解决
uni-app 后台升级 静默升级 uniapp 后台更新 静默更新 在线升级
SpringBoot多Module启动报错Could not transfer metadata
Hibernate项目报错:Cannot call sendError() after the response has been committed
网友评论