java 更改图片格式
发布网友
发布时间:2024-07-04 00:22
我来回答
共1个回答
热心网友
时间:2024-07-04 11:52
jpg ico 互换可以解决jimi包 但转成 gif 需要先将其转成jpg,再转gif
代码如下。
1, public void toJPG(String source, String dest, int quality) {
if (dest == null || dest.trim().equals(""))
dest = source;
if (!dest.toLowerCase().trim().endsWith("jpg")) {
dest += ".jpg";
System.out.println("Overriding to JPG, output file: " + dest);
}
if (quality < 0 || quality > 100 || (quality + "") == null
|| (quality + "").equals("")) {
System.out.println("quality must between ’0’ and ’100’");
System.out.println("set to DEFAULT value:’75’");
quality = 75;
}
try {
JPGOptions options = new JPGOptions();
options.setQuality(quality);
ImageProcer image = Jimi.getImageProcer(source);
JimiWriter writer = Jimi.createJimiWriter(dest);
writer.setSource(image);
// 加入属性设置,非必要
// /*
writer.setOptions(options);
// */
writer.putImage(dest);
} catch (JimiException je) {
System.err.println("Error: " + je);
}
}
2public void toGIF(String source, String dest) throws JimiException {
if (dest == null || dest.trim().equals(""))
dest = source;
// 1:转换为jpg
if (!dest.toLowerCase().trim().endsWith("jpg")) {
dest += ".jpg";
}
toJPG(source, dest, 75);
BufferedImage file_in = null;
File file = new File(dest);
try {
file_in = javax.imageio.ImageIO.read(file);
} catch (IOException e) {
e.printStackTrace();
}
int end = dest.lastIndexOf(".");
file.deleteOnExit();
// output *.gif
file.renameTo(new File(dest.substring(0, end) + ".gif"));
// jpg to gif
AnimatedGifEncoder e = new AnimatedGifEncoder();
e.start(dest);
e.addFrame(file_in);
e.finish();
}