Class library for Java image processing: SimpleImage (transfer)

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

[java]   preview copy
 
 
  1. package cn.wuzhuti;  
  2.   
  3. import  java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import org.apache.commons.io.IOUtils;  
  7. import  com.alibaba.simpleimage.ImageRender;  
  8. import com.alibaba.simpleimage.SimpleImageException;  
  9. import com.alibaba.simpleimage.render.ReadRender;  
  10. import  com.alibaba.simpleimage.render.ScaleParameter;  
  11. import  com.alibaba.simpleimage.render.ScaleRender;  
  12. import com.alibaba.simpleimage.render.WriteRender;  
  13.   
  14. publicclass Example {   
  15.   
  16.     publicstaticvoid main(String[] args) {     
  17.         File in =  new  File( "d:/source.jpg" );       //Original image  
  18.         File out = new File("d:/dest.jpg");       //目的图片  
  19.         ScaleParameter scaleParam = new ScaleParameter(10241024);  //将图像缩略到1024x1024以内,不足1024x1024则不做任何处理  
  20.           
  21.         FileInputStream inStream = null;  
  22.         FileOutputStream outStream = null;  
  23.         WriteRender wr = null;  
  24.         try {  
  25.             inStream = new FileInputStream(in);  
  26.             outStream = new FileOutputStream(out);  
  27.             ImageRender rr = new ReadRender(inStream);  
  28.             ImageRender sr = new ScaleRender(rr, scaleParam);  
  29.             wr = new WriteRender(sr, outStream);  
  30.           
  31.             wr.render();                            //触发图像处理  
  32.         } catch(Exception e) {  
  33.             e.printStackTrace();  
  34.         } finally {  
  35.             IOUtils.closeQuietly(inStream);         //图片文件输入输出流必须记得关闭  
  36.             IOUtils.closeQuietly(outStream);  
  37.             if (wr != null) {  
  38.                 try {  
  39.                     wr.dispose();                   //释放simpleImage的内部资源  
  40.                 } catch (SimpleImageException ignore) {  
  41.                     // skip ...   
  42.                 }  
  43.             }  
  44.         }  
  45.     }  
  46. }  

项目地址

依赖jar

commons-io.jar  下载地址

jai_codec.jar 下载地址

jai_core.jar 下载地址

常见问题

1.Linux系统下异常。

项目在windows中使用没有问题,而到Linux系统中,出现:

java.lang.NoClassDefFoundError:

Could not initializeclass sun.awt.image.code.JPEGImageEncoderImpl

这是因为java虚拟机在处理图片时,需要本地的x-server的支持;解决方案如下:

在java启动时加上参数:-Djava.awt.headless=true

例如:我们在启动tomcat时,在启动命令行里面加上这个参数即可。

或者在tomcat的catalina.sh中添加参数:

[shell]  预览 复制
 
 
  1. ...  
  2. # LOGGING_CONFIG (Optional) Override Tomcat's logging config file  
  3. # Example (all one line)  
  4. # LOGGING_CONFIG="-Djava.util.logging.config.file=$CATALINA_BASE/conf/logging.properties"  
  5. #  
  6. # LOGGING_MANAGER (Optional) Override Tomcat's logging manager  
  7. # Example (all one line)  
  8. # LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"  
  9. # -----------------------------------------------------------------------------  
  10.   
  11. JAVA_OPTS= "-Djava.awt.headless=true" #Startup parameters   
  12.   
  13. # OS specific support. $var _must_ be set to either true or false.  
  14. cygwin=false  
  15. darwin=false  
 
top
0
 
step on
0

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326416287&siteId=291194637