首先说下,QRCode是日本人开发的,ZXing是google开发,barcode4j也是老美开发的,barcode4j对一维条形码处理的很好,而且支持的格式很多,当然也可以对二维码进行处理,效果个人感觉没有前两种好;ZXing对j2me,j2se,还有Android等支持也比较好,如果你是搞Android的或以后准备走Android,建议还是用zxing的比较好,毕竟都一个母亲(goole)生的,QRCode就不用说了吧,虽说技术无国界,但是国人还是有点...。

好,言归正传,java用ZXing开发二维码,首先需要添加code.jar包,这个包需要自己生成下,官网下载ZXing-2.3.0(目前最新的)后,解压 把code中的代码拷贝到一个工程中生成jar包。ZXing-code-2.3.0.jar包和源码包本人已编译后,不想自己重新编译下的可以直接下载使用的,下面的代码就是使用这个jar包的,ZXing-code-2.3.0.jar下载地址:http://download.csdn.net/detail/sanfye/8704583

Code源码包:http://download.csdn.net/detail/sanfye/8704593

ZXing 生成二维码

二维码的生成可以参考Google代码测试包中的MatrixToImageWriter类来辅助开发,可以将该类直接拷贝到源码中使用 (code/src/test/java ),当然你也可以自己写个,个人感觉没必要所以就直接考过来用了。另外,在生成带图片的二维码时要注意下,参数中设置的容错级别尽量设置到最高,以免出现无法解析的情况:hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;import javax.imageio.ImageIO;import com.google.zxing.common.BitMatrix;/*** 二维码的生成需要借助MatrixToImageWriter类,该类是由Google提供的,可以将该类直接拷贝到源码中使用,当然你也可以自己写个* 生产条形码的基类*/
public 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));
//              image.setRGB(x, y,  (matrix.get(x, y) ? Color.YELLOW.getRGB() : Color.CYAN.getRGB()));}}return image;}public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {BufferedImage image = toBufferedImage(matrix);//设置logo图标LogoConfig logoConfig = new LogoConfig();image = logoConfig.LogoMatrix(image);if (!ImageIO.write(image, format, file)) {throw new IOException("Could not write an image of format " + format + " to " + file);}else{System.out.println("图片生成成功!");}}public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException {BufferedImage image = toBufferedImage(matrix);//设置logo图标LogoConfig logoConfig = new LogoConfig();image = logoConfig.LogoMatrix(image);if (!ImageIO.write(image, format, stream)) {throw new IOException("Could not write an image of format " + format);}}
}

操作类:

import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.utils.code.MatrixToImageWriter;public class EncodeTest {public static void Encode_QR_CODE() throws IOException, WriterException{String contents = "ZXing 二维码内容1234!"; // 二维码内容int width = 430; // 二维码图片宽度 300 int height = 430; // 二维码图片高度300String format = "gif";// 二维码的图片格式 gifHashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();// 指定纠错等级,纠错级别(L 7%、M 15%、Q 25%、H 30%)hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);// 内容所使用字符集编码hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
//      hints.put(EncodeHintType.MAX_SIZE, 350);//设置图片的最大值
//      hints.put(EncodeHintType.MIN_SIZE, 100);//设置图片的最小值hints.put(EncodeHintType.MARGIN, 1);//设置二维码边的空度,非负数BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,//要编码的内容//编码类型,目前zxing支持:Aztec 2D,CODABAR 1D format,Code 39 1D,Code 93 1D ,Code 128 1D,//Data Matrix 2D , EAN-8 1D,EAN-13 1D,ITF (Interleaved Two of Five) 1D,//MaxiCode 2D barcode,PDF417,QR Code 2D,RSS 14,RSS EXPANDED,UPC-A 1D,UPC-E 1D,UPC/EAN extension,UPC_EAN_EXTENSIONBarcodeFormat.QR_CODE,width, //条形码的宽度height, //条形码的高度hints);//生成条形码时的一些配置,此项可选// 生成二维码File outputFile = new File("d:" + File.separator + "new-1.gif");//指定输出路径MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);}public static void main(String[] args) throws Exception {try {Encode_QR_CODE();} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}}}

LogoConfig类,主要处理logo图标已达到和微信自动的二维码相近的效果

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/*** 二维码 添加 logo图标 处理的方法,* 模仿微信自动生成二维码效果,有圆角边框,logo和二维码间有空白区,logo带有灰色边框* @author Administrator sangwenhao**/
public class LogoConfig {/*** 设置 logo * @param matrixImage 源二维码图片* @return 返回带有logo的二维码图片* @throws IOException* @author Administrator sangwenhao*/public BufferedImage LogoMatrix(BufferedImage matrixImage) throws IOException{/*** 读取二维码图片,并构建绘图对象*/Graphics2D g2 = matrixImage.createGraphics();int matrixWidth = matrixImage.getWidth();int matrixHeigh = matrixImage.getHeight();/*** 读取Logo图片*/BufferedImage logo = ImageIO.read(new File("D:\\logo.jpg"));//开始绘制图片g2.drawImage(logo,matrixWidth/5*2,matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5, null);//绘制     BasicStroke stroke = new BasicStroke(5,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND); g2.setStroke(stroke);// 设置笔画对象//指定弧度的圆角矩形RoundRectangle2D.Float round = new RoundRectangle2D.Float(matrixWidth/5*2, matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5,20,20);g2.setColor(Color.white);g2.draw(round);// 绘制圆弧矩形//设置logo 有一道灰色边框BasicStroke stroke2 = new BasicStroke(1,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND); g2.setStroke(stroke2);// 设置笔画对象RoundRectangle2D.Float round2 = new RoundRectangle2D.Float(matrixWidth/5*2+2, matrixHeigh/5*2+2, matrixWidth/5-4, matrixHeigh/5-4,20,20);g2.setColor(new Color(128,128,128));g2.draw(round2);// 绘制圆弧矩形g2.dispose();matrixImage.flush() ;return matrixImage ;}}

ZXing生成二维码和带logo的二维码,模仿微信生成二维码效果相关推荐

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

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

  2. 二维码(带Logo)加密解密-ZXing方式

    二维码(带Logo)加密解密-ZXing方式 ZXing生成和解析二维码的流程步骤在代码的注解里面. 二维码的加密解密工具类 /** * Copyright © 2020wangylCompany. ...

  3. Py之qrcode:调用python的qrcode库两种方式生成二维码、带logo的二维码

    Py之qrcode:调用python的qrcode库两种方式生成二维码.带logo的二维码 目录 python编程实现生成二维码 1.第一种方式-纯文本 2.第二种方式-带logo

  4. 【小程序源码】升级版王者荣耀铭文多功能助手微信小程序源码下载

    这是一个王者铭文小程序 支持每一个英雄的铭文出装推荐查看 支持铭文组合模拟数据 另外还支持游戏重复名生成和空白名生成 比之前分享的一款单一铭文好一点吧 废话不多说,下面就一起来看看小编的测试演示图吧! ...

  5. zxing 二维码、带logo二维码生成

    <span style="font-size:18px;">普通二维码生成</span> <span style="font-size:18 ...

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

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

  7. C#利用ZXing.Net生成条形码,二维码和带Logo的二维码

    本文是利用ZXing.Net在WinForm中生成条形码,二维码的小例子,仅供学习分享使用,如有不足之处,还请指正. 什么是ZXing.Net? ZXing是一个开放源码的,用Java实现的多种格式的 ...

  8. java 生成二维码可带LOGO和文字描述

    maven依赖 <properties><zxing.version>3.3.0</zxing.version><commons-lang-version&g ...

  9. qrcode 生成二维码,带logo 带文字描述

    qrcode 生成二维码 1.引入 pom.xml 2. ResourceRenderer 3. QRCodeUtil 4. QRCodeController 5. HTML 6. 测试 1.引入 p ...

最新文章

  1. [分享]技术改进方案模板
  2. linux清理swap内容,Linux如何清理swap.buffer及cache等缓存
  3. python中re_Python中re(正则表达式)模块学习
  4. raid5需要几块硬盘_Raid5磁盘阵列数据恢复思路分析--附真实案例
  5. 网传“杭州市聘罗永浩为形象大使”?官方:为不实信息
  6. BZOJ 2527 Meteors | 整体二分
  7. 程序员工作交接文档怎么写_离职程序员交接工作被怒怼:每一行代码都必须讲清楚,不然投诉...
  8. ping 和 远程桌面 与防火墙的关系
  9. C#hashtable使用说明 以及 Hashtable和HashMap的区别【总结性】
  10. 时间序列预测——ARIMA模型
  11. 9008刷机工具_黔隆科技刷机教程OPPOR11T忘记密码免刷机保资料解屏幕锁教程
  12. a标签下载文档 a下载文档失败问题 跨域调用
  13. BZOJ 1406: [AHOI2007]密码箱 数论
  14. VMware安装Android x86_64 9.0虚拟机
  15. kile生成lib文件
  16. 网络赛1-D - Find Integer HDU - 6441
  17. 再聊面试,这次关于钱,关于培训,关于内卷
  18. bzoj4864 [BeiJing 2017 Wc]神秘物质
  19. java sql 工资管理,企业工资管理系统(Java+MySQL)Word版
  20. 【俞吾金】哲学的“世界”概念

热门文章

  1. 这10款App问题突出:强制安装捆绑应用 窃取信息偷偷吸费
  2. python写chrome插件_用VueJS写一个Chrome浏览器插件
  3. ResultSet的升级RowSet、离线的CachedRowSet、离线分页查询
  4. 如何让男生感觉到你的好
  5. c语言1000的阶乘有几个零,1000的阶乘后面有多少个零?
  6. microsoft office professional plus2007在安装过程中出错
  7. VS 不能启动,在开发者命令模式下以安全模式打开
  8. 【 源代码 】用Eclipse编写的Java小游戏——疯狂猜猜猜
  9. jQuery 遍历数组
  10. 唱吧 iOS 音视频缓存处理框架