`
breezedancer
  • 浏览: 18912 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

JIMI的学习

阅读更多
网上有对他的简介很多,这不废话了,看看是怎么用,根据起自带的一些demo,简单说明下java对图片的处理

下载相应包(JimiProClasses.zip)并加到classpath,是个zip文件;
1.简单处理

java 代码
  1. import com.sun.jimi.core.*;   
  2. import java.awt.Image;   
  3.   
  4. public class JimiIsReallyEasy   
  5. {   
  6.  public static void main(String[] args) throws JimiException   
  7.  {   
  8.   if (args.length < 2) {   
  9.    System.err.println("Usage: JimiIsReallyEasy <source></source> <dest></dest>");   
  10.   }   
  11.   else {   
  12.    Image image = Jimi.getImage(args[0]);   
  13.    Jimi.putImage(image, args[1]);   
  14.    System.exit(0);   
  15.   }   
  16.  }   
  17. }   
  18.   

这样处理的结果图片变小了,效果却还可以,肉眼感觉不出

============================================================================================================================================


2. ColorReduce对图片颜色的降低处理

1)先准备一张图片,方便测试,放在与java文件同目录下
2)写代码ColorReduceTest.java
这里主要用到com.sun.jimi.core.util.ColorReducer这个类,在起demo中有错误,多了个util,去了就可以了;

java 代码
  1. import com.sun.jimi.core.*;//有相关的异常类,Jimi类,以及相应的JimiReader,JimiWriter   
  2. import com.sun.jimi.core.util.ColorReducer;   
  3. import java.awt.*;   
  4.   
  5. public class ColorReduceTest   
  6. {   
  7.  static Image image = null;   
  8.   
  9.  public static void main(String[] args) throws JimiException   
  10.  {   
  11.   if (args.length < 3) {   
  12.    System.err.println("Usage: ColorReduceWithJimi <source></source> <dest></dest> <# colors>");   
  13.    System.exit(1);   
  14.   }   
  15.   image = Jimi.getImage(args[0]);//装载图片   
  16.   image = reduceColors(image, Integer.parseInt(args[2]), true);//核心处理   
  17.   Jimi.putImage(image, args[1]);//保存图片   
  18.   System.exit(0);   
  19.  }   
  20.     
  21.  public static Image reduceColors(Image image, int colors, boolean dither)   
  22.  {   
  23.   ColorReducer reducer = new ColorReducer(colors, dither);//dither是慌乱,颤抖的意思   
  24.   Image img = null;   
  25.   try {   
  26.     img = reducer.getColorReducedImage(image);   
  27.   }   
  28.   catch (JimiException e) {   
  29.    System.out.println("Error color reducing/dithering");   
  30.   }   
  31.   return img;   
  32.  }   
  33. }   
  34.   

////////////////////////////
ColorReducer 方法的介绍
2个构造方法
ColorReducer
public ColorReducer(int maxColors)Creates a ColorReducer to perform color reduction on an Image without any dithering.
Parameters:
maxColors - the maximum number of colors in the reduced image
--------------------------------------------------------------------------------
ColorReducer
public ColorReducer(int maxColors,
                    boolean dither)Creates a ColorReducer to perform color reduction on an Image.
Parameters:
maxColors - the maximum number of colors in the reduced image
dither - true if the image should be dithered to create smoother results

其他方法
getColorReducedImageProducer
public java.awt.image.ImageProducer getColorReducedImageProducer(java.awt.image.ImageProducer producer)
                                                          throws JimiException
Perform color reduction.
Parameters:
producer - the ImageProducer to draw image data from
--------------------------------------------------------------------------------
getColorReducedImageProducer
public java.awt.image.ImageProducer getColorReducedImageProducer(java.awt.Image image)
                                                          throws JimiException
Perform color reduction.
Parameters:
image - the Image to draw image data from
-------------------------------------------------------------------------------
getColorReducedImage
public java.awt.Image getColorReducedImage(java.awt.image.ImageProducer producer)
                                    throws JimiException
Perform color reduction.
Parameters:
producer - the ImageProducer to draw image data from
--------------------------------------------------------------------------------
getColorReducedImage
public java.awt.Image getColorReducedImage(java.awt.Image image)
                                    throws JimiException
Perform color reduction.
Parameters:
image - the Image to draw image data from
--------------------------------------------------------------------------------
doColorReduction
protected JimiRasterImage doColorReduction(java.awt.image.ImageProducer producer)
                                    throws JimiException

#完#
---------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------


二、
把gif图片处理成png

1.准备图片
2.编写代码

java 代码
  1. import com.sun.jimi.core.Jimi;   
  2. import com.sun.jimi.core.JimiException;   
  3. import com.sun.jimi.core.JimiWriter;   
  4. import com.sun.jimi.core.options.PNGOptions;//注意这里,转成其他格式也可以   
  5.   
  6. import java.awt.image.ImageProducer;   
  7.   
  8. /**  
  9.  * Simple class for saving PNG files with or without compression  
  10.  * as a demonstration of format-specific option usage.  
  11.  */  
  12. public class CustomOption   
  13. {   
  14.  public static void main(String[] args)   
  15.  {   
  16.   if (args.length < 3) {   
  17.    System.err.println("Convert a file to PNG with compression on or off.");   
  18.    System.err.println("Usage: CustomOption <source></source> <dest></dest> ");   
  19.    System.exit(1);   
  20.   }   
  21.   String source = args[0];   
  22.   String dest = args[1];   
  23.   String compression = args[2];//压缩情况   
  24.   
  25.   // only handle PNG files   
  26.              // 如果存为非png的格式,则自动加png格式结尾   
  27.   if (!dest.endsWith("png")) {   
  28.    dest += ".png";   
  29.    System.out.println("Overriding to PNG, output file: " + dest);   
  30.   }   
  31.   
  32.   try {   
  33.    PNGOptions options = new PNGOptions();   
  34.    if (compression.equals("max")) { //对压缩情况处理   
  35.     options.setCompressionType(PNGOptions.COMPRESSION_MAX);   
  36.    }   
  37.    else if (compression.equals("none")) {   
  38.     options.setCompressionType(PNGOptions.COMPRESSION_NONE);   
  39.    }   
  40.    else {   
  41.     options.setCompressionType(PNGOptions.COMPRESSION_DEFAULT);   
  42.    }   
  43.    ImageProducer image = Jimi.getImageProducer(args[0]); //Get an ImageProducer for an image.   
  44.    JimiWriter writer = Jimi.createJimiWriter(dest); // Create a JimiWriter to control encoding of an image                  writer.setSource(image); //Set the source image to be written.   
  45.    writer.setOptions(options); //Set the options to use for encoding.   
  46.    writer.putImage(dest); //Write the source to a file.   
  47.   }   
  48.   catch (JimiException je) {   
  49.    System.err.println("Error: " + je);   
  50.   }   
  51.  }   
  52. }   
  53.   

经过max压缩,文件缩小了些,清晰度却肉眼没有感觉到变化
更多说明请看api
=============================================================================================================================================

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics