码迷,mamicode.com
首页 > 其他好文 > 详细

QRGen+thumbnailator生成包含logo 参考配置说明

时间:2020-10-18 16:18:40      阅读:18      评论:0      收藏:0      [点我收藏+]

标签:细节   depend   rip   data   container   except   efault   alpha   nbsp   

以下主要是说明下生成的一些细节,减少使用难度

参考代码

  • 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

QRGen+thumbnailator生成包含logo 参考配置说明

标签:细节   depend   rip   data   container   except   efault   alpha   nbsp   

原文地址:https://www.cnblogs.com/rongfengliang/p/13825820.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!