---
title: "JIMI的学习"
author: "breezedancer"
date: "2007-05-22T13:11:00Z"
tags: []
categories: ["非技术"]
slug: "jimi"
draft: false
lastmod: "2007-05-22T13:11:00Z"
id: 81803
url: "https://breezedancer.iteye.com/blog/81803"
---

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

下载相应包（JimiProClasses.zip）并加到classpath，是个zip文件；  
1.简单处理

java 代码

01. import com.sun.jimi.core.\*;
02. import java.awt.Image;
03. public class JimiIsReallyEasy
04. {
05. public static void main(String\[] args) throws JimiException
06. {
07. if (args.length &lt; 2) {
08. System.err.println("Usage: JimiIsReallyEasy &lt;source&gt;&lt;/source&gt; &lt;dest&gt;&lt;/dest&gt;");
09. }
10. else {
11. Image image = Jimi.getImage(args\[0]);
12. Jimi.putImage(image, args\[1]);
13. System.exit(0);
14. }
15. }
16. }

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

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

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

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

java 代码

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

////////////////////////////  
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 代码

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

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