当然,首先要导入zxing的jar包。

生成二维码代码:

package com.imooc.zxing;

import java.io.File;
import java.nio.file.Path;
import java.util.HashMap; import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; /**
* java代码生成二维码<br>
*
* @author Administrator
*
*/
public class CreateQRCode { public static void main(String[] args) {
int width = 300;
int height = 300;
String format = "png";
String content = "http://www.hupu.com"; // 定义二维码参数
HashMap hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");// 字符集
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);// 纠错级别
hints.put(EncodeHintType.MARGIN, 2);// 空白 try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
BarcodeFormat.QR_CODE, width, height, hints);
Path file = new File("E:/code/img.png").toPath(); MatrixToImageWriter.writeToPath(bitMatrix, format, file); } catch (Exception e) {
e.printStackTrace();
} } }

生成二维码如下:

读取二维码:

package com.imooc.zxing;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap; import javax.imageio.ImageIO; import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer; public class ReadQRCode { public static void main(String[] args) {
try {
MultiFormatReader formatReader = new MultiFormatReader();
File file = new File("E:/code/img.png");
BufferedImage image = ImageIO.read(file); BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
new BufferedImageLuminanceSource(image))); HashMap hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");// 字符集 Result result = formatReader.decode(binaryBitmap, hints); System.out.println("解析之后的结果:" + result.toString());
System.out.println("二维码格式类型:" + result.getBarcodeFormat());
System.out.println("二维码文本内容:" + result.getText()); } catch (Exception e) {
e.printStackTrace();
} } }

代码运行结果:

zxing生成二维码和读取二维码的更多相关文章

  1. 【java】google的zxing架包生成二维码和读取二维码【可带文字和logo】

    承接RC4生成不重复字符串的需求之后,因为优惠码要方便用户使用的缘故,所以思来想去,觉得还是直接生成二维码给用户直接扫比较实用,也不用用户专门记录冗长的优惠码编号. ================= ...

  2. ZXing生成二维码、读取二维码

    使用谷歌的开源包ZXing maven引入如下两个包即可 <dependency>   <groupId>com.google.zxing</groupId>  & ...

  3. 使用zxing生成彩色或带图片的二维码

    <!-- https://mvnrepository.com/artifact/com.google.zxing/core --> <dependency> <group ...

  4. PHP 二维码解码 (读取二维码)

    #zbar wget http://ncu.dl.sourceforge.net/project/zbar/zbar/0.10/zbar-0.10.tar.bz2 yum install gtk2 g ...

  5. jquery-qrcode 生成和读取二维码

    首先要导入jar包(生成二维码的jar和读取二维码的jar) 生成二维码: package com.imooc.qrcode; import java.awt.Color; import java.a ...

  6. ZXing 生成、读取二维码(带logo)

    前言 ZXing,一个支持在图像中解码和生成条形码(如二维码.PDF 417.EAN.UPC.Aztec.Data Matrix.Codabar)的库.ZXing(“zebra crossing”)是 ...

  7. ZXing 生成、解析二维码图片的小示例

    概述 ZXing 是一个开源 Java 类库用于解析多种格式的 1D/2D 条形码.目标是能够对QR编码.Data Matrix.UPC的1D条形码进行解码. 其提供了多种平台下的客户端包括:J2ME ...

  8. (转)ZXing生成二维码和带logo的二维码,模仿微信生成二维码效果

    场景:移动支付需要对二维码的生成与部署有所了解,掌握目前主流的二维码生成技术. 1 ZXing 生成二维码 首先说下,QRCode是日本人开发的,ZXing是google开发,barcode4j也是老 ...

  9. ZXing生成条形码、二维码、带logo二维码

    采用的是开源的ZXing,Maven配置如下,jar包下载地址,自己选择版本下载,顺便推荐下Maven Repository <!-- https://mvnrepository.com/art ...

随机推荐

  1. 【Android开发日记】之入门篇(十五)——ViewPager+自定义无限ViewPager

    ViewPager 在 Android 控件中,ViewPager 一直算是使用率比较高的控件,包括首页的banner,tab页的切换都能见到ViewPager的身影. viewpager 来源自 v ...

  2. leetcode 之Copy List with Random Pointer(23)

    深拷贝一个链表,不同的是这个链表有个额外的随机指针.参考:http://blog.csdn.net/ljiabin/article/details/39054999 做法非常的巧妙,分成三步,一是新建 ...

  3. 从Java Future到Guava ListenableFuture实现异步调用

    原文地址: http://blog.csdn.net/pistolove/article/details/51232004 Java Future     通过Executors可以创建不同类似的线程 ...

  4. LeetCode解题报告—— Maximal Rectangle

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and ...

  5. MySQL的数据引擎讲解

    一.MySQL的数据引擎讲解 在MySQL数据库中,常用的引擎主要就是2个:Innodb和MyIASM. 1.简单介绍这两种引擎,以及该如何去选择. a.Innodb引擎,Innodb引擎提供了对数据 ...

  6. 2.Spark Streaming运行机制和架构

    1 解密Spark Streaming运行机制 上节课我们谈到了技术界的寻龙点穴.这就像过去的风水一样,每个领域都有自己的龙脉,Spark就是龙脉之所在,它的龙穴或者关键点就是SparkStreami ...

  7. iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 8001 -j DNAT --to-destination 172.17.0.5:8080 ! -i docker0: iptables: No chain/target/match by that name.

    在docker容器上部署项目后,启动docker容器,出现 iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dpor ...

  8. shopnc 店铺二级分类添加商品

    $class_2 = $goods_class[$gc_id]['gc_parent_id']; $class_1 = $goods_class[$class_2]['gc_parent_id']; ...

  9. linux 进程命令小结

    top:动态查看进程的变化 ps:将某个时间点的进程运行情况选取下来 ps -l :仅查看自己的bash相关进程 ps -aux :查看系统所有的进程 pstree :列出目前系统上面所有的进程树的相 ...

  10. 第K短路模板【POJ2449 / 洛谷2483 / BZOJ1975 / HDU6181】

    1.到底如何求k短路的? 我们考虑,要求k短路,要先求出最短路/次短路/第三短路……/第(k-1)短路,然后访问到第k短路. 接下来的方法就是如此操作的. 2.f(x)的意义? 我们得到的f(x)更小 ...