JAVA生成二维码(zxing)
2016-01-16 11:31
561 查看
上一篇博客中介绍了条码的使用示例,这一篇继续介绍如何使用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; } }
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树