当前位置: 首页 > news >正文

java生成带logo的二维码

随着微信在市场上的占有率不对升高,二维码开始逐渐进入人们的视野,扫码支付,扫码关注,扫码打开连接...

究竟二维码是个什么东西我就不在这里赘述了,关于这方面大家可以去上搜索引擎

我在这里就简单介绍一下一个Java的生成二维码工具,是Google提供的

上代码,

  1 package com.xxxx.xx;
  2 
  3 import java.awt.Color;
  4 import java.awt.Font;
  5 import java.awt.Graphics2D;
  6 import java.awt.image.BufferedImage;
  7 import java.io.File;
  8 import java.util.HashMap;
  9 import java.util.Map;
 10 import java.util.Objects;
 11 
 12 import javax.imageio.ImageIO;
 13 
 14 import org.apache.commons.lang3.StringUtils;
 15 
 16 import com.google.zxing.BarcodeFormat;
 17 import com.google.zxing.EncodeHintType;
 18 import com.google.zxing.MultiFormatWriter;
 19 import com.google.zxing.WriterException;
 20 import com.google.zxing.common.BitMatrix;
 21 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
 22 
 23 /**
 24  * 画制定logo和制定描述的二维码
 25  * 
 26  * @author songyz
 27  *
 28  */
 29 public class ZXingCode {
 30     private static final int QRCOLOR = 0xFF000000; // 默认是黑色
 31     private static final int BGWHITE = 0xFFFFFFFF; // 背景颜色
 32 
 33     private static final int WIDTH = 400; // 二维码宽
 34     private static final int HEIGHT = 400; // 二维码高
 35 
 36     // 用于设置QR二维码参数
 37     private static Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>() {
 38         private static final long serialVersionUID = 1L;
 39         {
 40             put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);// 设置QR二维码的纠错级别(H为最高级别)具体级别信息
 41             put(EncodeHintType.CHARACTER_SET, "utf-8");// 设置编码方式
 42             put(EncodeHintType.MARGIN, 0);
 43         }
 44     };
 45 
 46     public static void main(String[] args) throws WriterException {
 47         File logoFile = new File("D://QrCode/logo3.png");
 48         File QrCodeFile = new File("D://QrCode/05.png");
 49         String url = "https://www.baidu.com/";
 50         String note = "访问百度连接";
 51         drawLogoQRCode(logoFile, QrCodeFile, url, note);
 52     }
 53 
 54     // 生成带logo的二维码图片
 55     public static void drawLogoQRCode(File logoFile, File codeFile, String qrUrl, String note) {
 56         try {
 57             MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
 58             // 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数
 59             BitMatrix bm = multiFormatWriter.encode(qrUrl, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
 60             BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
 61 
 62             // 开始利用二维码数据创建Bitmap图片,分别设为黑(0xFFFFFFFF)白(0xFF000000)两色
 63             for (int x = 0; x < WIDTH; x++) {
 64                 for (int y = 0; y < HEIGHT; y++) {
 65                     image.setRGB(x, y, bm.get(x, y) ? QRCOLOR : BGWHITE);
 66                 }
 67             }
 68 
 69             int width = image.getWidth();
 70             int height = image.getHeight();
 71             if (Objects.nonNull(logoFile) && logoFile.exists()) {
 72                 // 构建绘图对象
 73                 Graphics2D g = image.createGraphics();
 74                 // 读取Logo图片
 75                 BufferedImage logo = ImageIO.read(logoFile);
 76                 // 开始绘制logo图片
 77                 g.drawImage(logo, width * 2 / 5, height * 2 / 5, width * 2 / 10, height * 2 / 10, null);
 78                 g.dispose();
 79                 logo.flush();
 80             }
 81 
 82             // 自定义文本描述
 83             if (StringUtils.isNotEmpty(note)) {
 84                 // 新的图片,把带logo的二维码下面加上文字
 85                 BufferedImage outImage = new BufferedImage(400, 445, BufferedImage.TYPE_4BYTE_ABGR);
 86                 Graphics2D outg = outImage.createGraphics();
 87                 // 画二维码到新的面板
 88                 outg.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
 89                 // 画文字到新的面板
 90                 outg.setColor(Color.BLACK);
 91                 outg.setFont(new Font("楷体", Font.BOLD, 30)); // 字体、字型、字号
 92                 int strWidth = outg.getFontMetrics().stringWidth(note);
 93                 if (strWidth > 399) {
 94                     // //长度过长就截取前面部分
 95                     // 长度过长就换行
 96                     String note1 = note.substring(0, note.length() / 2);
 97                     String note2 = note.substring(note.length() / 2, note.length());
 98                     int strWidth1 = outg.getFontMetrics().stringWidth(note1);
 99                     int strWidth2 = outg.getFontMetrics().stringWidth(note2);
100                     outg.drawString(note1, 200 - strWidth1 / 2, height + (outImage.getHeight() - height) / 2 + 12);
101                     BufferedImage outImage2 = new BufferedImage(400, 485, BufferedImage.TYPE_4BYTE_ABGR);
102                     Graphics2D outg2 = outImage2.createGraphics();
103                     outg2.drawImage(outImage, 0, 0, outImage.getWidth(), outImage.getHeight(), null);
104                     outg2.setColor(Color.BLACK);
105                     outg2.setFont(new Font("宋体", Font.BOLD, 30)); // 字体、字型、字号
106                     outg2.drawString(note2, 200 - strWidth2 / 2,outImage.getHeight() + (outImage2.getHeight() - outImage.getHeight()) / 2 + 5);
107                     outg2.dispose();
108                     outImage2.flush();
109                     outImage = outImage2;
110                 } else {
111                     outg.drawString(note, 200 - strWidth / 2, height + (outImage.getHeight() - height) / 2 + 12); // 画文字
112                 }
113                 outg.dispose();
114                 outImage.flush();
115                 image = outImage;
116             }
117 
118             image.flush();
119 
120             ImageIO.write(image, "png", codeFile); // TODO
121         } catch (Exception e) {
122             e.printStackTrace();
123         }
124     }
125 
126 }

相关文章:

  • RK平台UVC摄像头shell测试脚本
  • Kafka 0.11.0.2 安装备忘录
  • Radxa Rock 3a NPU调用指南
  • Java 线程中断、线程让步、线程睡眠、线程合并
  • Java笔记15 - 面向对象
  • XSS脚本攻击防御(Antisamy)
  • Session的原理分析
  • 设计模式--单例模式(懒汉、饿汉)
  • 20个Java小项目,献给嗜学如狂的人,拿来练练手
  • GFS文件分布式系统概述与部署
  • zabbix监控基本概念和部署
  • 灵性图书馆:好书推荐-《情绪的惊人力量》
  • Google Earth Engine(GEE)——MODIS/061/MOD09GA影像计算NDVI并导出结果并UI可视化批量导出(含错误提示)
  • 【MC教程】iPad启动Java版mc(无需越狱)(保姆级?) Jitterbug启动iOS我的世界Java版启动器 PojavLauncher
  • 记SpringBoot拦截器报错getWriter() has already been called for this response
  • JavaScript 一些 DOM 的知识点
  • Linux中的硬链接与软链接
  • Node + FFmpeg 实现Canvas动画导出视频
  • php面试题 汇集2
  • Selenium实战教程系列(二)---元素定位
  • Sublime Text 2/3 绑定Eclipse快捷键
  • Web设计流程优化:网页效果图设计新思路
  • 安装python包到指定虚拟环境
  • 干货 | 以太坊Mist负责人教你建立无服务器应用
  • 记录一下第一次使用npm
  • 前端
  • 如何在GitHub上创建个人博客
  • 要让cordova项目适配iphoneX + ios11.4,总共要几步?三步
  • 一道面试题引发的“血案”
  • 移动端唤起键盘时取消position:fixed定位
  • 原生 js 实现移动端 Touch 滑动反弹
  • 格斗健身潮牌24KiCK获近千万Pre-A轮融资,用户留存高达9个月 ...
  • 容器镜像
  • ​业务双活的数据切换思路设计(下)
  • #[Composer学习笔记]Part1:安装composer并通过composer创建一个项目
  • #1014 : Trie树
  • #考研#计算机文化知识1(局域网及网络互联)
  • ${factoryList }后面有空格不影响
  • (2)(2.10) LTM telemetry
  • (function(){})()的分步解析
  • (笔试题)分解质因式
  • (附表设计)不是我吹!超级全面的权限系统设计方案面世了
  • (附源码)springboot青少年公共卫生教育平台 毕业设计 643214
  • (生成器)yield与(迭代器)generator
  • (已更新)关于Visual Studio 2019安装时VS installer无法下载文件,进度条为0,显示网络有问题的解决办法
  • (转)机器学习的数学基础(1)--Dirichlet分布
  • .NET C#版本和.NET版本以及VS版本的对应关系
  • .Net Core 中间件验签
  • .NET MVC 验证码
  • .NET Standard、.NET Framework 、.NET Core三者的关系与区别?
  • .NET 表达式计算:Expression Evaluator
  • .NET/C# 使窗口永不获得焦点
  • .pings勒索病毒的威胁:如何应对.pings勒索病毒的突袭?
  • .sys文件乱码_python vscode输出乱码
  • :“Failed to access IIS metabase”解决方法