/** * * @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"; JimiWriter writer = Jimi.createJimiWriter(dest); writer.setSource(img); dest = dest.substring(0, dest.lastIndexOf(".")) + ".tif"; writer.putImage(dest); } /** * 线性改变图片尺寸(可同时改变图片格式) * * @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); // 得到源图长 int iWideth = (int) (wideth * ins); int iHeight = (int) (height * ins); BufferedImage tag = new BufferedImage(iWideth, iHeight, BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(src, 0, 0, iWideth, iHeight, null); // 绘制缩小后的图 if (!temp.trim().equals(desc)) _file.deleteOnExit(); if (desc.toLowerCase().trim().endsWith("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")) { this.toTIF(tag, desc); } else { JimiWriter writer = Jimi.createJimiWriter(desc); writer.setSource(tag); writer.putImage(desc); } }
|