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

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


相关文章
无法执行 GraphicsMagick/ImageMagick #14
无法执行 GraphicsMagick/ImageMagick #14
68 0
|
自然语言处理 关系型数据库 Java
sphinx(二)centos7编译安装coreseek3.2
sphinx是不支持中文分词的,所以,百度上给的大多数的结果是基于sphinx内核开发的coreseek+mmseg分词的一套组合来实现中文分词+全文检索。
250 0
sphinx(二)centos7编译安装coreseek3.2
|
Apache
ImageMagick
ImageMagick
322 0
|
JavaScript Linux 前端开发
|
Java 应用服务中间件 Windows