使用java解析和制作二维码
项目结构
文件源码 QR.zip
第一步:导入zxing的两个架包 core.jar和javase.jar
第二步:使用工具类 MatrixToImageWriter.java
package util;
import com.google.zxing.common.BitMatrix;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.OutputStream;
import java.io.IOException;
import java.awt.image.BufferedImage;
public final class MatrixToImageWriter {
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
private MatrixToImageWriter() {
}
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;
}
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);
}
}
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);
}
}
}
第三步:创建二维码
package demo;
import java.io.File;
import java.util.Hashtable;
import util.MatrixToImageWriter;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
/**
*将文字转换为二维码
*/
public class Encoder {
public static void main(String[] args) throws Exception {
String text = "http://www.tfgzs.com";
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
// 内容所使用编码
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(text,BarcodeFormat.QR_CODE, 300, 300, hints);
// 生成二维码
MatrixToImageWriter.writeToFile(bitMatrix, "gif", new File("E:/123/new.gif"));
System.out.println("二维码生成完成");
}
}
第四步:解析二维码
package demo;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
/**
* 将二维码解析为文字
*/
public class Decoder {
public static void main(String[] args) {
BufferedImage bufferedImage = null;
try {
bufferedImage = ImageIO.read(new File("E://123/qrcodeImage.png"));
} catch (IOException e) {
e.printStackTrace();
}
LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();
hints.put(DecodeHintType.CHARACTER_SET, "GBK");
Result result = null;
try {
result = new MultiFormatReader().decode(bitmap, hints);
} catch (NotFoundException e) {
e.printStackTrace();
}
System.out.println(result.toString());
}
}
使用java解析和制作二维码的更多相关文章
- 分享:Java 开发精美艺术二维码
博客地址:https://ainyi.com/58 Java 开发精美艺术二维码 看到网络上各种各样的二维码层出不穷,好像很炫酷的样子,一时兴起,我也要制作这种炫酷二维码效果 例如: 根据以往例子 根 ...
- Java后台直接生成二维码介绍
Java后台直接生成二维码 1.其实jquery也可以直接生成二维码的,但我测试的时候,二维码生成后太模糊,难以识别.所以在这里介绍在后来生成二维码的方式. 2.不善于文字描述,直接上代码了. imp ...
- asp.net 生成、解析条形码和二维码
原文 asp.net 生成.解析条形码和二维码 一.条形码 一维码,俗称条形码,广泛的用于电子工业等行业.比如我们常见的书籍背面就会有条形码,通过扫描枪等设备扫描就可以获得书籍的ISBN(Intern ...
- Java根据链接生成二维码
Java根据链接生成二维码 相关 jar 包: core-3.1.0.jar 源码及 jar 包下载:http://files.cnblogs.com/files/liaolongjun/qrcode ...
- java学习-zxing生成二维码矩阵的简单例子
这个例子需要使用google的开源项目zxing的核心jar包 core-3.2.0.jar 可以百度搜索下载jar文件,也可使用maven添加依赖 <dependency> <gr ...
- 关于APP上架制作二维码相关
1.安卓版本APP上架并生成二维码问题:安卓版本上架国内市场,这个情况比较复杂一些,比如百度,网址是以上传APP生成的一个编号来进行的,每次升级更新后都发生了变化,也就相当于每次升级后网址发生改变(比 ...
- 利用Python制作二维码
利用简单的Python代码制作二维码 一.制作工具 安装Python环境 + PyCharm编译器. 二.电脑系统 本人win10 + Python3.7.0 + PyCharm. 三.写代码前先下载 ...
- 如何使用二维码进行表白-一键制作二维码,gif二维码制作
" 如何使用二维码进行表白-一键制作二维码" 二维码在我们现在生活中随处可见,因为一次突发奇想,那我们可不可以制作一个自己的二维码呢!然后让女朋友去扫描一下----嘿嘿嘿--不行, ...
- java制作二维码的两种方式
原博:http://www.importnew.com/15028.html Zebra Crossing(ZXing)是一个很棒的,几乎可以在所有平台(Android.JavaSE.iPhone.R ...
随机推荐
- [nowCoder] 两个不等长数组求第K大数
给定两个有序数组arr1和arr2,在给定一个整数k,返回两个数组的所有数中第K小的数.例如:arr1 = {1,2,3,4,5};arr2 = {3,4,5};K = 1;因为1为所有数中最小的,所 ...
- Finite State Machine
Contents [hide] 1 Description 2 Components 3 C# - FSMSystem.cs 4 Example Description This is a Dete ...
- 数组对象Vector用法
import java.util.Vector; public class VectorTesting { public static void main(String[] args) { Vecto ...
- POJ2104 K-th number 函数式线段树
很久没打代码了,不知道为什么,昨天考岭南文化之前突然开始思考起这个问题来,这个问题据说有很多种方法,划分树什么的,不过对于我现在这种水平还是用熟悉的线段树做比较好.这到题今年8月份的时候曾经做过,那个 ...
- java001-Helloworld
public class test05 { public static void main(String[] args) { System.out.println("Hello World! ...
- 深入浅出ES6(五):不定参数和默认参数
作者 Jason Orendorff github主页 https://github.com/jorendorff 不定参数 我们通常使用可变参函数来构造API,可变参函数可接受任意数量的参数.例 ...
- hdu1022 Train Problem I
http://acm.hdu.edu.cn/showproblem.php?pid=1022 #include<iostream> #include<stdio.h> #inc ...
- 【memcache缓存专题(3)】PHP-memcache扩展的安装以及使用
安装PHP-memcache扩展和安装其他PHP扩展的步骤是一样的. 安装 step 1:搜索下载扩展 http://pecl.php.net/package/memcache step 2: gzi ...
- 去掉eclipse的xml和js验证
一.XML Properties > Validation you have two options: 1- Configure Workspace Settings (disable the ...
- spring autoWire注解
1.autowire注解,可以用来获得applicationContext,ResourceLoader,BeanFactory的注入 autoWire会获得相应资源 2.autoWire注解还可以用 ...