- 浏览: 5103612 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
silence19841230:
先拿走看看
SpringBoot2.0开发WebSocket应用完整示例 -
wallimn:
masuweng 写道发下源码下载地址吧!三个相关文件打了个包 ...
SpringBoot2.0开发WebSocket应用完整示例 -
masuweng:
发下源码下载地址吧!
SpringBoot2.0开发WebSocket应用完整示例 -
masuweng:
SpringBoot2.0开发WebSocket应用完整示例 -
wallimn:
水淼火 写道你好,我使用以后,图标不显示,应该怎么引用呢,谢谢 ...
前端框架iviewui使用示例之菜单+多Tab页布局
最近琢磨了一下二维码、一维码的编码、解码方法,感觉google的zxing用起来还是比较方便。
本人原创,欢迎转载,转载请标注原文地址:http://wallimn.iteye.com/blog/2071020
一、工具类
二、使用示例
本人原创,欢迎转载,转载请标注原文地址:http://wallimn.iteye.com/blog/2071020
一、工具类
package com.exam.services.qrcode; import com.google.zxing.BarcodeFormat; import com.google.zxing.BinaryBitmap; import com.google.zxing.DecodeHintType; import com.google.zxing.EncodeHintType; import com.google.zxing.LuminanceSource; import com.google.zxing.MultiFormatReader; import com.google.zxing.MultiFormatWriter; import com.google.zxing.Result; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.BufferedImageLuminanceSource; import com.google.zxing.common.BitMatrix; import com.google.zxing.common.HybridBinarizer; import javax.imageio.ImageIO; import java.io.File; import java.io.OutputStream; import java.io.IOException; import java.util.Hashtable; import java.awt.image.BufferedImage; /** * 使用ZXing2.3,生成条码的辅助类。可以编码、解码。编码使用code包,解码需要javase包。 * * <br/> * <br/> * 作者:wallimn<br/> * 联系:54871876@qq.com,http://wallimn.iteye.com<br/> * 时间:2014年5月25日 下午10:33:05<br/> */ public final class MatrixUtil { private static final String CHARSET = "utf-8"; private static final int BLACK = 0xFF000000; private static final int WHITE = 0xFFFFFFFF; /** * 禁止生成实例,生成实例也没有意义。 */ private MatrixUtil() { } /** * 生成矩阵,是一个简单的函数,参数固定,更多的是使用示范。 * * <br/> * <br/> * 作者:wallimn<br/> * 时间:2014年5月25日 下午10:41:12<br/> * 联系:54871876@qq.com<br/> * * @param text * @return */ public static BitMatrix toQRCodeMatrix(String text, Integer width, Integer height) { if (width == null || width < 300) { width = 300; } if (height == null || height < 300) { height = 300; } // 二维码的图片格式 Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); // 内容所使用编码 hints.put(EncodeHintType.CHARACTER_SET, CHARSET); BitMatrix bitMatrix = null; try { bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints); } catch (WriterException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 生成二维码 // File outputFile = new File("d:"+File.separator+"new.gif"); // MatrixUtil.writeToFile(bitMatrix, format, outputFile); return bitMatrix; } /** * 将指定的字符串生成二维码图片。简单的使用示例。 * * <br/> * <br/> * 作者:wallimn<br/> * 时间:2014年5月25日 下午10:44:52<br/> * 联系:54871876@qq.com<br/> * * @param text * @param file * @param format * @return */ public boolean toQrcodeFile(String text, File file, String format) { BitMatrix matrix = toQRCodeMatrix(text, null, null); if (matrix != null) { try { writeToFile(matrix, format, file); return true; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return false; } /** * 根据点矩阵生成黑白图。 作者:wallimn<br/> * 时间:2014年5月25日 下午10:26:22<br/> * 联系:54871876@qq.com<br/> */ public static BufferedImage toBufferedImage(BitMatrix matrix) { int width = matrix.getWidth(); int height = matrix.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, matrix.get(x, y) ? BLACK : WHITE); } } return image; } /** * 将字符串编成一维条码的矩阵 * * <br/> * <br/> * 作者:wallimn<br/> * 时间:2014年5月25日 下午10:56:34<br/> * 联系:54871876@qq.com<br/> * * @param str * @param width * @param height * @return */ public static BitMatrix toBarCodeMatrix(String str, Integer width, Integer height) { if (width == null || width < 200) { width = 200; } if (height == null || height < 50) { height = 50; } try { // 文字编码 Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, CHARSET); BitMatrix bitMatrix = new MultiFormatWriter().encode(str, BarcodeFormat.CODE_128, width, height, hints); return bitMatrix; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 根据矩阵、图片格式,生成文件。 作者:wallimn<br/> * 时间:2014年5月25日 下午10:26:43<br/> * 联系:54871876@qq.com<br/> */ public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException { BufferedImage image = toBufferedImage(matrix); if (!ImageIO.write(image, format, file)) { throw new IOException("Could not write an image of format " + format + " to " + file); } } /** * 将矩阵写入到输出流中。 作者:wallimn<br/> * 时间:2014年5月25日 下午10:27:58<br/> * 联系:54871876@qq.com<br/> */ public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException { BufferedImage image = toBufferedImage(matrix); if (!ImageIO.write(image, format, stream)) { throw new IOException("Could not write an image of format " + format); } } /** * 解码,需要javase包。 * * <br/> * <br/> * 作者:wallimn<br/> * 时间:2014年5月25日 下午11:06:07<br/> * 联系:54871876@qq.com<br/> * * @param file * @return */ public static String decode(File file) { BufferedImage image; try { if (file == null || file.exists() == false) { throw new Exception(" File not found:" + file.getPath()); } image = ImageIO.read(file); LuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Result result; // 解码设置编码方式为:utf-8, Hashtable hints = new Hashtable(); hints.put(DecodeHintType.CHARACTER_SET, CHARSET); result = new MultiFormatReader().decode(bitmap, hints); return result.getText(); } catch (Exception e) { e.printStackTrace(); } return null; } }
二、使用示例
package com.exam.services.qrcode; import java.io.File; public class Test { /** * 测试函数。简单地将指定的字符串生成二维码图片。 * * <br/><br/> * 作者:wallimn<br/> * 时间:2014年5月25日 下午10:30:00<br/> * 联系:54871876@qq.com<br/> */ public static void main(String[] args) throws Exception { String text = "http://wallimn.itey.com"; String result; String format = "gif"; //生成二维码 File outputFile = new File("d:"+File.separator+"rqcode.gif"); MatrixUtil.writeToFile(MatrixUtil.toQRCodeMatrix(text, null, null), format, outputFile); result = MatrixUtil.decode(outputFile); System.out.println(result); outputFile = new File("d:"+File.separator+"barcode.gif"); MatrixUtil.writeToFile(MatrixUtil.toBarCodeMatrix(text, null, null), format, outputFile); result = MatrixUtil.decode(outputFile); System.out.println(result); } }
发表评论
-
gradle编译错误:Could not find method compile() for arguments
2020-09-19 10:50 18186编译(IDEA+Gradle)一个别人的工程,出现一个 ... -
netty心跳检查之UDP篇
2019-09-15 08:50 2129部分UDP通信场景中,需要客户端定期发送心跳信息,以获取终 ... -
解决tomcat部署两个SpringBoot应用提示InstanceAlreadyExistsException
2019-06-30 11:49 3175两个SpringBoot应用部署在一个Tomcat中,单独 ... -
Eclipse配置MyBatis代码自动化功能
2019-06-29 10:16 16431.安装插件 Eclipse中,Help->Ecli ... -
vue.js中使用qrcode生成二维码
2019-05-20 00:00 7562一、安装包 npm install qrcodejs2 --s ... -
MySQL插入数据报错: Incorrect string value: '\xFD\xDE'
2019-03-31 23:19 1174我MySQL数据库用的uft-8字符集,插入数据一直很正常 ... -
vue自定义组件并双向绑定属性
2019-03-08 22:46 3209做了两个子组件,原理基本一样,一个是使用原生的select ... -
vue-router简单示例
2019-03-05 00:32 1090写个基本完整、稍有借鉴意义的示例,防止自己忘记。 &l ... -
“联通充值系统繁忙”轻松应对
2019-02-06 11:03 3906大过年的,联通充个值一直报“充值系统繁忙”。昨天晚上试了几 ... -
electron.js数据库应用---导航菜单(element-ui+mysql)
2019-02-05 21:33 2296一、环境搭建 略, ... -
electron.js数据库应用---入门(mysql+element-ui)
2019-01-27 23:19 7314我的机器:Windows10,64 ... -
SpringMVC 在controller层中注入成员变量request,是否线程安全
2018-12-17 21:17 2693@RestController public class ... -
VueJS 组件参数名命名与组件属性转化
2018-12-03 00:00 2007转自:https://www.cnblogs.com/meiy ... -
vue-resource拦截器实现token发送及检验自动化
2018-11-16 22:38 3042用了很长时间vue-resource,最近思考$http发 ... -
element-ui试用手记
2018-10-29 20:25 1685element-ui、iviewui都以vue.js为基础 ... -
iviewui中表格控件中render的使用示例
2018-07-07 16:46 9716示例了如何在表格中显示按钮,如何将代码转化为文字。 i ... -
Tomcat错误“Alias name tomcat does not identify a key entry”解决
2018-07-05 21:39 6189申请到了阿里云的证书后,下载、按照说明生成jks格式证书、 ... -
阿里云免费证书“fileauth.txt内容配置错误”解决
2018-07-05 20:43 5201最近研究微信小程序开发,上阿里云申请了个证书,使用文件验证 ... -
springboot2.0跨域配置
2018-07-04 22:11 5239springboot2.0跨域配置: 一、代码 ... -
微信小程序使用code换openid的方法(JAVA、SpringBoot)
2018-07-01 21:52 10295微信小程序序的代码中提示,使用code换取openid,但 ...
相关推荐
google zxing读取、生成一维码、二维码图片 google zxing java 一维码 条形码 二维码 生成 读取
Zxing生成一维码二维码图片 Service服务
ZXing-2.3.0 一维码 二维码源代码
Zxing
基于zxing一维码二维码解析,可将内容转换为string
一维码生成BarcodeLib.dll 和 zxing.dll生成条形码和二维码 ,条码应用,与大家分享。BarcodeLib.dll 一维条码库支持以下条码格式。 ZXing (ZebraCrossing)是一个开源的,支持多种格式的条形码图像处理库, 。使用该...
ZXing识别和生成条形码和二维码
本文是利用ZXing.Net在WinForm中生成条形码,二维码的小例子,仅供学习分享使用,如有不足之处,还请指正。 什么是ZXing.Net? ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到...
NULL 博文链接:https://wiselyman.iteye.com/blog/2149556
Android使用ZXing生成和读取条形码、二维码、绝对超值、绝对可用、不会是另一个坑、
使用google开源zxing工具自己整理的二维码编码和解析代码,将logo按比例压缩后添加至彩色二维码中。
android上使用ZXing识别条形码和二维码,代码简单易懂,相信能帮助读者迅速掌握相关的开发
使用ireport生成一维码和二维码,二维码采用google zxing的方式生成,包含ireport生成二维码需要的jar包如javase.jar,core-3.0.0.jar等
一维码二维码生成识别所需jar包,直接引用即可,
通过zxing生成一维码和二维码与解析一维码和二维码源码.请大家放心下载绝不骗积分
ZXing一维码/二维码 使用文档 样例: System.IO.Stream stmYiWei = new System.IO.MemoryStream(); BitMatrix byteMatrix = new MultiFormatWriter().encode(sCode, BarcodeFormat.CODE_39, 230, 40); toBitmap...
C#利用Zxing.net生成条形码和二维码并实现打印,解码
zxing提供二维一维码扫描,本项目是android项目代码
android利用Zxing编码与解码
看着网上好看的二维码,也想着自己实现一个,经过3周的理论加开发时间,终于实现,详细介绍请看 https://blog.csdn.net/u011837804/article/details/129229973,本源码基于zxing组件扩展实现了草料二维码99%的功能。...