JAVA中生成二维码图片的方法

  JAVA中生成二维码的方法并不复杂,使用google的zxing包就可以实现。下面的方法包含了生成二维码、在中间附加logo、添加文字功能。

一、下载zxing的架包,并导入项目中,如下:

最主要的包都在com.google.zxing.core下。如果是maven项目,maven依赖如下:

1 <dependency>
2   <groupId>com.google.zxing</groupId>
3   <artifactId>core</artifactId>
4   <version>3.3.0</version>
5 </dependency>

二、附上代码例子,如下:

  1 public class TestQRcode {
  2
  3     private static final int BLACK = 0xFF000000;
  4     private static final int WHITE = 0xFFFFFFFF;
  5     private static final int margin = 0;
  6     private static final int LogoPart = 4;
  7
  8     /**
  9      * 生成二维码矩阵信息
 10      * @param content 二维码图片内容
 11      * @param width 二维码图片宽度
 12      * @param height 二维码图片高度
 13      */
 14     public static BitMatrix setBitMatrix(String content, int width, int height){
 15         Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
 16         hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 指定编码方式,防止中文乱码
 17         hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 指定纠错等级
 18         hints.put(EncodeHintType.MARGIN, margin); // 指定二维码四周白色区域大小
 19         BitMatrix bitMatrix = null;
 20         try {
 21             bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
 22         } catch (WriterException e) {
 23             e.printStackTrace();
 24         }
 25         return bitMatrix;
 26     }
 27
 28     /**
 29      * 将二维码图片输出
 30      * @param matrix 二维码矩阵信息
 31      * @param format 图片格式
 32      * @param outStream 输出流
 33      * @param logoPath logo图片路径
 34      */
 35     public static void writeToFile(BitMatrix matrix, String format, OutputStream outStream, String logoPath) throws IOException {
 36         BufferedImage image = toBufferedImage(matrix);
 37         // 加入LOGO水印效果
 38         if (StringUtils.isNotBlank(logoPath)) {
 39             image = addLogo(image, logoPath);
 40         }
 41         ImageIO.write(image, format, outStream);
 42     }
 43
 44     /**
 45      * 生成二维码图片
 46      * @param matrix 二维码矩阵信息
 47      */
 48     public static BufferedImage toBufferedImage(BitMatrix matrix) {
 49         int width = matrix.getWidth();
 50         int height = matrix.getHeight();
 51         BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
 52         for (int x = 0; x < width; x++) {
 53             for (int y = 0; y < height; y++) {
 54                 image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
 55             }
 56         }
 57         return image;
 58     }
 59
 60     /**
 61      * 在二维码图片中添加logo图片
 62      * @param image 二维码图片
 63      * @param logoPath logo图片路径
 64      */
 65     public static BufferedImage addLogo(BufferedImage image, String logoPath) throws IOException {
 66         Graphics2D g = image.createGraphics();
 67         BufferedImage logoImage = ImageIO.read(new File(logoPath));
 68         // 计算logo图片大小,可适应长方形图片,根据较短边生成正方形
 69         int width = image.getWidth() < image.getHeight() ? image.getWidth() / LogoPart : image.getHeight() / LogoPart;
 70         int height = width;
 71         // 计算logo图片放置位置
 72         int x = (image.getWidth() - width) / 2;
 73         int y = (image.getHeight() - height) / 2;
 74         // 在二维码图片上绘制logo图片
 75         g.drawImage(logoImage, x, y, width, height, null);
 76         // 绘制logo边框,可选
 77 //        g.drawRoundRect(x, y, logoImage.getWidth(), logoImage.getHeight(), 10, 10);
 78         g.setStroke(new BasicStroke(2)); // 画笔粗细
 79         g.setColor(Color.WHITE); // 边框颜色
 80         g.drawRect(x, y, width, height); // 矩形边框
 81         logoImage.flush();
 82         g.dispose();
 83         return image;
 84     }
 85
 86     /**
 87      * 为图片添加文字
 88      * @param pressText 文字
 89      * @param newImage 带文字的图片
 90      * @param targetImage 需要添加文字的图片
 91      * @param fontStyle 字体风格
 92      * @param color 字体颜色
 93      * @param fontSize 字体大小
 94      * @param width 图片宽度
 95      * @param height 图片高度
 96      */
 97     public static void pressText(String pressText, String newImage, String targetImage, int fontStyle, Color color, int fontSize, int width, int height) {
 98         // 计算文字开始的位置
 99         // x开始的位置:(图片宽度-字体大小*字的个数)/2
100         int startX = (width-(fontSize*pressText.length()))/2;
101         // y开始的位置:图片高度-(图片高度-图片宽度)/2
102         int startY = height-(height-width)/2 + fontSize;
103         try {
104             File file = new File(targetImage);
105             BufferedImage src = ImageIO.read(file);
106             int imageW = src.getWidth(null);
107             int imageH = src.getHeight(null);
108             BufferedImage image = new BufferedImage(imageW, imageH, BufferedImage.TYPE_INT_RGB);
109             Graphics g = image.createGraphics();
110             g.drawImage(src, 0, 0, imageW, imageH, null);
111             g.setColor(color);
112             g.setFont(new Font(null, fontStyle, fontSize));
113             g.drawString(pressText, startX, startY);
114             g.dispose();
115             FileOutputStream out = new FileOutputStream(newImage);
116             ImageIO.write(image, "png", out);
117             out.close();
118         } catch (Exception e) {
119             System.out.println(e);
120         }
121     }
122
123     public static void main(String[] args) {
124         String content = "http://www.baidu.com";
125         String logoPath = "C:/logo.png";
126         String format = "jpg";
127         int width = 180;
128         int height = 220;
129         BitMatrix bitMatrix = setBitMatrix(content, width, height);
130         // 可通过输出流输出到页面,也可直接保存到文件
131         OutputStream outStream = null;
132         String path = "c:/qr"+new Date().getTime()+".png";
133         try {
134             outStream = new FileOutputStream(new File(path));
135             writeToFile(bitMatrix, format, outStream, logoPath);
136             outStream.close();
137         } catch (Exception e) {
138             e.printStackTrace();
139         }
140         // 添加文字效果
141         int fontSize = 12; // 字体大小
142         int fontStyle = 1; // 字体风格
143         String text = "测试二维码";
144         String withTextPath = "c:/text"+new Date().getTime()+".png";
145         pressText(text, withTextPath, path, fontStyle, Color.BLUE, fontSize, width, height);
146     }
147 }

三、生成效果如下:

  代码注释比较详细,就不多解释啦,大家可以根据自己的需求进行调整。

PS:

1、如果想生成带文字的二维码,记得要用长方形图片,为文字预留空间。

2、要生成带logo的二维码要注意遮挡率的问题,setBitMatrix()方法中ErrorCorrectionLevel.H这个纠错等级参数决定了二维码可被遮挡率。对应如下:

L水平 7%的字码可被修正
M水平 15%的字码可被修正
Q水平 25%的字码可被修正
H水平 30%的字码可被修正
时间: 2024-07-29 23:32:20

JAVA中生成二维码图片的方法的相关文章

C#生成二维码图片

使用C#生成二维码图片,并保存到指定的目录. 1.添加对生成二维码图片dll的引用: 下载地址:http://files.cnblogs.com/files/zflsyn/ThoughtWorks.QRCode.zip 2.引用命名空间 1 using System.Text; 2 using System.Drawing; 3 using ThoughtWorks; 4 using ThoughtWorks.QRCode; 5 using ThoughtWorks.QRCode.Codec;

JAVA生成二维码图片代码

首先需要导入 QRCode.jar 包 下载地址看这里   http://pan.baidu.com/s/1o6qRFqM import java.awt.Color;import java.awt.Graphics2D;import java.awt.Image;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.io.UnsupportedEncoding

java 使用qrcode生成二维码图片或者base64字符串

通过传入字符串,生成二维码图片或者base64格式字符串 1 public static String barcode2Base64(String msg) throws Exception{ 2 Qrcode x = new Qrcode(); 3 //N代表数字,A代表a-z,B代表其他字符 4 x.setQrcodeEncodeMode('B'); 5 //设置纠错等级 6 x.setQrcodeErrorCorrect('M'); 7 //设置版本号(1-40) 8 x.setQrcod

java实现生成二维码

                                                     java实现生成二维码 二维码vs条形码 最大的区别就是:二维码具有容错功能,当二维码图片被遮挡一部分后,仍可以扫描出来.容错的原理是二维码在编码过程中进行了冗余,就像是123被编码成123123,这样只要扫描到一部分二维码图片,二维码内容还是可以被全部读到. 二维码容错率即是指二维码图标被遮挡多少后,仍可以被扫描出来的能力.容错率越高,则二维码图片能被遮挡的部分越多. 二维码容错率用字母表

使用python调用zxing库生成二维码图片

(1)     安装Jpype 用python调用jar包须要安装jpype扩展,在Ubuntu上能够直接使用apt-get安装jpype扩展 $ sudo apt-get install python-jpype 关于使用Jpype调用jar包的方式.请看http://blog.csdn.net/niuyisheng/article/details/9002926 (2)     得到zxing  jar包 使用zxing第三方库生成二维码图片,关于zxing的介绍能够看其github地址:h

C# 利用QRCode生成二维码图片

引用LYBwwp的博文http://blog.csdn.net/lybwwp/article/details/18444369 网上生成二维码的组件是真多,可是真正好用的,并且生成速度很快的没几个,QRCode就是我在众多中找到的,它的生成速度快.但是网上关于它的使用说明,真的太少了,大都是千篇一律的复制粘贴.这是本要用它做了一个项目后,简单的整理了一下. 组件下载地址:http://download.csdn.net/detail/lybwwp/6861821 下载文件包包含ThoughtWo

在.net core web项目中生成二维码

原文:在.net core web项目中生成二维码 1.添加QRCoder包引用 2. public IActionResult MakeQrCode()        { string url="https://www.baidu.com"; var generator = new QRCodeGenerator(); var codeData = generator.CreateQrCode(str,QRCodeGenerator.ECCLevel.M,true); var qrc

QrenCode : 命令行下生成二维码图片

对于二维码大家应该并不陌生,英文名为 2-dimensional bar code 或 QR Code,是一种用图形记载信息的技术,最常见的是应用在手机应用上.用户通过手机摄像头扫描二维码或输入二维码下面的号码.关键字即可实现快速手机上网,快速便捷地浏览网页.下载图文.音乐.视频等等. 在 Ubuntu / Linux 上,有一个名为 QrenCode 的命令行工具可以很容易帮我们生成二维码. # 安装: sudo apt-get install qrencode # 使用: qrencode

phpqrcode.php 生成二维码图片用于推广

<?php /* * PHP QR Code encoder * * This file contains MERGED version of PHP QR Code library. * It was auto-generated from full version for your convenience. * * This merged version was configured to not requre any external files, * with disabled cach