java二维码生成工具
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.util.Hashtable;
- import java.util.Map;
- import javax.imageio.ImageIO;
- 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.ReaderException;
- import com.google.zxing.Result;
- import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
- import com.google.zxing.common.BitMatrix;
- import com.google.zxing.common.HybridBinarizer;
- /**
- * 利用zxing开源工具生成二维码QRCode
- *
- * @date 2012-10-26
- * @author xhw
- *
- */
- public class QRCode {
- private static final int BLACK = 0xff000000;
- private static final int WHITE = 0xFFFFFFFF;
- /**
- * @param args
- */
- public static void main(String[] args) {
- /**
- * 在com.google.zxing.MultiFormatWriter类中,定义了一些我们不知道的码,二维码只是其中的一种<br>
- * public BitMatrix encode(String contents,
- BarcodeFormat format,
- int width, int height,
- Map<EncodeHintType,?> hints) throws WriterException {
- Writer writer;
- switch (format) {
- case EAN_8:
- writer = new EAN8Writer();
- break;
- case EAN_13:
- writer = new EAN13Writer();
- break;
- case UPC_A:
- writer = new UPCAWriter();
- break;
- case QR_CODE:
- writer = new QRCodeWriter();
- break;
- case CODE_39:
- writer = new Code39Writer();
- break;
- case CODE_128:
- writer = new Code128Writer();
- break;
- case ITF:
- writer = new ITFWriter();
- break;
- case PDF_417:
- writer = new PDF417Writer();
- break;
- case CODABAR:
- writer = new CodaBarWriter();
- break;
- default:
- throw new IllegalArgumentException("No encoder available for format " + format);
- }
- return writer.encode(contents, format, width, height, hints);
- }
- */
- String filePostfix="png";
- File file = new File("C://test_QR_CODE."+filePostfix);
- QRCode.encode("http://www.cnblogs.com/sprinng", file,filePostfix, BarcodeFormat.QR_CODE, 500, 500, null);
- QRCode.decode(file);
- }
- /**
- * 生成QRCode二维码<br>
- * 在编码时需要将com.google.zxing.qrcode.encoder.Encoder.java中的<br>
- * static final String DEFAULT_BYTE_MODE_ENCODING = "ISO8859-1";<br>
- * 修改为UTF-8,否则中文编译后解析不了<br>
- * @param contents 二维码的内容
- * @param file 二维码保存的路径,如:C://test_QR_CODE.png
- * @param filePostfix 生成二维码图片的格式:png,jpeg,gif等格式
- * @param format qrcode码的生成格式
- * @param width 图片宽度
- * @param height 图片高度
- * @param hints
- */
- public static void encode(String contents, File file,String filePostfix, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints) {
- try {
- BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, format, width, height);
- writeToFile(bitMatrix, filePostfix, file);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * 生成二维码图片<br>
- *
- * @param matrix
- * @param format
- * 图片格式
- * @param file
- * 生成二维码图片位置
- * @throws IOException
- */
- public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {
- BufferedImage image = toBufferedImage(matrix);
- ImageIO.write(image, format, file);
- }
- /**
- * 生成二维码内容<br>
- *
- * @param matrix
- * @return
- */
- public static BufferedImage toBufferedImage(BitMatrix matrix) {
- int width = matrix.getWidth();
- int height = matrix.getHeight();
- BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
- for (int x = 0; x < width; x++) {
- for (int y = 0; y < height; y++) {
- image.setRGB(x, y, matrix.get(x, y) == true ? BLACK : WHITE);
- }
- }
- return image;
- }
- /**
- * 解析QRCode二维码
- */
- @SuppressWarnings("unchecked")
- public static void decode(File file) {
- try {
- BufferedImage image;
- try {
- image = ImageIO.read(file);
- if (image == null) {
- System.out.println("Could not decode image");
- }
- LuminanceSource source = new BufferedImageLuminanceSource(image);
- BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
- Result result;
- @SuppressWarnings("rawtypes")
- Hashtable hints = new Hashtable();
- //解码设置编码方式为:utf-8
- hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
- result = new MultiFormatReader().decode(bitmap, hints);
- String resultStr = result.getText();
- System.out.println("解析后内容:" + resultStr);
- } catch (IOException ioe) {
- System.out.println(ioe.toString());
- } catch (ReaderException re) {
- System.out.println(re.toString());
- }
- } catch (Exception ex) {
- System.out.println(ex.toString());
- }
- }
- }
java二维码生成工具的更多相关文章
- Java 二维码生成工具类
/** * 二维码 工具 * * @author Rubekid * */ public class QRcodeUtils { /** * 默认version */ public static fi ...
- Java二维码生成与解码工具Zxing使用
Zxing是Google研发的一款非常好用的开放源代码的二维码生成工具,目前源码托管在github上,源码地址: https://github.com/zxing/zxing 可以看到Zxing库有很 ...
- java二维码生成-谷歌(Google.zxing)开源二维码生成学习及实例
java二维码生成-谷歌(Google.zxing)开源二维码生成的实例及介绍 我们使用比特矩阵(位矩阵)的QR码编码在缓冲图片上画出二维码 实例有以下一个传入参数 OutputStream ou ...
- Java二维码生成与解码
基于google zxing 的Java二维码生成与解码 一.添加Maven依赖(解码时需要上传二维码图片,所以需要依赖文件上传包) <!-- google二维码工具 --> &l ...
- 二维码生成工具类java版
注意:这里我不提供所需jar包的路径,我会把所有引用的jar包显示出来,大家自行Google package com.net.util; import java.awt.BasicStroke; im ...
- 谷歌zxing 二维码生成工具
一.加入maven依赖 <!-- 谷歌zxing 二维码 --> <dependency> <groupId>com.google.zxing</groupI ...
- java 二维码生成(可带图片)springboot版
本文(2019年6月29日 飞快的蜗牛博客) 有时候,男人和女人是两个完全不同的世界,男人的玩笑和女人的玩笑也完全是两码事,爱的人完全不了解你,你也不要指望一个女人了解你,所以男的不是要求别人怎么样, ...
- vue项目条形码和二维码生成工具试用
项目开发需要,优惠券分不同类型,简单的使用id生成条形码供店铺使用,麻烦点的需要多个字段的就需要使用二维码来展示了,对应的效果如下 条形码(一维码)使用工具code128 需引入code128.js ...
- java 二维码生成
直接上代码: 二维码生成核心类: package com.bbkj.wechat.tool; import java.awt.image.BufferedImage; import java.io.F ...
随机推荐
- 使用Chrome快速实现数据的抓取(三)——JQuery
使用Chrome抓取页面一个非常方便的地方就是它可以执行JS,也就是说我们可以通过JS函数获取我们想要的数据.一个非常强大易用的库就是Jquery,本文就简单的介绍一下使用Chrome获取数据时Jqu ...
- 【ButterKnife】 安卓程序猿的一大利器
注:近期才看到的这个类库,来自于jakewharton大神的力作,安卓里面的视图注入库 另小弟水平有限,翻译的不好,还请多多指正 首先是地址(托管在github上):http://jakewharto ...
- Property Finder – a Cross-Platform Xamarin MonoTouch Mobile App
Developers are now finding themselves having to author applications for a diverse range of mobile pl ...
- 聊聊JVM的年轻代(转)
聊聊JVM的年轻代 本文转自http://ifeve.com/jvm-yong-generation/ 1.为什么会有年轻代 我们先来屡屡,为什么需要把堆分代?不分代不能完成他所做的事情么?其实不分代 ...
- win7无线网络共享
一.最简单的方法: 1.使用360安全卫士 2.安装一个驱动人生 二.手工设置,参考:http://www.jb51.net/os/windows/63472.html
- js 设置焦点 判断控件是否获得焦点 判断哪个控件获得焦点
设置焦点 <html> <head> <title>设置焦点</title> <mce:script language ="javasc ...
- jstl 处理字符串函数 substring spli等
在jstl中的fn标签也是我们在网页设计中经常要用到的很关键的标签,在使用的时候要先加上头 <%@ taglib uri="http://java.sun.com/jsp/jstl/f ...
- [PHP] ubuntu16.04下 Phpstorm发布项目到apache
reference to : http://blog.csdn.net/qq_23937195/article/details/72953308 在网上找的不靠谱,倒腾了大半天的,终于找到正确姿势QA ...
- Rotate List leetcode java
题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example: Gi ...
- RS错误RSV-VAL-0032之项目未在布局中引用的3种解决办法
如下图所示,我用RS新建了一个空白页面,拖入了一个列表,给该列表新建了一个条件样式 条件样式如下所示,表达式来自查询1 运行,报错如下图所示 原因就是条件样式使用到了查询1中的数据项1但是数据项1在报 ...