Skip to content

分享海报制作图片处理:使用Java Graphics2D及thumbnails工具包对分享海报中图片进行旋转、裁剪操作

Notifications You must be signed in to change notification settings

ChenJFMorton/graphics-thumbnails-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

graphics-thumbnails-demo

1、功能

分享海报制作图片处理:使用Java Graphics2D及thumbnails工具包对分享海报中图片进行旋转、裁剪操作

2、效果图一览

2.1、方形图片旋转效果


原图:

效果图:

2.2、方形图片裁剪成圆图效果

原图:

效果图:

3、使用方法

3.1、环境要求

jdk1.8+

maven依赖

<!--web应用基本环境配置 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.4.0</version>
    </dependency>

    <!-- lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.16</version>
    </dependency>

    <!-- 图片处理工具包 -->
    <dependency>
        <groupId>net.coobird</groupId>
        <artifactId>thumbnailator</artifactId>
        <version>0.4.17</version>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.6</version>
    </dependency>

    <!-- 工具包(https://hutool.cn/) -->
    <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>5.8.5</version>
    </dependency>
    <dependency>
        <groupId>com.google.zxing</groupId>
        <artifactId>core</artifactId>
        <version>3.3.0</version>
    </dependency>

3.2、如何使用

public static void main(String[] args) throws IOException {
		// 二维码生成
		File qrCodeFile = QrCodeUtil.generate("https://chenjfmorton.github.io/", 192, 192, FileUtil.file("./qrcode.jpg"));
		byte[] wxCode = Files.readAllBytes(qrCodeFile.toPath());


		ShareContext ctx = new ShareContext();
		ctx.setIsRound(true); // 方形图片旋转还是方形图片裁剪成圆形
		ctx.setBgImgPath(ctx.getIsRound() ? "image/bg-round.png":"image/bg-rotate.png");

		DrawComponent shareComponent = new ShareComponent(ctx, wxCode);
		// 画图
		BufferedImage draw = shareComponent.draw();


		Thumbnails.of(draw)
				.size(draw.getWidth(),draw.getHeight())
				.outputQuality(1)
				.toFile("./result.png");
	}

通过com/cjf/draw/component/ShareComponent.java文件中main来进行分享图生成,代码中的坐标、长宽根据具体要求来进行配置,达到你需要的效果!

About

分享海报制作图片处理:使用Java Graphics2D及thumbnails工具包对分享海报中图片进行旋转、裁剪操作

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages