本篇博客用于自己的小计,仅供参考。简单粗暴,直接上代码
需要引用的jar
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.0</version>
</dependency>
工具类
FolderUtils.java
import java.io.File;
import org.apache.commons.io.FilenameUtils;
/**
* 文件夹工具
*
*/
public class FolderUtils {
/**
* 创建完整路径
*
* @param path a {@link java.lang.String} object.
*/
public static final void mkdirs(final String... path) {
for (String foo : path) {
final String realPath = FilenameUtils.normalizeNoEndSeparator(foo, true);
final File folder = new File(realPath);
if (!folder.exists() || folder.isFile()) {
folder.mkdirs();
}
}
}
}
MatrixToImageWriterEx.java
import java.awt.BasicStroke;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import org.apache.commons.io.IOUtils;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
class MatrixToImageWriterEx {
private static final MatrixToLogoImageConfig DEFAULT_CONFIG = new MatrixToLogoImageConfig();
/**
* 根据内容生成二维码数据
*
* @param content
* 二维码文字内容[为了信息安全性,一般都要先进行数据加密]
* @param width
* 二维码照片宽度
* @param height
* 二维码照片高度
* @param errorCorrectionLevel
* 纠错等级
* @return a {@link com.google.zxing.common.BitMatrix} object.
* @since 0.0.7
*/
public static BitMatrix createQRCode(String content, int width, int height,
ErrorCorrectionLevel errorCorrectionLevel) {
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
// 设置字符编码
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
// 指定纠错等级
hints.put(EncodeHintType.ERROR_CORRECTION, errorCorrectionLevel);
BitMatrix matrix = null;
try {
matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
} catch (WriterException e) {
e.printStackTrace();
}
return matrix;
}
/**
* 根据内容生成二维码数据
*
* @param content
* 二维码文字内容[为了信息安全性,一般都要先进行数据加密]
* @param width
* 二维码照片宽度
* @param height
* 二维码照片高度
* @return a {@link com.google.zxing.common.BitMatrix} object.
* @since 0.0.7
*/
public static BitMatrix createQRCode(String content, int width, int height) {
return createQRCode(content, width, height, ErrorCorrectionLevel.H);
}
/**
* 写入二维码、以及将照片logo写入二维码中
*
* @param matrix
* 要写入的二维码
* @param format
* 二维码照片格式
* @param imagePath
* 二维码照片保存路径
* @param logoPath
* logo路径
* @throws java.io.IOException
* if any.
* @since 0.0.7
*/
public static void writeToFile(BitMatrix matrix, String format,
String imagePath, String logoPath) throws IOException {
InputStream input = null;
try {
input =