Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 344|回复: 0

[图像处理学习]把目录下的图片制作成缩略图并生成html

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-29 23:56:45 | 显示全部楼层 |阅读模式
    1. import java.awt.Graphics;
    2. import java.awt.Image;
    3. import java.awt.image.BufferedImage;
    4. import java.awt.image.ImageObserver;
    5. import java.io.File;
    6. import java.io.FileFilter;
    7. import java.io.FileInputStream;
    8. import java.io.FileOutputStream;
    9. import java.io.FileWriter;
    10. import java.io.IOException;
    11. import java.io.InputStream;
    12. import javax.imageio.ImageIO;
    13. /**
    14. * 此程序来自http://www.dingl.com ,站长略作修改.
    15. * 程序功能:把一个目录下的所有jpg图片制作成缩略图并生成html演示文件
    16. */
    17. public class Zoom {
    18. String srcPath;
    19. String Title;
    20. StringBuffer html;
    21. int count;
    22. public Zoom(String srcPath,String Title) {
    23.   this.srcPath = srcPath;
    24.   this.Title=Title;

    25. }


    26. public static void main(String[] args) {
    27.    String srcPath = args[0];//源图像的目录
    28.    String Title=args[1];//html文件的标题
    29.   if (srcPath==null||Title==null){
    30.    printHelp();
    31.    return;
    32.   }
    33.   
    34.   Zoom zoom = new Zoom(srcPath,Title);
    35.   zoom.process();
    36. }

    37. public void process() {
    38.   File[] files = getFiles();//获取图像源文件夹下所有的jpg图像
    39.   mkdirs(); //创建缩略图的输出目录zoom
    40.   System.out.println(files.length);
    41.   int k=files.length%45;//每页图像
    42.   int p=files.length/45;//页数
    43.    if(k!=0) p++;
    44.   System.out.println(p);
    45.   
    46.   
    47.   for (int i = 0; i < p; i++) {
    48.      init(i);
    49.     for(int j=0;j< 45;j++){
    50.       if(i*45+j>=files.length) break;
    51.       zoom(files[i*45+j]);
    52.     }
    53.    trail(p);
    54.    outputHtmlFile(i);
    55.    
    56.   }
    57. }

    58.   public void zoom(File input) {
    59.   //输出的位置
    60.   String fn=input.getName();
    61.   int x=fn.indexOf(".");
    62.   String zipf=fn.substring(0,x)+".zip";
    63.   //System.out.println(zipf);
    64.   String output = getOutputPath();
    65.   try {
    66.    InputStream imageStream = new FileInputStream(input);
    67.    //根据目标图片建立一个缓存图片
    68.    BufferedImage imageFile = ImageIO.read(imageStream);
    69.   // float zoom = 0.1F; //你要方缩的比例
    70.    //获得目标图片的宽高,同时乘以放缩比例得到新图片大小
    71.   // int w = (int) (imageFile.getWidth() * zoom);
    72.    //int h = (int) (imageFile.getHeight() * zoom);
    73.    
    74.   // if(w>100) w=100;
    75.   //if(h>90) h=90;
    76.   int w = 100;
    77.   int h = 90;
    78.    //建立一个新图片的缓存图片
    79.    BufferedImage bufImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    80.    String zoomFile = output + "/zooms_" + input.getName();//缩略图文件名
    81.    FileOutputStream out = new FileOutputStream(zoomFile);
    82.    //从目标图片上获得Graphics以便画在新图片上,最后一个参数是内部无名类,可以用null代替
    83.    Graphics g = bufImage.getGraphics();
    84.    g.drawImage(imageFile, 0, 0, w, h, new ImageObserver() {
    85.     public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
    86.      return true;
    87.     }
    88.    });
    89.    //输出
    90.    ImageIO.write(bufImage, "JPEG", out);
    91.    out.flush();
    92.    out.close();
    93.    imageStream.close();
    94.    int row = count % 3;
    95.    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>");
    }
    1. count++;
    2.    } catch (Exception e) {
    3.       e.printStackTrace();
    4.    }
    5. }
    6. private File[] getFiles() {
    7.   File path = new File(srcPath);
    8.   File[] files = path.listFiles(new FileFilter() {
    9.    public boolean accept(File pathname) {
    10.     if (pathname == null)
    11.      return false;
    12.     String ext = pathname.getName().substring(pathname.getName().lastIndexOf(".") + 1).toUpperCase();
    13.     return ext.equals("JPG") || ext.equals("GIF");
    14.    }
    15.   });
    16.   return files;
    17. }
    18. private void mkdirs() {
    19.   File zoomPath = new File(getOutputPath());
    20.   zoomPath.mkdirs();
    21. }
    22. private String getOutputPath() {
    23.   return srcPath + "/zoom";
    24. }
    25.   private void init(int i) {
    26.   count = 0;
    27.   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>&nbsp</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>");

       }
    1. private String getDirName() {
    2.   if (srcPath.endsWith("/")) {
    3.    srcPath = srcPath.substring(0, srcPath.length() - 1);
    4.   }
    5.   return srcPath.substring(srcPath.lastIndexOf("/") + 1);
    6. }
    7. private void outputHtmlFile(int i) {
    8.   FileWriter writer = null;
    9.   try {
    10.    File htmlFile = new File(srcPath + "/img"+i+".html");
    11.    writer = new FileWriter(htmlFile);
    12.    writer.write(html.toString());
    13.    writer.flush();
    14.    System.out.println("OK"+i);
    15.   } catch (IOException e) {
    16.    e.printStackTrace();
    17.   } finally {
    18.    if (writer != null) {
    19.     try {
    20.      writer.close();
    21.     } catch (IOException e1) {
    22.      e1.printStackTrace();
    23.     }
    24.    }
    25.   }
    26. }

    27. public static void printHelp(){
    28.   System.out.println("USAGE : java Zoom < FILEPATH> < Title>");
    29. }
    30. }
    复制代码

       
         
         
          
          

            
          

            
          
         
       

      


    源码下载:http://file.javaxxz.com/2014/10/29/235645625.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-26 01:13 , Processed in 0.330123 second(s), 34 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表