diff --git a/ahao-image/pom.xml b/ahao-image/pom.xml index 9422c80e..e85b4256 100644 --- a/ahao-image/pom.xml +++ b/ahao-image/pom.xml @@ -15,27 +15,15 @@ jar - - org.springframework.boot - spring-boot-starter - - - org.projectlombok - lombok - - - org.slf4j - slf4j-api - - - ch.qos.logback - logback-classic - 1.2.10 - org.im4java im4java 1.4.0 + + com.sharneng + gm4java + 1.1.1 + diff --git a/ahao-image/src/test/java/moe/ahao/image/Gm4JavaTest.java b/ahao-image/src/test/java/moe/ahao/image/Gm4JavaTest.java new file mode 100644 index 00000000..a058362e --- /dev/null +++ b/ahao-image/src/test/java/moe/ahao/image/Gm4JavaTest.java @@ -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路径未找到"); + } + } +} diff --git a/ahao-image/src/test/java/moe/ahao/image/ImageTest.java b/ahao-image/src/test/java/moe/ahao/image/Im4JavaTest.java similarity index 74% rename from ahao-image/src/test/java/moe/ahao/image/ImageTest.java rename to ahao-image/src/test/java/moe/ahao/image/Im4JavaTest.java index 72d92f68..2dbce654 100644 --- a/ahao-image/src/test/java/moe/ahao/image/ImageTest.java +++ b/ahao-image/src/test/java/moe/ahao/image/Im4JavaTest.java @@ -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"; @@ -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 @@ -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路径未找到");