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());
}
}
}

  jar包下载

java二维码生成工具的更多相关文章

  1. Java 二维码生成工具类

    /** * 二维码 工具 * * @author Rubekid * */ public class QRcodeUtils { /** * 默认version */ public static fi ...

  2. Java二维码生成与解码工具Zxing使用

    Zxing是Google研发的一款非常好用的开放源代码的二维码生成工具,目前源码托管在github上,源码地址: https://github.com/zxing/zxing 可以看到Zxing库有很 ...

  3. java二维码生成-谷歌(Google.zxing)开源二维码生成学习及实例

    java二维码生成-谷歌(Google.zxing)开源二维码生成的实例及介绍   我们使用比特矩阵(位矩阵)的QR码编码在缓冲图片上画出二维码 实例有以下一个传入参数 OutputStream ou ...

  4. Java二维码生成与解码

      基于google zxing 的Java二维码生成与解码   一.添加Maven依赖(解码时需要上传二维码图片,所以需要依赖文件上传包) <!-- google二维码工具 --> &l ...

  5. 二维码生成工具类java版

    注意:这里我不提供所需jar包的路径,我会把所有引用的jar包显示出来,大家自行Google package com.net.util; import java.awt.BasicStroke; im ...

  6. 谷歌zxing 二维码生成工具

    一.加入maven依赖 <!-- 谷歌zxing 二维码 --> <dependency> <groupId>com.google.zxing</groupI ...

  7. java 二维码生成(可带图片)springboot版

    本文(2019年6月29日 飞快的蜗牛博客) 有时候,男人和女人是两个完全不同的世界,男人的玩笑和女人的玩笑也完全是两码事,爱的人完全不了解你,你也不要指望一个女人了解你,所以男的不是要求别人怎么样, ...

  8. vue项目条形码和二维码生成工具试用

    项目开发需要,优惠券分不同类型,简单的使用id生成条形码供店铺使用,麻烦点的需要多个字段的就需要使用二维码来展示了,对应的效果如下 条形码(一维码)使用工具code128 需引入code128.js ...

  9. java 二维码生成

    直接上代码: 二维码生成核心类: package com.bbkj.wechat.tool; import java.awt.image.BufferedImage; import java.io.F ...

随机推荐

  1. iOS上使用自己定义ttf字体

    项目中想使用第三方的字体,在stackoverflow上查询解决的方法,也折腾一会,加入成功,示比例如以下: 1.将xx.ttf字体库增加project里面 2.在project的xx-Info.pl ...

  2. 将React Native集成至Android原生应用

    将React Native集成至Android原生应用 Android Studio 2.1 Preview 4生成的空项目 react-native 环境 0.22.2 初次编译后apk有1.1M, ...

  3. DevExpress Components16.2.6 Source Code 编译

    DevExpress 是一个比较有名的界面控件套件,提供了一系列优秀的界面控件.这篇文章将展示如何在拥有源代码的情况下,对 DevExpress 的程序集进行重新编译. 特别提示:重编译后,已安装好的 ...

  4. 比較两个 List 的值是否相等

    public static <T extends Comparable<T>> boolean compare(List<T> a, List<T> b ...

  5. [翻译] The Amazing Audio Engine

    The Amazing Audio Engine https://github.com/TheAmazingAudioEngine/TheAmazingAudioEngine The Amazing ...

  6. [JQuery] jQuery选择器ID、CLASS、标签获取对象值、属性、设置css样式

    reference : http://www.suyunyou.com/aid1657.html jQuery是继prototype之后又一个优秀的Javascrīpt框架.它是轻量级的js库(压缩后 ...

  7. [PHP] ubuntu 16.04系统下解决MySQL 5.7版本的root用户重置密码问题

    reference to : http://www.cnblogs.com/roadofstudy/p/7446690.html 最近在ubuntu系统上安装了MySQL,但是安装时没有提示输入roo ...

  8. Asp.net FileUpload+Image制作头像效果

    在Web开发中会经常使用到个人信息注册,而个人信息中通常需要自己的头像或者照片.今天主要介绍一下使用FileUpload+img控件上传照片. FileUpLoad控件使用介绍 FileUpLoad控 ...

  9. 15、高可用 PXC(percona xtradb cluster) 搭建

    安装环境: 集群名 pxc_lk 节点1: 192.168.1.20 节点2: 192.168.1.21 节点3: 192.168.1.22   所有节点安装 wget http://www.perc ...

  10. Objective-C:浅复制(拷贝)

    浅复制:复制对象时,如果对象中包含对象类型的实例变量,只是复制指针.新对象中的对象类型实例变量和旧对象中的对象类型实例变量指的是同一个对象.任何一方实例变量对对象做修改,另一方实例变量指向的该对象也就 ...