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

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


相关文章
|
前端开发
node express 给前端返回图片流
node express 给前端返回图片流
node express 给前端返回图片流
|
9月前
|
人工智能 Linux API
Omnitool:开发者桌面革命!开源神器一键整合ChatGPT+Stable Diffusion等主流AI平台,本地运行不联网
Omnitool 是一款开源的 AI 桌面环境,支持本地运行,提供统一交互界面,快速接入 OpenAI、Stable Diffusion、Hugging Face 等主流 AI 平台,具备高度扩展性。
999 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标签的用法
|
机器学习/深度学习 PyTorch TensorFlow
使用Python实现智能食品质量检测的深度学习模型
使用Python实现智能食品质量检测的深度学习模型
540 1
|
开发者 Python
手把手教你申请软件著作权(已下证 带模板)
手把手教你申请软件著作权(已下证 带模板)
|
存储 安全 Linux
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: ```
2021 7
|
Java 数据处理
Java8的新特性parallelStream()的概念、对比线程优势与实战
parallelStream() 是 Java 8 中新增的一个方法,它是 Stream 类的一种扩展,提供了将集合数据并行处理的能力。普通的 stream() 方法是使用单线程对集合数据进行顺序处理,而 parallelStream() 方法则可以将集合数据分成多个小块,分配到多个线程并行处理,从而提高程序的执行效率。
948 3
|
机器学习/深度学习 算法 Python
深入理解Python中的集成方法:Boosting
深入理解Python中的集成方法:Boosting
405 1
nodejs17/18版本报错:digital envelope routines::unsupported
nodejs17/18版本报错:digital envelope routines::unsupported
463 0
|
关系型数据库 MySQL
MySQL的不同字符集的排序规则
不同字符集在MySQL中使用不同的排序规则,确定了对字符数据的排序和比较方式。下面是一些常用字符集的排序规则示例: 1. UTF-8字符集: - utf8_bin:基于二进制比较,区分大小写。 - utf8_general_ci:大小写不敏感,根据字符的Unicode值进行排序,对于大多数应用来说是足够的。 2. Latin1字符集: - latin1_bin:基于二进制比较,区分大小写。 - latin1_general_ci:大小写不敏感,根据字符的字典顺序进行排序。 3. GBK字符集: - gbk_bin:基于二进制比较,区分大小写。 - gb
411 0