//** 輸入的是 HiColor 的 jpeg 檔案..
//** 輸出的格式欲為 HiColor 的 gif 檔案, 但每次都出現 too many colors (error message)
//** 若輸入的是8-bit jpeg(灰階) 檔案, 則就不會有問題..頂多只到 256灰階顏色罷了..
//** 是否有可解決的方法???
//*************************************************************
import com.davisor.graphics.codec.JPEGDecoder;
import com.davisor.graphics.codec.CodecFactory;
import Acme.JPM.Encoders.GifEncoder;
JPEGDecoder jdc = new JPEGDecoder();
FileInputStream fi = new FileInputStream(new File("C://jpg2gif.jpg"));
Image ie = jdc.decode(fi);
fi.close();
BufferedImage bi = CodecFactory.createBufferedImage(ie);
FileOutputStream fo = new FileOutputStream(new File("D://jpg2gif2.gif"));
GifEncoder ger = new GifEncoder(ie, fo);
ger.encode();
fo.close();
gif4j包实现对JPG和GIF文件格式的读取
from: http://www.lordz.cn/post/12.html
在写采集程序的时候如果想抓取网络上的GIF图片,有时候抓过来只有一帧。GIF图片采用了LZW算法,所以在处理的时候很复杂,特别是在调整大小的时候。这里推荐一个破解的工具包。
1. if(".gif".equalsIgnoreCase(fileExtension)){
2. try {
3. GifImage gifImage=GifDecoder.decode(new URL(bean.getImgsrc()));
4. File newFile=new File(destFile);
5. GifEncoder.encode(gifImage, newFile);
6. } catch (Exception e) {
7. e.printStackTrace();
8. }
9.
10. }else {
11. try {
12. GetImage.SaveImage(bean.getImgsrc(), destFile);
13. } catch (Exception e) {
14. e.printStackTrace();
15. }
16.
17. }
if(".gif".equalsIgnoreCase(fileExtension)){ try { GifImage gifImage=GifDecoder.decode(new URL(bean.getImgsrc())); File newFile=new File(destFile); GifEncoder.encode(gifImage, newFile); } catch (Exception e) { e.printStackTrace(); } }else { try { GetImage.SaveImage(bean.getImgsrc(), destFile); } catch (Exception e) { e.printStackTrace(); } }
保存的方法
1. public static void SaveImage(String srcpath, String destfile) {
2. try {
3. URLConnection connection=new URL(srcpath).openConnection();
4. InputStream imgInputStream=connection.getInputStream();
5. BufferedImage image=JPEGCodec.createJPEGDecoder(imgInputStream).decodeAsBufferedImage();
6. java.io.FileOutputStream output = new FileOutputStream(new File(destfile));
7. JPEGImageEncoder encoder = JPEGCodec
8. .createJPEGEncoder(output);
9. encoder.encode(image);
10. imgInputStream.close();
11. } catch (Exception ex) {
12. ex.printStackTrace();
13. }
14.
15. }
1. package com.org.gif4j;
2.
3. import java.awt.Rectangle;
4. import java.io.File;
5. import java.io.IOException;
6. import com.gif4j.GifDecoder;
7. import com.gif4j.GifEncoder;
8. import com.gif4j.GifImage;
9. import com.gif4j.GifTransformer;
10.
11. public class demo {
12. public static void main(String[] args) {
13. File srcImg = new File("f:/test.gif");
14. File destImg = new File("f:/t.gif");
15. demo.getGifImage(srcImg, destImg, 270, 310, true);// 根据需要传参数
16. }
17.
18. /**
19. * gif图的简单处理
20. *
21. * @param srcImg
22. * @param destImg
23. * @param width
24. * @param height
25. * @param smooth
26. */
27. public static void getGifImage(File srcImg, File destImg, int width, int height, boolean smooth) {
28. try {
29. GifImage gifImage = GifDecoder.decode(srcImg);//创建一个GifImage对象.
30. // 1.缩放重新更改大小.
31. GifImage resizeIMG = GifTransformer.resize(gifImage,width, height,smooth);
32. // 2.剪切图片演示.
33. // Rectangle rect = new Rectangle(0,0,200,200);
34. // GifImage cropIMG = GifTransformer.crop(gifImage, rect);
35. // 3.按比例缩放
36. // GifImage scaleIMG = GifTransformer.scale(gifImage, 200.0, 200.0, true);//参数需要double型
37. // 4.其他的方法.还有很多,比如水平翻转,垂直翻转 等等.都是GifTransformer类里面的.
38. GifEncoder.encode(resizeIMG, destImg);
39. } catch (IOException e) {
40. e.printStackTrace();
41. }
42. }
43. }
下载gif4j包.网址:http://www.gif4j.com/download.htm#PRO
gif4j很好的对gif图的操作进行了封装.具体的可以看下载后的帮助文件和example
将jar包引入工程..
加水印
另外如果要在上面加水印的话可以用这段代码执行: // 来自gif4j示例代码 public GifImage addTextWatermarkToGifImage(GifImage gifImage, String watermarkText) { //create new TextPainter TextPainter textPainter = new TextPainter(new Font("Verdana", Font.BOLD, 12)); textPainter.setOutlinePaint(Color.WHITE); //render the specified text outlined BufferedImage renderedWatermarkText = textPainter.renderString(watermarkText, true); //create new Watermark Watermark watermark = new Watermark(renderedWatermarkText, Watermark.LAYOUT_MIDDLE_CENTER); //apply watermark to the specified gif image and return the result return watermark.apply(gifImage, true); }///执行主函数
public static void main(String[] args) { try { mainCls im = new mainCls(); String dir = "d:
my.gif"; File srcImg = new File(dir); File destImg = new File("d:/your.gif"); GifImage gif = GifDecoder.decode(srcImg); GifImage gi = im.addTextWatermarkToGifImage(gif,"www.wapdu.com"); GifEncoder.encode(gi,destImg); } catch(Exception e) { e.printStackTrace(); } }
http://www.javaeye.com/topic/266585
1. /**
2. * 缩略图实现,将图片(jpg、bmp、png、gif等等)真实的变成想要的大小
3. */
4. package com.joewalker.test;
5.
6. import java.awt.Image;
7. import java.awt.image.BufferedImage;
8. import java.io.File;
9. import java.io.FileOutputStream;
10. import java.io.IOException;
11. import javax.imageio.ImageIO;
12. import com.sun.image.codec.jpeg.JPEGCodec;
13. import com.sun.image.codec.jpeg.JPEGImageEncoder;
14.
15. /*******************************************************************************
16. * 缩略图类(通用) 本java类能将jpg、bmp、png、gif图片文件,进行等比或非等比的大小转换。 具体使用方法
17. * compressPic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true))
18. */
19. public class CompressPicDemo {
20. private File file = null; // 文件对象
21. private String inputDir; // 输入图路径
22. private String outputDir; // 输出图路径
23. private String inputFileName; // 输入图文件名
24. private String outputFileName; // 输出图文件名
25. private int outputWidth = 100; // 默认输出图片宽
26. private int outputHeight = 100; // 默认输出图片高
27. private boolean proportion = true; // 是否等比缩放标记(默认为等比缩放)
28. public CompressPicDemo() { // 初始化变量
29. inputDir = "";
30. outputDir = "";
31. inputFileName = "";
32. outputFileName = "";
33. outputWidth = 100;
34. outputHeight = 100;
35. }
36. public void setInputDir(String inputDir) {
37. this.inputDir = inputDir;
38. }
39. public void setOutputDir(String outputDir) {
40. this.outputDir = outputDir;
41. }
42. public void setInputFileName(String inputFileName) {
43. this.inputFileName = inputFileName;
44. }
45. public void setOutputFileName(String outputFileName) {
46. this.outputFileName = outputFileName;
47. }
48. public void setOutputWidth(int outputWidth) {
49. this.outputWidth = outputWidth;
50. }
51. public void setOutputHeight(int outputHeight) {
52. this.outputHeight = outputHeight;
53. }
54. public void setWidthAndHeight(int width, int height) {
55. this.outputWidth = width;
56. this.outputHeight = height;
57. }
58.
59. /*
60. * 获得图片大小
61. * 传入参数 String path :图片路径
62. */
63. public long getPicSize(String path) {
64. file = new File(path);
65. return file.length();
66. }
67.
68. // 图片处理
69. public String compressPic() {
70. try {
71. //获得源文件
72. file = new File(inputDir + inputFileName);
73. if (!file.exists()) {
74. return "";
75. }
76. Image img = ImageIO.read(file);
77. // 判断图片格式是否正确
78. if (img.getWidth(null) == -1) {
79. System.out.println(" can't read,retry!" + "<BR>");
80. return "no";
81. } else {
82. int newWidth; int newHeight;
83. // 判断是否是等比缩放
84. if (this.proportion == true) {
85. // 为等比缩放计算输出的图片宽度及高度
86. double rate1 = ((double) img.getWidth(null)) / (double) outputWidth + 0.1;
87. double rate2 = ((double) img.getHeight(null)) / (double) outputHeight + 0.1;
88. // 根据缩放比率大的进行缩放控制
89. double rate = rate1 > rate2 ? rate1 : rate2;
90. newWidth = (int) (((double) img.getWidth(null)) / rate);
91. newHeight = (int) (((double) img.getHeight(null)) / rate);
92. } else {
93. newWidth = outputWidth; // 输出的图片宽度
94. newHeight = outputHeight; // 输出的图片高度
95. }
96. BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB);
97.
98. /*
99. * Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的
100. * 优先级比速度高 生成的图片质量比较好 但速度慢
101. */
102. tag.getGraphics().drawImage(img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null);
103. FileOutputStream out = new FileOutputStream(outputDir + outputFileName);
104. // JPEGImageEncoder可适用于其他图片类型的转换
105. JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
106. encoder.encode(tag);
107. out.close();
108. }
109. } catch (IOException ex) {
110. ex.printStackTrace();
111. }
112. return "ok";
113. }
114. public String compressPic (String inputDir, String outputDir, String inputFileName, String outputFileName) {
115. // 输入图路径
116. this.inputDir = inputDir;
117. // 输出图路径
118. this.outputDir = outputDir;
119. // 输入图文件名
120. this.inputFileName = inputFileName;
121. // 输出图文件名
122. this.outputFileName = outputFileName;
123. return compressPic();
124. }
125. public String compressPic(String inputDir, String outputDir, String inputFileName, String outputFileName, int width, int height, boolean gp) {
126. // 输入图路径
127. this.inputDir = inputDir;
128. // 输出图路径
129. this.outputDir = outputDir;
130. // 输入图文件名
131. this.inputFileName = inputFileName;
132. // 输出图文件名
133. this.outputFileName = outputFileName;
134. // 设置图片长宽
135. setWidthAndHeight(width, height);
136. // 是否是等比缩放 标记
137. this.proportion = gp;
138. return compressPic();
139. }
140.
141. // main测试
142. // compressPic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true))
143. public static void main(String[] arg) {
144. CompressPicDemo mypic = new CompressPicDemo();
145. System.out.println("输入的图片大小:" + mypic.getPicSize("e://1.jpg")/1024 + "KB");
146. int count = 0; // 记录全部图片压缩所用时间
147. for (int i = 0; i < 100; i++) {
148. int start = (int) System.currentTimeMillis(); // 开始时间
149. mypic.compressPic("e://", "e://test//", "1.jpg", "r1"+i+".jpg", 120, 120, true);
150. int end = (int) System.currentTimeMillis(); // 结束时间
151. int re = end-start; // 但图片生成处理时间
152. count += re; System.out.println("第" + (i+1) + "张图片压缩处理使用了: " + re + "毫秒");
153. System.out.println("输出的图片大小:" + mypic.getPicSize("e://test//r1"+i+".jpg")/1024 + "KB");
154. }
155. System.out.println("总共用了:" + count + "毫秒");
156. }
157. }
//调用类中部分代码
Java代码
1. // 转换并写文件,若为GIF则需要特殊转换
2. if ("gif".equalsIgnoreCase(suf)) {
3. GifResizeProcessor.getGifImage(srcImg, destImg, width, height, true);
4. } else {
5. Image dest = src.getScaledInstance(width, height,
6. Image.SCALE_SMOOTH);
7. BufferedImage tag = new BufferedImage(width, height,
8. BufferedImage.TYPE_INT_RGB);
9. tag.getGraphics().drawImage(dest, 0, 0, width, height, null);
10. ImageIO.write(tag, suf, destImg);
11. }
Java代码
1. /**
2. *GIF文件缩略图处理函数
3. *srcImg 源图
4. *destImg 缩略图
5. */
6. public static void getGifImage(File srcImg, File destImg, int width,
7. int height, boolean smooth) {
8.
9. try {
10. GifImage gifImage = GifDecoder.decode(srcImg);
11. GifImage resizedGifImage2 = GifTransformer.resize(gifImage, width, height, smooth);
12. GifEncoder.encode(resizedGifImage2, destImg);
13. } catch (IOException e) {
14. e.printStackTrace();
15. }
16.
17. }