Skip to content

Commit

Permalink
✨ imagemagick图片处理
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahaochan committed Sep 11, 2023
1 parent 6e94368 commit 4b4cd5a
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 35 deletions.
22 changes: 5 additions & 17 deletions ahao-image/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,15 @@
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.10</version>
</dependency>
<dependency>
<groupId>org.im4java</groupId>
<artifactId>im4java</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>com.sharneng</groupId>
<artifactId>gm4java</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
</project>
93 changes: 93 additions & 0 deletions ahao-image/src/test/java/moe/ahao/image/Gm4JavaTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package moe.ahao.image;

import org.gm4java.engine.GMService;
import org.gm4java.engine.support.GMConnectionPoolConfig;
import org.gm4java.engine.support.PooledGMService;
import org.gm4java.im4java.GMBatchCommand;
import org.im4java.core.IMOperation;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import java.io.File;
import java.io.FileNotFoundException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Gm4JavaTest {
public static final String SOURCE = File.separator + "img" + File.separator + "source.jpg";
public static final String TARGET = File.separator + "img" + File.separator + "%s.jpg";

@BeforeAll
static void beforeAll() {
// 设置gm的命令路径
}

@Test
@DisplayName("压缩图片")
@ParameterizedTest
@CsvSource({"1,1", "1,10", "1,20", "1,50", "1,100",
"10,10", "10,20", "10,50", "10,100",
"20,20", "20,50", "20,100",
"50,50", "50,100",
"100,100"})
void compressImage(int threadCount, int taskCount, TestInfo testInfo) throws Exception {
GMConnectionPoolConfig config = new GMConnectionPoolConfig();
config.setGMPath("D:/GraphicsMagick-1.3.41-Q16/gm");
GMService gmService = new PooledGMService(config);


ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
CountDownLatch latch = new CountDownLatch(taskCount);

for (int i = 0; i < taskCount; i++) {
int finalI = i;
executorService.submit(() -> {
try {
// 1. 设置图片信息
String methodName = testInfo.getTestMethod().map(Method::getName).orElse("");
String target = String.format(TARGET, methodName + finalI);

// 2. 设置处理图片参数
Double quality = 50d; // 图片压缩比,有效值范围是0.0-100.0,数值越大,缩略图越清晰。

// 3. 处理图片
IMOperation op = new IMOperation();
op.addImage(getPath(SOURCE));
op.quality(quality);
op.addImage(getPath(target));
GMBatchCommand command = new GMBatchCommand(gmService, "convert");
command.run(op);
// System.out.println("压缩图片成功");
} catch (Exception e) {
e.printStackTrace();
} finally {
latch.countDown();
}
});
}

latch.await();
System.out.println("执行完毕");
}

private String getPath(String path) throws FileNotFoundException {
ClassLoader classLoader = Im4JavaTest.class.getClassLoader();
URL resourceURL = classLoader.getResource("");

if (resourceURL != null) {
// 从URL中获取文件的绝对路径
String absolutePath = new File(resourceURL.getFile()).getAbsolutePath() + path;
// System.out.println("绝对路径:" + absolutePath);
return absolutePath;
} else {
throw new FileNotFoundException("classpath路径未找到");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import java.io.File;
import java.io.FileNotFoundException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ImageTest {
public class Im4JavaTest {
public static final String SOURCE = File.separator + "img" + File.separator + "source.jpg";
public static final String TARGET = File.separator + "img" + File.separator + "%s.jpg";

Expand Down Expand Up @@ -96,22 +101,45 @@ void zoomImage(TestInfo testInfo) throws Exception {

@Test
@DisplayName("压缩图片")
void compressImage(TestInfo testInfo) throws Exception {
// 1. 设置图片信息
String methodName = testInfo.getTestMethod().map(Method::getName).orElse("");
String target = String.format(TARGET, methodName);

// 2. 设置处理图片参数
Double quality = 50d; // 图片压缩比,有效值范围是0.0-100.0,数值越大,缩略图越清晰。
@ParameterizedTest
@CsvSource({ "1,1", "1,10", "1,20", "1,50", "1,100",
"10,10", "10,20", "10,50", "10,100",
"20,20", "20,50", "20,100",
"50,50", "50,100",
"100,100" })
void compressImage(int threadCount, int taskCount, TestInfo testInfo) throws Exception {
ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
CountDownLatch latch = new CountDownLatch(taskCount);

for (int i = 0; i < taskCount; i++) {
int finalI = i;
executorService.submit(() -> {
try {
// 1. 设置图片信息
String methodName = testInfo.getTestMethod().map(Method::getName).orElse("");
String target = String.format(TARGET, methodName + finalI);

// 2. 设置处理图片参数
Double quality = 50d; // 图片压缩比,有效值范围是0.0-100.0,数值越大,缩略图越清晰。

// 3. 处理图片
IMOperation op = new IMOperation();
op.addImage(getPath(SOURCE));
op.quality(quality);
op.addImage(getPath(target));
ImageCommand convert = new ConvertCmd(useGM);
convert.run(op);
// System.out.println("压缩图片成功");
} catch (Exception e) {
e.printStackTrace();
} finally {
latch.countDown();
}
});
}

// 3. 处理图片
IMOperation op = new IMOperation();
op.addImage(this.getPath(SOURCE));
op.quality(quality);
op.addImage(this.getPath(target));
ImageCommand convert = new ConvertCmd(useGM);
convert.run(op);
System.out.println("压缩图片成功");
latch.await();
System.out.println("执行完毕");
}

@Test
Expand Down Expand Up @@ -171,13 +199,13 @@ void compositeImage(TestInfo testInfo) throws Exception {
}

private String getPath(String path) throws FileNotFoundException {
ClassLoader classLoader = ImageTest.class.getClassLoader();
ClassLoader classLoader = Im4JavaTest.class.getClassLoader();
URL resourceURL = classLoader.getResource("");

if (resourceURL != null) {
// 从URL中获取文件的绝对路径
String absolutePath = new File(resourceURL.getFile()).getAbsolutePath() + path;
System.out.println("绝对路径:" + absolutePath);
// System.out.println("绝对路径:" + absolutePath);
return absolutePath;
} else {
throw new FileNotFoundException("classpath路径未找到");
Expand Down

0 comments on commit 4b4cd5a

Please sign in to comment.