GraphicsMagick + IM4Java use in the project summary

A) Introduction:
a server-side revision image upload logic, the compressed file itself into 3 parts and then uploaded to a cloud storage.
Select GraphicsMagick + IM4Java, give up weak concurrency ImageMagick + Jmagick.
Learn GraphicsMagick available online and in IM4Java is too little, over and over again just so few, can only be met over and over again abnormal reading official documents! This is what I wrote this article one of the reasons.

im4java References:

1)请详见Developer's Guide: http://im4java.sourceforge.net/docs/dev-guide.html

2)github上im4java的源码:https://github.com/Widen/im4java

GraphicsMagick References:

1)http://www.graphicsmagick.org   

Install locally using the test, the performance still feel OK, then, it is placed on the test server deployment test the actual performance. The local environment is a win, GM (GraphicsMagick) win under very good installation, software installation is no different from ordinary.

B) the use IM4Java:

Look in the Developer's Guide "Before you begin: Setting up the Environment" content, the effect is to set the tool before using tool (of course means compressing images, called gm) search path, and describes these types of set ways of difference.

Such as the installation path of the win, if I install path D: \ GraphicsMagick-1.3.19-Q16, then the code is:

String myPath="D:\\GraphicsMagick-1.3.19-Q16";

ProcessStarter.setGlobalSearchPath(myPath);   

注:我们不能在linux服务器中这么写代码,linux直接就设置环境变量:export PATH=$PATH:/abc...,其中/abc是你的GM工具所在路径,比如我的是/home/db/roderickyu/soft/bin

Simple usage directly look at the document, I wrote here a few content documentation is not easy to read out:

压缩时可以直接添加本地的图片,比如:
IMOperation op = new IMOperation();
op.addImage("D:\\test.jpg");     // 输入要压缩的文件路径
op.resize(640);                  // 多番尝试后才知道这是限定width,height等比缩放
op.addImage("D:\\newTest.jpg");  // 压缩后的文件的输出路径,当然可以没有扩展名!

还有一种是对输入流进行压缩,比如官方文档中:
IMOperation op = new IMOperation();
op.addImage("-");                   // read from stdin
op.addImage("tif:-");               // write to stdout in tif-format
代码中tif是指按照tif格式进行压缩,但是变化不明显(而且压缩后的大小与size大小不成正比),很容易给初学者造成是不是代码不好使的挫败感!将tif换成jpg压缩的大小变化就明显了。这是一个坑!

回到这个需求来,我们只有一个输入流,怎么把这个输入流压缩成3份大小不等的文件呢?
将输入流转化成byte数组,就不存在流被第一个文件读完第二三的文件没有流可以读时报异常。。。empty input file...
代码为:
InputStream fileStream = ......
int total = fileStream.available();
byte[] bs = new byte[total];
fileStream.read(bs);

GM failed to install the official documentation in accordance with GM, after the solution was: Path in the read-write permissions to install the current path is not a linux user, can read and write into the path of the current status of chant or change login identity chant ~

Finally tested the performance is quite good!

Reproduced in: https: //www.cnblogs.com/xu-thinking/p/3660779.html

Guess you like

Origin blog.csdn.net/weixin_33755847/article/details/93374840