GraphicsMagick使用练习

简介:

GraphicsMagick官网地址:http://www.graphicsmagick.org/,介绍不多说网上都有,我这里只使用了它图片缩放功能,用java调用练习。

第一步:下载安装。

我下载的是:GraphicsMagick-1.3.20-Q8-win32-dll.exe,安装目录:C:\Program Files (x86)\GraphicsMagick-1.3.20-Q8。安装完毕后,安装目录自动配置到环境变量的Path中了,安装目录里有gm.exe。测试一下,gm -version,如果打印出GraphicsMagick相关信息则ok。这时就可以在命令行里使用gm命令了。

如果使用java调用报错:Caused by: java.io.IOException: CreateProcess error=2。则表示没有gm命令找不到,也就是说环境变量有可能更改了但是没有生效,我的做法是重启eclipse,运行同样程序就ok。如果还是报错,可能环境变量还是没有生效,重启电脑,又或者是安装目录压根就没有配置到环境变量Path中去,手动配置即可。

第二步:写代码。

依赖:

1

2

3

4

5

<dependency>

<groupId>org.im4java</groupId>

<artifactId>im4java</artifactId>

<version>1.4.0</version>

</dependency>

代码:(api的使用和网上的都不大一样,网上的都是传入2个路径,这里是传入2个字节数组)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

package org.zoo.hippo.utils;

import java.awt.image.BufferedImage;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import javax.imageio.ImageIO;

import org.im4java.core.ConvertCmd;

import org.im4java.core.IMOperation;

import org.im4java.core.Stream2BufferedImage;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

/**

*

* @author yankai913@gmail.com

* @date 2015-1-20

*/

public class ImageTool {

private static final Logger logger = LoggerFactory.getLogger(ImageTool.class);

private static String METHOD_SAMPLE = "sample";

private static String METHOD_RESIZE = "resize";

private static String METHOD_THUMBNAIL = "thumbnail";

private static String METHOD_SCALE = "scale";

private static IMOperation createIMOperation(double quality, String suffix, int width, int height,

String method) {

IMOperation op = new IMOperation();

op.addImage();

if (METHOD_SAMPLE.equals(method)) {

op.sample(width, height);

}

else if (METHOD_RESIZE.equals(method)) {

op.resize(width, height);

}

else if (METHOD_THUMBNAIL.equals(method)) {

op.thumbnail(width, height);

}

else {

op.scale(width, height);

}

op.quality(quality);

op.addImage(new String[] { suffix + ":-" });

return op;

}

public static byte[] sample(byte[] srcData, double quality, String suffix, int width, int height) {

return scale0(srcData, quality, suffix, width, height, METHOD_SAMPLE);

}

public static byte[] resize(byte[] srcData, double quality, String suffix, int width, int height) {

return scale0(srcData, quality, suffix, width, height, METHOD_RESIZE);

}

public static byte[] thumbnail(byte[] srcData, double quality, String suffix, int width, int height) {

return scale0(srcData, quality, suffix, width, height, METHOD_THUMBNAIL);

}

public static byte[] scale(byte[] srcData, double quality, String suffix, int width, int height) {

return scale0(srcData, quality, suffix, width, height, METHOD_SCALE);

}

private static byte[] scale0(byte[] srcData, double quality, String suffix, int width, int height,

String method) {

byte[] result = null;

IMOperation op = createIMOperation(quality, suffix, width, height, method);

try {

ConvertCmd localConvertCmd = new ConvertCmd(true);

long s = System.currentTimeMillis();

ByteArrayInputStream bais = new ByteArrayInputStream(srcData);

BufferedImage inputImage = ImageIO.read(bais);

long s2 = System.currentTimeMillis();

logger.info("inputStream ==> BufferedImage, take " + (s2 - s) + "ms");

Stream2BufferedImage streamBuffered = new Stream2BufferedImage();

localConvertCmd.setOutputConsumer(streamBuffered);

localConvertCmd.run(op, new Object[] { inputImage });

long s3 = System.currentTimeMillis();

logger.info("do convert, take " + (s3 - s2) + "ms");

BufferedImage outputImage = streamBuffered.getImage();

ByteArrayOutputStream baos = new ByteArrayOutputStream();

ImageIO.write(outputImage, suffix, baos);

long s4 = System.currentTimeMillis();

logger.info("BufferedImage ==> outputStream, take " + (s4 - s3) + "ms");

result = baos.toByteArray();

} catch (Exception e) {

logger.error(e.getMessage(), e);

}

return result;

}

}

测试:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

package org.zoo.hippo;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import org.junit.Assert;

import org.junit.Test;

import org.zoo.hippo.utils.ImageTool;

/**

*

* @author yankai913@gmail.com

* @date 2015-1-20

*/

public class ImageToolTest {

@Test

public void test_sample() throws Exception {

FileInputStream fis = new FileInputStream("d:/Lighthouse.jpg");

byte[] srcData = new byte[fis.available()];

fis.read(srcData);

fis.close();

int width = 800;

int height = 800;

String suffix = "jpg";

double quality = 90.0;

byte[] dest = ImageTool.sample(srcData, quality, suffix, width, height);

Assert.assertNotNull(dest);

FileOutputStream fos = new FileOutputStream("d:/Lighthouse2.jpg");

fos.write(dest);

fos.close();

}

}

运行日志输出:
23:07:45.977 [main] INFO org.zoo.hippo.utils.ImageTool – inputStream ==> BufferedImage, take 104ms
23:07:46.313 [main] INFO org.zoo.hippo.utils.ImageTool – do convert, take 338ms
23:07:46.341 [main] INFO org.zoo.hippo.utils.ImageTool – BufferedImage ==> outputStream, take 28ms

到此代码完毕,感兴趣的哥们可以看下。


目录
打赏
0
0
0
0
65
分享
相关文章
深入理解MySQL日志:通用查询、慢查询和错误日志详解
深入理解MySQL日志:通用查询、慢查询和错误日志详解
1763 0
Omnitool:开发者桌面革命!开源神器一键整合ChatGPT+Stable Diffusion等主流AI平台,本地运行不联网
Omnitool 是一款开源的 AI 桌面环境,支持本地运行,提供统一交互界面,快速接入 OpenAI、Stable Diffusion、Hugging Face 等主流 AI 平台,具备高度扩展性。
590 94
Omnitool:开发者桌面革命!开源神器一键整合ChatGPT+Stable Diffusion等主流AI平台,本地运行不联网
mybatis复习03,动态SQL,if,choose,where,set,trim标签及foreach标签的用法
文章介绍了MyBatis中动态SQL的用法,包括if、choose、where、set和trim标签,以及foreach标签的详细使用。通过实际代码示例,展示了如何根据条件动态构建查询、更新和批量插入操作的SQL语句。
mybatis复习03,动态SQL,if,choose,where,set,trim标签及foreach标签的用法
|
7月前
|
Win10系统上直接使用linux子系统教程(仅需五步!超简单,快速上手)
本文介绍了如何在Windows 10上安装并使用Linux子系统。首先,通过应用商店安装Windows Terminal和Linux系统(如Ubuntu)。接着,在控制面板中启用“适用于Linux的Windows子系统”并重启电脑。最后,在Windows Terminal中选择安装的Linux系统即可开始使用。文中还提供了注意事项和进一步配置的链接。
698 0
Vision Mamba:将Mamba应用于计算机视觉任务的新模型
Mamba是LLM的一种新架构,与Transformers等传统模型相比,它能够更有效地处理长序列。就像VIT一样现在已经有人将他应用到了计算机视觉领域,让我们来看看最近的这篇论文“Vision Mamba: Efficient Visual Representation Learning with Bidirectional State Space Models,”
1046 7
使用Python实现智能食品质量检测的深度学习模型
使用Python实现智能食品质量检测的深度学习模型
401 1
s3fs挂载S3对象桶
s3fs(Simple Storage Service File System)是一个基于FUSE(Filesystem in Userspace)的文件系统,它允许将S3(Simple Storage Service)或其他兼容S3 API的对象存储服务挂载到本地文件系统中,从而能够像访问本地磁盘一样访问远程对象存储。以下是通过s3fs挂载OBS(Object Storage Service,对象存储服务,这里以华为云OBS为例)对象桶的基本步骤: ### 一、环境准备 1. **安装s3fs**: - 对于CentOS系统,可以使用yum安装s3fs-fuse: ```
1225 7
Java8的新特性parallelStream()的概念、对比线程优势与实战
parallelStream() 是 Java 8 中新增的一个方法,它是 Stream 类的一种扩展,提供了将集合数据并行处理的能力。普通的 stream() 方法是使用单线程对集合数据进行顺序处理,而 parallelStream() 方法则可以将集合数据分成多个小块,分配到多个线程并行处理,从而提高程序的执行效率。
735 3
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等