introduce
Processing graphics under Java is a very troublesome job. In this regard, although Java provides JAI/ImageIO API components to allow programmers to complete a large number of operations, due to some inherent defects, the stability of the application/common functions are greatly affected. Impact. In addition, some graphics libraries made by C/C++ have various problems, such as the Java API JImageMagic encapsulated by them, and a slightly higher pressure cluster may cause several crashes per day.
SimpleImage is a Java image processing class library of Alibaba , which can realize image thumbnail, watermark and other processing.
ImageRender in SimpleImage is the base class for image processing. It is an abstract class. As we can see, an abstract method render() is defined in this class, and it also holds a reference to the ImageRender class.
ReadRedner can be understood as a component, not a decorator, because ReadRender is the first step in all rendering operations. Other subclasses DrawTextRender (watermark processing), ScaleRender (abbreviation processing), WriterRender (output) are all decorators.
Example of use
- package cn.wuzhuti;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import org.apache.commons.io.IOUtils;
- import com.alibaba.simpleimage.ImageRender;
- import com.alibaba.simpleimage.SimpleImageException;
- import com.alibaba.simpleimage.render.ReadRender;
- import com.alibaba.simpleimage.render.ScaleParameter;
- import com.alibaba.simpleimage.render.ScaleRender;
- import com.alibaba.simpleimage.render.WriteRender;
- publicclass Example {
- publicstaticvoid main(String[] args) {
- File in = new File( "d:/source.jpg" ); //Original image
- File out = new File("d:/dest.jpg"); //目的图片
- ScaleParameter scaleParam = new ScaleParameter(1024, 1024); //将图像缩略到1024x1024以内,不足1024x1024则不做任何处理
- FileInputStream inStream = null;
- FileOutputStream outStream = null;
- WriteRender wr = null;
- try {
- inStream = new FileInputStream(in);
- outStream = new FileOutputStream(out);
- ImageRender rr = new ReadRender(inStream);
- ImageRender sr = new ScaleRender(rr, scaleParam);
- wr = new WriteRender(sr, outStream);
- wr.render(); //触发图像处理
- } catch(Exception e) {
- e.printStackTrace();
- } finally {
- IOUtils.closeQuietly(inStream); //图片文件输入输出流必须记得关闭
- IOUtils.closeQuietly(outStream);
- if (wr != null) {
- try {
- wr.dispose(); //释放simpleImage的内部资源
- } catch (SimpleImageException ignore) {
- // skip ...
- }
- }
- }
- }
- }
项目地址
依赖jar
commons-io.jar 下载地址
jai_codec.jar 下载地址
jai_core.jar 下载地址
常见问题
1.Linux系统下异常。
项目在windows中使用没有问题,而到Linux系统中,出现:
java.lang.NoClassDefFoundError:
Could not initialize
class sun.awt.image.code.JPEGImageEncoderImpl
这是因为java虚拟机在处理图片时,需要本地的x-server的支持;解决方案如下:
在java启动时加上参数:-Djava.awt.headless=true
例如:我们在启动tomcat时,在启动命令行里面加上这个参数即可。
或者在tomcat的catalina.sh中添加参数:
- ...
- # LOGGING_CONFIG (Optional) Override Tomcat's logging config file
- # Example (all one line)
- # LOGGING_CONFIG="-Djava.util.logging.config.file=$CATALINA_BASE/conf/logging.properties"
- #
- # LOGGING_MANAGER (Optional) Override Tomcat's logging manager
- # Example (all one line)
- # LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"
- # -----------------------------------------------------------------------------
- JAVA_OPTS= "-Djava.awt.headless=true" #Startup parameters
- # OS specific support. $var _must_ be set to either true or false.
- cygwin=false
- darwin=false