QRGen+thumbnailator生成包含logo 参考配置说明
以下主要是说明下生成的一些细节,减少使用难度
参考代码
- pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dalong</groupId>
<artifactId>imageapp</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>apache-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.github.kenglxn.qrgen</groupId>
<artifactId>javase</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
</dependencies>
</project>
- 代码
package com.dalong;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import net.coobird.thumbnailator.resizers.configurations.AlphaInterpolation;
import net.glxn.qrgen.core.image.ImageType;
import net.glxn.qrgen.javase.QRCode;
import org.apache.commons.codec.binary.Base64;
import javax.imageio.ImageIO;
import java.io.*;
public class Application {
public static void main(String[] args) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// 符合黑白模式的,推荐withColor 的第一个参数为0xFF000000,这样保障生成的二维码具有比较高的识别度
File file = QRCode.from("https://github.com/rongfengliang").withColor(0xFF000000, 0xf7f7f7).to(ImageType.PNG).withSize(300, 300).file();
// 基于水印模式进行logo的添加,推荐水印配置alphaInterpolation为AlphaInterpolation.QUALITY
Thumbnails.of(file)
.size(300, 300)
.allowOverwrite(true)
.keepAspectRatio(true)
.alphaInterpolation(AlphaInterpolation.QUALITY)
.watermark(Positions.CENTER, ImageIO.read(Application.class.getClassLoader().getResourceAsStream("yyy.png")), 1f)
.toOutputStream(outputStream);
byte[] encodedBytes = Base64.encodeBase64(outputStream.toByteArray());
System.out.println(new String(encodedBytes));
}
}
参考资料
https://github.com/kenglxn/QRGen
https://github.com/coobird/thumbnailator
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2019-10-16 sqlg rdbms 上实现的Apache TinkerPop
2019-10-16 golang 几个好用的cli package
2018-10-16 开源泛域名服务xip.io部署试用
2016-10-16 Netflix Falcor获取JSON数据
2016-10-16 graphql 新API 开发方式
2014-10-16 C# winform javascript 互调用