博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java实现二维码的编码与解码
阅读量:6231 次
发布时间:2019-06-21

本文共 4547 字,大约阅读时间需要 15 分钟。

hot3.png

<blockquote> <p>本文中实现二维码的操作依赖于google的zxing,需要添加的jar包有:core.jar 和 javase.jar</p> <p>依赖的类有:</p> <div> <pre><span style="color: #0000ff">import</span> javax.imageio.ImageIO;

<span style="color: #0000ff">import</span> com.google.zxing.BarcodeFormat; <span style="color: #0000ff">import</span> com.google.zxing.BinaryBitmap; <span style="color: #0000ff">import</span> com.google.zxing.DecodeHintType; <span style="color: #0000ff">import</span> com.google.zxing.EncodeHintType; <span style="color: #0000ff">import</span> com.google.zxing.LuminanceSource; <span style="color: #0000ff">import</span> com.google.zxing.MultiFormatReader; <span style="color: #0000ff">import</span> com.google.zxing.MultiFormatWriter; <span style="color: #0000ff">import</span> com.google.zxing.ReaderException; <span style="color: #0000ff">import</span> com.google.zxing.Result; <span style="color: #0000ff">import</span> com.google.zxing.client.j2se.BufferedImageLuminanceSource; <span style="color: #0000ff">import</span> com.google.zxing.client.j2se.MatrixToImageWriter; <span style="color: #0000ff">import</span> com.google.zxing.common.BitMatrix; <span style="color: #0000ff">import</span> com.google.zxing.common.HybridBinarizer;</pre>

</div>

<br />

<h4>二维码编码:</h4>

<div> <pre><span style="color: #008000">/** * 二维码编码 * @param str 需要编码的字符串 * @return */</span> @SuppressWarnings(&quot;<span style="color: #8b0000">unchecked</span>&quot;) <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> File encode(String str) { <span style="color: #0000ff">try</span> { SimpleDateFormat sdDateFormat = <span style="color: #0000ff">new</span> SimpleDateFormat(&quot;<span style="color: #8b0000">yyyyMMddhhmmss</span>&quot;); String path = &quot;<span style="color: #8b0000">./encode</span>&quot; + sdDateFormat.format(<span style="color: #0000ff">new</span> Date()) + &quot;<span style="color: #8b0000">.png</span>&quot;; <span style="color: #0000ff">int</span> width = 180; <span style="color: #0000ff">int</span> height = 160; Hashtable hints = <span style="color: #0000ff">new</span> Hashtable(); hints.put(EncodeHintType.CHARACTER_SET, &quot;<span style="color: #8b0000">utf-8</span>&quot;); BitMatrix byteMatrix; byteMatrix = <span style="color: #0000ff">new</span> MultiFormatWriter().encode(str,BarcodeFormat.QR_CODE, width, height,hints); File file = <span style="color: #0000ff">new</span> File(path); MatrixToImageWriter.writeToFile(byteMatrix, &quot;<span style="color: #8b0000">png</span>&quot;, file); <span style="color: #0000ff">return</span> file; } <span style="color: #0000ff">catch</span> (Exception e) { e.printStackTrace(); <span style="color: #0000ff">return</span> <span style="color: #0000ff">null</span>; } }</pre> </div>

<br />

<h4>二维码解码:</h4>

<div> <pre><span style="color: #008000">/** * 二维码解码 * @param file 需要解码的文件(如图片) * @return */</span> @SuppressWarnings(&quot;<span style="color: #8b0000">unchecked</span>&quot;) <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> Result decode(File file) { <span style="color: #0000ff">try</span> { BufferedImage image; <span style="color: #0000ff">try</span> { image = ImageIO.read(file); <span style="color: #0000ff">if</span> (image == <span style="color: #0000ff">null</span>) { System.out.println(&quot;<span style="color: #8b0000">Could not decode image</span>&quot;); } LuminanceSource source = <span style="color: #0000ff">new</span> BufferedImageLuminanceSource(image); BinaryBitmap bitmap = <span style="color: #0000ff">new</span> BinaryBitmap(<span style="color: #0000ff">new</span> HybridBinarizer( source)); Result result; Hashtable hints = <span style="color: #0000ff">new</span> Hashtable(); hints.put(DecodeHintType.CHARACTER_SET, &quot;<span style="color: #8b0000">utf-8</span>&quot;); result = <span style="color: #0000ff">new</span> MultiFormatReader().decode(bitmap, hints); <span style="color: #0000ff">return</span> result; } <span style="color: #0000ff">catch</span> (IOException ioe) { System.out.println(ioe.toString()); <span style="color: #0000ff">return</span> <span style="color: #0000ff">null</span>; } <span style="color: #0000ff">catch</span> (ReaderException re) { System.out.println(re.toString()); <span style="color: #0000ff">return</span> <span style="color: #0000ff">null</span>; } } <span style="color: #0000ff">catch</span> (Exception ex) { <span style="color: #0000ff">return</span> <span style="color: #0000ff">null</span>; } }</pre> </div></blockquote>

转载于:https://my.oschina.net/sorthWindow/blog/110902

你可能感兴趣的文章
关于web优化(一)
查看>>
C# 判断路径是否存在
查看>>
GIT 常见的使用指令
查看>>
ORACLE字符串函数
查看>>
Hibernate基础知识介绍
查看>>
nuttx学习-0:模拟安装
查看>>
写出3个使用this的典型应用
查看>>
Triangles
查看>>
BZOJ 1064 假面舞会
查看>>
mysql 优化
查看>>
java之collection总结
查看>>
提升控件绘制速度
查看>>
封装自用的脚本ajax.js
查看>>
SQL左、右、内、全连接
查看>>
Ehcache 整合Spring 使用页面、对象缓存
查看>>
哈哈哈哈哈哈哈
查看>>
wordpress学习四: 一个简单的自定义主题
查看>>
04文件操作1
查看>>
Asp.net mvc 3 - JSONResult return array
查看>>
Spring MVC - log4j 配置
查看>>