maven项目引入Google二维码框架zxing实现二维码(支持logo图)
pom依赖:
<!--二维码--> <dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.1.0</version> </dependency> <dependency><groupId>com.google.zxing</groupId><artifactId>javase</artifactId><version>3.1.0</version> </dependency>
代码部分:import java.awt.BasicStroke; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Shape; import java.awt.geom.RoundRectangle2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.OutputStream; import java.util.Hashtable; import java.util.Random;import javax.imageio.ImageIO;import com.google.zxing.BarcodeFormat; import com.google.zxing.BinaryBitmap; import com.google.zxing.DecodeHintType; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatReader; import com.google.zxing.MultiFormatWriter; import com.google.zxing.Result; import com.google.zxing.client.j2se.BufferedImageLuminanceSource; import com.google.zxing.common.BitMatrix; import com.google.zxing.common.HybridBinarizer; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;/*** 二维码工具类**/ public class QRCodeUtil {private static final String CHARSET = "utf-8";private static final String FORMAT = "JPG";// 二维码尺寸private static final int QRCODE_SIZE = 300;// LOGO宽度private static final int LOGO_WIDTH = 60;// LOGO高度private static final int LOGO_HEIGHT = 60;private static BufferedImage createImage(String content, String logoPath, boolean needCompress) throws Exception {Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);hints.put(EncodeHintType.CHARACTER_SET, CHARSET);hints.put(EncodeHintType.MARGIN, 1);BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE,hints);int width = bitMatrix.getWidth();int height = bitMatrix.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, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);}}if (logoPath == null || "".equals(logoPath)) {return image;}// 插入图片QRCodeUtil.insertImage(image, logoPath, needCompress);return image;}/*** 插入LOGO** @param source* 二维码图片* @param logoPath* LOGO图片地址* @param needCompress* 是否压缩* @throws Exception*/private static void insertImage(BufferedImage source, String logoPath, boolean needCompress) throws Exception {File file = new File(logoPath);if (!file.exists()) {throw new Exception("logo file not found.");}Image src = ImageIO.read(new File(logoPath));int width = src.getWidth(null);int height = src.getHeight(null);if (needCompress) { // 压缩LOGOif (width > LOGO_WIDTH) {width = LOGO_WIDTH;}if (height > LOGO_HEIGHT) {height = LOGO_HEIGHT;}Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);Graphics g = tag.getGraphics();g.drawImage(image, 0, 0, null); // 绘制缩小后的图g.dispose();src = image;}// 插入LOGOGraphics2D graph = source.createGraphics();int x = (QRCODE_SIZE - width) / 2;int y = (QRCODE_SIZE - height) / 2;graph.drawImage(src, x, y, width, height, null);Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);graph.setStroke(new BasicStroke(3f));graph.draw(shape);graph.dispose();}/*** 生成二维码(内嵌LOGO)* 二维码文件名随机,文件名可能会有重复** @param content* 内容* @param logoPath* LOGO地址* @param destPath* 存放目录* @param needCompress* 是否压缩LOGO* @throws Exception*/public static String encode(String content, String logoPath, String destPath, boolean needCompress) throws Exception {BufferedImage image = QRCodeUtil.createImage(content, logoPath, needCompress);mkdirs(destPath);String fileName = new Random().nextInt(99999999) + "." + FORMAT.toLowerCase();ImageIO.write(image, FORMAT, new File(destPath + "/" + fileName));return fileName;}/*** 生成二维码(内嵌LOGO)* 调用者指定二维码文件名** @param content* 内容* @param logoPath* LOGO地址* @param destPath* 存放目录* @param fileName* 二维码文件名* @param needCompress* 是否压缩LOGO* @throws Exception*/public static String encode(String content, String logoPath, String destPath, String fileName, boolean needCompress) throws Exception {BufferedImage image = QRCodeUtil.createImage(content, logoPath, needCompress);mkdirs(destPath);fileName = fileName.substring(0, fileName.indexOf(".")>0?fileName.indexOf("."):fileName.length())+ "." + FORMAT.toLowerCase();ImageIO.write(image, FORMAT, new File(destPath + "/" + fileName));return fileName;}/*** 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.* (mkdir如果父目录不存在则会抛出异常)* @param destPath* 存放目录*/public static void mkdirs(String destPath) {File file = new File(destPath);if (!file.exists() && !file.isDirectory()) {file.mkdirs();}}/*** 生成二维码(内嵌LOGO)** @param content* 内容* @param logoPath* LOGO地址* @param destPath* 存储地址* @throws Exception*/public static String encode(String content, String logoPath, String destPath) throws Exception {return QRCodeUtil.encode(content, logoPath, destPath, false);}/*** 生成二维码** @param content* 内容* @param destPath* 存储地址* @param needCompress* 是否压缩LOGO* @throws Exception*/public static String encode(String content, String destPath, boolean needCompress) throws Exception {return QRCodeUtil.encode(content, null, destPath, needCompress);}/*** 生成二维码** @param content* 内容* @param destPath* 存储地址* @throws Exception*/public static String encode(String content, String destPath) throws Exception {return QRCodeUtil.encode(content, null, destPath, false);}/*** 生成二维码(内嵌LOGO)** @param content* 内容* @param logoPath* LOGO地址* @param output* 输出流* @param needCompress* 是否压缩LOGO* @throws Exception*/public static void encode(String content, String logoPath, OutputStream output, boolean needCompress)throws Exception {BufferedImage image = QRCodeUtil.createImage(content, logoPath, needCompress);ImageIO.write(image, FORMAT, output);}/*** 生成二维码** @param content* 内容* @param output* 输出流* @throws Exception*/public static void encode(String content, OutputStream output) throws Exception {QRCodeUtil.encode(content, null, output, false);}/*** 解析二维码** @param file* 二维码图片* @return* @throws Exception*/public static String decode(File file) throws Exception {BufferedImage image;image = ImageIO.read(file);if (image == null) {return null;}BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));Result result;Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();hints.put(DecodeHintType.CHARACTER_SET, CHARSET);result = new MultiFormatReader().decode(bitmap, hints);String resultStr = result.getText();return resultStr;}/*** 解析二维码** @param path* 二维码图片地址* @return* @throws Exception*/public static String decode(String path) throws Exception {return QRCodeUtil.decode(new File(path));}public static void main(String[] args) throws Exception {String text = "http://localhost:8083/communication/distribution?qrid=54999";//不含Logo//QRCodeUtil.encode(text, null, "e:\\", true);//含Logo,不指定二维码图片名//QRCodeUtil.encode(text, "e:\\csdn.jpg", "e:\\", true);//含Logo,指定二维码图片名QRCodeUtil.encode(text, "e:\\error.jpg", "e:\\", "qrcode", true);} }
maven项目引入Google二维码框架zxing实现二维码(支持logo图)相关推荐
- (转)通过maven,给没有pom文件的jar包生成pom文件,maven项目引入本地jar包
文章完全转载自 : https://blog.csdn.net/qq_31289187/article/details/81117478 问题一: 经常遇到公司私服或者中央仓库没有的jar包,然后通过 ...
- maven项目引入三方jar包
Maven项目引入三方jar包 前提: 在项目开发过程中,难免会用到第三方jar的时候,比如:sdk.或者特定的jar包(不在maven仓库中),这种情况下,我们怎么在项目中直接使用,并且使用mave ...
- maven项目引入新依赖问题
title: maven项目引入新依赖问题 tags: Maven categories: 工作日志 date: 2017-05-25 18:18:55 前一篇描述过关于版本号没有写明确,写了rele ...
- 二维码框架Zxing的使用及自定义
项目需要一个二维码扫描功能 关于二维码的扫描,我之前就听说过ZXing了(虽然从来没用过) 所以既然这次要用它了,自然是得搜索一番资料咯. 在开始介绍之前,我先说一下我的使用情况吧,网上的博客里面基本 ...
- maven项目(引入依赖失败, pom.xml 报错\爆红)
☺ 引入项目过程中,idea引入磁盘的某个的maven项目 这里以springboot项目以例子,发现pom.xml 的依赖大面积爆红,springboot的版本依赖也报错了,然后发现下面有进度条在下 ...
- 安卓框架之二维码框架zxing的快速上手
Zxing的使用 导入依赖: compile 'cn.yipianfengye.android:zxing-library:2.2' 申请权限: <!--震动权限--><uses-p ...
- maven项目引入sqljdbc4 找不到包的完美 解决方案。
今天碰到了这个问题,解决了,顺便做一下记录.首先来 重现 一下这个问题,maven install报错,说 找不到这个包,但是其实 我已经安装了. 我们 再来 看看 maven本地仓库里面有 什么,这 ...
- IDEA将maven项目复制成一个新的框架/项目
1.将项目在本地的文件夹(源项目)复制一份并重命名(目标项目) 2.删掉IDEA配置文件以及.git 删掉子项目中的iml文件 3.修改项目中的pom文件 其余子项目中也需要更改 4.使用idea打开 ...
- IDEA Maven项目引入本地外部jar包
Ctrl+shift+alt+S
最新文章
- 十大经典排序算法(建议收藏)
- 解决Missing artifact com.microsoft.sqlserver:sqljdbc4:jar:4.0问题
- 深度学习和目标检测系列教程 7-300:先进的目标检测Faster R-CNN架构
- 音视频技术开发周刊 | 168
- HashMap Hashtable区别
- 关于电脑(window)后门查看的一些总结
- 用pythone画棵圣诞树,祝大家圣诞快乐
- pycharm安装怎么选_安装新风系统,地送风和顶送风哪种?专业师傅分析,不纠结怎么选...
- 夜神模拟器怎么设置android版本号,adb连接夜神模拟器(包括安装adb,夜神模拟器)需要将夜神模拟器的版本号与adb版本一致...
- SCI论文撰写和投稿过程的详细介绍 - 易智编译EaseEditing
- 【1月7日】议程正式公布!年度AIoT产业盛典重磅来袭!
- SQLServer共享锁,更新锁,排它锁,死锁,默认隔离级别
- 海思SD3403开发板学习(一)
- Android硬件通信之 蓝牙通信
- JULIA学习材料合集
- 酷我音乐 linux,GitHub - weakish/kwplayer: 酷我音乐盒的 Gtk/Linux 实现
- plt画网格图_matplotlib子图与多种图形绘制
- C++ 中的万能开头
- mac OSx 打包成dmg文件
- div设置下拉滚动条以及翻滚到底部