来源:http://zbnw.blog.hexun.com/9894214_d.html
java的开源软件jimi图片处理工具,编写了一个图片转换工具类,包含了图片水印功能,给大家分享。
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ImageProducer;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.sun.jimi.core.Jimi;
import com.sun.jimi.core.JimiException;
import com.sun.jimi.core.JimiWriter;
import com.sun.jimi.core.options.JPGOptions;
public class TransferPicture {
/**
* @param source
* @param dest
* @param quality
* 图片格式转换
*/
public void toJPG(String source, String type, int quality) {
//0 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; } String outfile = ConvertUtil.getFilename(source)+type; try { JPGOptions options = new JPGOptions(); options.setQuality(quality); ImageProducer image = Jimi.getImageProducer(source); JimiWriter writer = Jimi.createJimiWriter(outfile); writer.setSource(image); // 加入属性设置,非必要 // /* writer.setOptions(options); // */ writer.putImage(outfile); } catch (JimiException je) { System.err.println("Error: " + je); } } /** * @param source * @param dest * @throws JimiException */ public 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 (Exception 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(); /* //分解GIF: GifDecoder d = new GifDecoder(); d.read("sample.gif"); int n = d.getFrameCount(); for(int i = 0; i < n; i++) { BufferedImage frame = d.getFrame(i); // frame i int t = d.getDelay(i); // display duration of frame in milliseconds // do something with frame } //合成GIF: AnimatedGifEncoder e = new AnimatedGifEncoder(); e.start(outputFileName); e.setDelay(1000); // 1 frame per sec e.addFrame(image1); e.addFrame(image2); e.finish(); */ } /** * @param img * @param dest * @throws JimiException */ public void toTIF(Image img, String dest) throws JimiException { if (!dest.toLowerCase().trim().endsWith("tif")) { dest += ".tif"; System.out.println("Overriding to TIF, output file: " + dest); } dest = dest.substring(0, dest.lastIndexOf(".")) + ".jpg"; try{ System.out.println("toTIF encode"); JimiWriter writer = Jimi.createJimiWriter(dest); writer.setSource(img); dest = dest.substring(0, dest.lastIndexOf(".")) + ".tif"; writer.putImage(dest); }catch(Exception e){e.printStackTrace();} } /** * 线性改变图片尺寸(可同时改变图片格式) * @param source * 源文件完整路径 * @param desc * 目标文件完整路径 * @param ins * 放大/缩小比率 * @throws JimiException * @throws IOException */ public void changeDimension(String source, String desc, double ins) throws JimiException, IOException { String temp = desc; File _file = null; if (desc == null || desc.trim().equals("")) desc = source; if (!desc.toLowerCase().trim().endsWith("jpg")) { temp = desc.trim() + ".jpg"; } this.toJPG(source, temp, 75); _file = new File(temp); // 读入文件 Image src = javax.imageio.ImageIO.read(_file); // 构造Image对象 double wideth = (double) src.getWidth(null); // 得到源图宽 double height = (double) src.getHeight(null); // 得到源图长 System.out.println("源图宽:"+wideth); System.out.println("源图长:"+height); /*//缩放处理 int iWideth = (int) (wideth * ins); int iHeight = (int) (height * ins); */ int iWideth = 300; int iHeight = 200; System.out.println("现图宽:"+iWideth); System.out.println("现图长:"+iHeight); System.out.println("缩放参数:"+ins); BufferedImage tag = null; try{ tag = new BufferedImage(iWideth, iHeight,BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(src, 0, 0, iWideth, iHeight, null); // 绘制缩小后的图 /*//打水印 File _filebiao = new File("test2.jpg"); Image src_biao = ImageIO.read(_filebiao); int wideth_biao = 40;//src_biao.getWidth(null); int height_biao = 40;//src_biao.getHeight(null); tag.getGraphics().drawImage(src_biao,50,50,wideth_biao,height_biao,null); */ }catch(Exception e){e.printStackTrace();} if (!temp.trim().equals(desc)) _file.deleteOnExit(); if (desc.toLowerCase().trim().endsWith("gif")) { System.out.println("the type is gif!"); AnimatedGifEncoder e = new AnimatedGifEncoder(); e.start(desc); e.addFrame(tag); e.finish(); } else if (desc.toLowerCase().trim().endsWith("tif") || desc.toLowerCase().trim().endsWith("tiff")) { System.out.println("the type is tif!"); this.toTIF(tag, desc); } else { try{ System.out.println("common type!"); JimiWriter writer = Jimi.createJimiWriter(desc); writer.setSource(tag); writer.putImage(desc); }catch(Exception ex){ex.printStackTrace();} } } public void transpic(String infile,int iWideth, int iHeight, String type) throws JimiException, IOException { String temp = infile; File _file = null; /*if (outfile == null || outfile.trim().equals("")) outfile = infile; if (!outfile.toLowerCase().trim().endsWith("jpg")) { temp = outfile.trim() + ".jpg"; }*/ ConvertUtil cu = new ConvertUtil(); if(!cu.getFiletype(infile).equals("jpg")){ this.toJPG(infile, temp, 75); } _file = new File(temp); // 读入文件 Image src = javax.imageio.ImageIO.read(_file); // 构造Image对象 double wideth = (double) src.getWidth(null); // 得到源图宽 double height = (double) src.getHeight(null); // 得到源图长 System.out.println("源图宽:"+wideth); System.out.println("源图长:"+height); /*//缩放处理 int iWideth = (int) (wideth * ins); int iHeight = (int) (height * ins); */ //int iWideth = 300; //int iHeight = 200; System.out.println("现图宽:"+iWideth); System.out.println("现图长:"+iHeight); String outfile = ""; BufferedImage tag = null; try{ tag = new BufferedImage(iWideth, iHeight,BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(src, 0, 0, iWideth, iHeight, null); // 绘制缩小后的图 /*//打水印 File _filebiao = new File("test2.jpg"); Image src_biao = ImageIO.read(_filebiao); int wideth_biao = 40;//src_biao.getWidth(null); int height_biao = 40;//src_biao.getHeight(null); tag.getGraphics().drawImage(src_biao,50,50,wideth_biao,height_biao,null); */ }catch(Exception e){e.printStackTrace();} if (!temp.trim().equals(outfile)) _file.deleteOnExit(); if (outfile.toLowerCase().trim().endsWith("gif")) { System.out.println("the type is gif!"); AnimatedGifEncoder e = new AnimatedGifEncoder(); e.start(outfile); e.addFrame(tag); e.finish(); } else if (outfile.toLowerCase().trim().endsWith("tif") || outfile.toLowerCase().trim().endsWith("tiff")) { System.out.println("the type is tif!"); this.toTIF(tag, outfile); } else { try{ System.out.println("common type!"); JimiWriter writer = Jimi.createJimiWriter(outfile); writer.setSource(tag); writer.putImage(outfile); }catch(Exception ex){ex.printStackTrace();} } } public static void main(String[] args) { String dest = "test.jpg"; String source = "test.gif"; int quality = 100; double ins = 4; TransferPicture tp = new TransferPicture(); try{ System.out.println("before change"); System.out.println("-------------------"); //tp.toJPG(source,dest,quality); //tp.toGIF(source, dest); tp.changeDimension(source, dest, ins); System.out.println("-------------------"); System.out.println("end change"); }catch(Exception e){ e.printStackTrace(); } } }