博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA生成二维码(zxing)
阅读量:7080 次
发布时间:2019-06-28

本文共 4779 字,大约阅读时间需要 15 分钟。

上一篇博客中介绍了条码的使用示例,这一篇继续介绍如何使用JAVA生成二维码。

package com.hq.util;import java.awt.Graphics2D;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.BarcodeFormat;import com.google.zxing.MultiFormatWriter;import com.google.zxing.WriterException;import com.google.zxing.common.BitMatrix;/** * 二维码创建,需添加jar包:zxing-core.jar *  * @author jianggujin *  */public class QRCreater{
/** * 生成二维码文件 * * @param content * 二维码内容 * @param size * 二维码大小 * @param file * 生成文件 * @throws IOException * @throws WriterException */ public void write(String content, int size, File file) throws IOException, WriterException { ImageIO.write( toBufferedImage(new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, size, size), null, 0), "JPEG", file); } /** * 生成二维码并写入指定输出流 * * @param content * 二维码内容 * @param size * 二维码大小 * @param os * 输出流 * @throws IOException * @throws WriterException */ public void write(String content, int size, OutputStream os) throws IOException, WriterException { ImageIO.write( toBufferedImage(new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, size, size), null, 0), "JPEG", os); } /** * 生成带图标的二维码文件 * * @param content * 二维码内容 * @param size * 二维码大小 * @param icon * 图标文件 * @param iconSize * 图标大小 * @param file * 生成文件 * @throws IOException * @throws WriterException */ public void write(String content, int size, File icon, int iconSize, File file) throws IOException, WriterException { ImageIO.write( toBufferedImage(new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, size, size), icon, iconSize), "JPEG", file); } /** * 生成带图标的二维码并写入指定输出流 * * @param content * 二维码内容 * @param size * 二维码大小 * @param icon * 图标文件 * @param iconSize * 图标大小 * @param os * 输出流 * @throws IOException * @throws WriterException */ public void write(String content, int size, File icon, int iconSize, OutputStream os) throws IOException, WriterException { ImageIO.write( toBufferedImage(new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, size, size), icon, iconSize), "JPEG", os); } /** * 创建二维码的BufferedImage图像 * * @param content * 二维码内容 * @param size * 二维码大小 * @return image * @throws IOException * @throws WriterException */ public BufferedImage toBufferedImage(String content, int size) throws IOException, WriterException { return toBufferedImage(new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, size, size), null, 0); } /** * 创建带图标的二维码的BufferedImage图像 * * @param content * 二维码内容 * @param size * 二维码大小 * @param icon * 图标文件 * @param iconSize * 图标大小 * @return image * @throws IOException * @throws WriterException */ public BufferedImage toBufferedImage(String content, int size, File icon, int iconSize) throws IOException, WriterException { return toBufferedImage(new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, size, size), icon, iconSize); } /** * 创建二维码的BufferedImage图像 * * @param matrix * @param icon * 图标文件 * @param iconSize * 图标尺寸 * @return image * @throws IOException */ public BufferedImage toBufferedImage(BitMatrix matrix, File icon, int iconSize) throws IOException { int color = 0xFF000000; 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) ? color : 0xFFFFFFFF); } } Graphics2D graphics = image.createGraphics(); if (icon != null && icon.exists()) { BufferedImage bufferedImage = ImageIO.read(icon); if (iconSize <= 0) { iconSize = 50; } graphics.drawImage(bufferedImage, (image.getWidth() - iconSize) / 2, (image.getHeight() - iconSize) / 2, iconSize, iconSize, null); } graphics.dispose(); image.flush(); return image; }}

转载地址:http://htjml.baihongyu.com/

你可能感兴趣的文章
远程调用WMI安装软件
查看>>
从零开始学习jQuery (七) jQuery动画-让页面动起来!
查看>>
asp.net 操作word
查看>>
SQL Server 权限管理
查看>>
郎意难坚,侬情自热(文/王路)
查看>>
Android SDK Manager 中如果没有相应的镜像ARM XX Image
查看>>
ASP.NET Web API的Controller是如何被创建的?
查看>>
Ant build xml中的各种变量解释
查看>>
labview视频采集IMAdx
查看>>
Android:实现一种浮动选择菜单的效果
查看>>
【转】如何查看linux版本 如何查看LINUX是多少位
查看>>
openwrt-智能路由器hack技术(1)---"DNS劫持"
查看>>
第十二章 数据备份与还原
查看>>
[redis] Redis 配置文件置参数详解
查看>>
Java 多线程程序设计
查看>>
SQL--类型转换
查看>>
VGG_19 train_vali.prototxt file
查看>>
获取文件或是文件夹的大小和占用空间
查看>>
libssh2进行远程运行LINUX命令
查看>>
Android Gson深入分析
查看>>