import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
/**
* 此程序来自http://www.dingl.com ,站长略作修改.
* 程序功能:把一个目录下的所有jpg图片制作成缩略图并生成index.html演示文件
*/
public class Zoom { String srcPath; StringBuffer html; int count;//输出
public Zoom(String srcPath) { this.srcPath = srcPath; init(); }
public void zoom(File input) { //输出的位置 String output = getOutputPath(); try { InputStream imageStream = new FileInputStream(input); //根据目标图片建立一个缓存图片 BufferedImage imageFile = ImageIO.read(imageStream);
float zoom = 0.12F; //你要方缩的比例 //获得目标图片的宽高,同时乘以放缩比例得到新图片大小 int w = (int) (imageFile.getWidth() * zoom); int h = (int) (imageFile.getHeight() * zoom); //建立一个新图片的缓存图片 BufferedImage bufImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); String zoomFile = output + "/zooms_" + input.getName(); FileOutputStream out = new FileOutputStream(zoomFile); //从目标图片上获得Graphics以便画在新图片上,最后一个参数是内部无名类,可以用null代替 Graphics g = bufImage.getGraphics(); g.drawImage(imageFile, 0, 0, w, h, new ImageObserver() { public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) { return true; } });
ImageIO.write(bufImage, "JPEG", out);
out.flush();
out.close();
imageStream.close();
int row = count % 3;
if (row == 0) {
html.append("\n\t<tr>");
}
html.append("\n\t\t<td align='center'>");
html.append("
");
html.append(input.getName() + "</td>");
if (row == 2) {
html.append("\n\t</tr>");
}
count++;
} catch (Exception e) {
e.printStackTrace();
}
}
public void process() { File[] files = getFiles(); mkdirs(); for (int i = 0; i < files.length; i++) { zoom(files[i]); } trail(); outputHtmlFile(); } private File[] getFiles() { File path = new File(srcPath); File[] files = path.listFiles(new FileFilter() { public boolean accept(File pathname) { if (pathname == null) return false; String ext = pathname.getName().substring(pathname.getName().lastIndexOf(".") + 1).toUpperCase(); return ext.equals("JPG") || ext.equals("JPEG"); } }); return files; } private void mkdirs() { File zoomPath = new File(getOutputPath()); zoomPath.mkdirs(); } private String getOutputPath() { return srcPath + "/zoom"; }private void init() {
private void trail() {
int row = count % 3;
if (row == 0) {
html.append("\n\t\t<td> </td>");
}
if (row == 1) {
html.append("\n\t\t<td> </td>");
html.append("\n\t\t<td> </td>");
}
html.append("\n\t</tr>");
html.append("\n</table>");
html.append("\n</body>");
html.append("\n");
}
private String getDirName() { if (srcPath.endsWith("/")) { srcPath = srcPath.substring(0, srcPath.length() - 1); } return srcPath.substring(srcPath.lastIndexOf("/") + 1); } private void outputHtmlFile() { FileWriter writer = null; try { File htmlFile = new File(srcPath + "/index.html"); writer = new FileWriter(srcPath + "/index.html"); writer.write(html.toString()); writer.flush(); } catch (IOException e) { e.printStackTrace(); } finally { if (writer != null) { try { writer.close(); } catch (IOException e1) { e1.printStackTrace(); } } } } public static void main(String[] args) { String srcPath = args[0]; if (srcPath==null){ printHelp(); return; } Zoom zoom = new Zoom(srcPath); zoom.process(); } public static void printHelp(){ System.out.println("USAGE : java Zoom</td> </tr> <tr>"); } }
↑返回目录
前一篇: Java中堆和栈的区别
后一篇: 一个web服务器源码