TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
- 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图片制作成缩略图并生成html演示文件
- */
- public class Zoom {
- String srcPath;
- String Title;
- StringBuffer html;
- int count;
- public Zoom(String srcPath,String Title) {
- this.srcPath = srcPath;
- this.Title=Title;
-
- }
-
-
- public static void main(String[] args) {
- String srcPath = args[0];//源图像的目录
- String Title=args[1];//html文件的标题
- if (srcPath==null||Title==null){
- printHelp();
- return;
- }
-
- Zoom zoom = new Zoom(srcPath,Title);
- zoom.process();
- }
-
- public void process() {
- File[] files = getFiles();//获取图像源文件夹下所有的jpg图像
- mkdirs(); //创建缩略图的输出目录zoom
- System.out.println(files.length);
- int k=files.length%45;//每页图像
- int p=files.length/45;//页数
- if(k!=0) p++;
- System.out.println(p);
-
-
- for (int i = 0; i < p; i++) {
- init(i);
- for(int j=0;j< 45;j++){
- if(i*45+j>=files.length) break;
- zoom(files[i*45+j]);
- }
- trail(p);
- outputHtmlFile(i);
-
- }
- }
-
- public void zoom(File input) {
- //输出的位置
- String fn=input.getName();
- int x=fn.indexOf(".");
- String zipf=fn.substring(0,x)+".zip";
- //System.out.println(zipf);
- String output = getOutputPath();
- try {
- InputStream imageStream = new FileInputStream(input);
- //根据目标图片建立一个缓存图片
- BufferedImage imageFile = ImageIO.read(imageStream);
- // float zoom = 0.1F; //你要方缩的比例
- //获得目标图片的宽高,同时乘以放缩比例得到新图片大小
- // int w = (int) (imageFile.getWidth() * zoom);
- //int h = (int) (imageFile.getHeight() * zoom);
-
- // if(w>100) w=100;
- //if(h>90) h=90;
- int w = 100;
- int h = 90;
- //建立一个新图片的缓存图片
- 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("
<tr>");
}
html.append("
<td align="center">");
html.append("<img src="zoom/zooms_" + input.getName() + "" border="0"><br><br>");
html.append("<a href="http://xx.xx.com/zz3zimg/"+zipf+"">点击下载</a></td>");
if (row == 2) {
html.append("
</tr>");
}
- count++;
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- 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("GIF");
- }
- });
- return files;
- }
- private void mkdirs() {
- File zoomPath = new File(getOutputPath());
- zoomPath.mkdirs();
- }
- private String getOutputPath() {
- return srcPath + "/zoom";
- }
- private void init(int i) {
- count = 0;
- html = new StringBuffer();
复制代码 html.append("<hr size="1" noshade><table border="0" width="497" align="center">
");
} private void trail(int p) {
int row = count % 3;
if (row == 1) {
html.append("
<td> </td>");
html.append("
</tr>");
} if (row == 2) {
html.append("
<td> </td><td> </td>");
html.append("
</tr>");
}
html.append("
<tr><td colspan="3"><br>当前共有"+p+"页:");
for(int i=1;i<=p;i++){
html.append("<a href="img"+(i-1)+".html">"+i+"</a> ");
if(i%25==0) html.append("<br> ");
}
html.append("</td></tr>");
html.append("
</table>");
}
- private String getDirName() {
- if (srcPath.endsWith("/")) {
- srcPath = srcPath.substring(0, srcPath.length() - 1);
- }
- return srcPath.substring(srcPath.lastIndexOf("/") + 1);
- }
- private void outputHtmlFile(int i) {
- FileWriter writer = null;
- try {
- File htmlFile = new File(srcPath + "/img"+i+".html");
- writer = new FileWriter(htmlFile);
- writer.write(html.toString());
- writer.flush();
- System.out.println("OK"+i);
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (writer != null) {
- try {
- writer.close();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- }
- }
-
- public static void printHelp(){
- System.out.println("USAGE : java Zoom < FILEPATH> < Title>");
- }
- }
复制代码
源码下载:http://file.javaxxz.com/2014/10/29/235645625.zip |
|