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入门到精通教程
查看: 338|回复: 0

[Java基础知识]一个相册制作程序

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

    [LV.1]初来乍到

    发表于 2014-10-2 04:13:29 | 显示全部楼层 |阅读模式
    在此基础上修改,如在相片上追加文字、图片等,各位就根据自己的需要再加工吧!
    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.                         
    复制代码
    /**
    * 此程序来自http://www.dingl.com ,站长略作修改.
    * 程序功能:把一个目录下的所有jpg图片制作成缩略图并
    * 生成index.HTML演示文件
    */  
      
       
       
         
       

       
       
      
    1. public class Zoom {
    2. String srcPath;
    3. StringBuffer html;
    4. int count;

    5. public Zoom(String srcPath) {
    6.   this.srcPath = srcPath;
    7.   init();
    8. }

    9. public void zoom(File input) {
    10.   //输出的位置
    11.   String output = getOutputPath();
    12.   try {
    13.    InputStream imageStream = new FileInputStream(input);
    14.    //根据目标图片建立一个缓存图片
    15.    BufferedImage imageFile = ImageIO.read(imageStream);

    16.    float zoom = 0.12F; //你要方缩的比例
    17.    //获得目标图片的宽高,同时乘以放缩比例得到新图片大小
    18.    int w = (int) (imageFile.getWidth() * zoom);
    19.    int h = (int) (imageFile.getHeight() * zoom);
    20.    //建立一个新图片的缓存图片
    21.    BufferedImage bufImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    22.    String zoomFile = output + "/zooms_" + input.getName();
    23.    FileOutputStream out = new FileOutputStream(zoomFile);
    24.    //从目标图片上获得Graphics以便画在新图片上,最后一个参数是内部无名类,可以用null代替
    25.    Graphics g = bufImage.getGraphics();
    26.    g.drawImage(imageFile, 0, 0, w, h, new ImageObserver() {
    27.     public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
    28.      return true;
    29.     }
    30.    });
    复制代码
    //输出  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"><a href="").append(input.getName()).append("" target="_blank">");
    html.append("<img src="zoom" + "/zooms_" + input.getName() + "" border="0"><br>");
    html.append(input.getName() + "</a></td>");
    if (row == 2) {
       html.append("
            </tr>");
    }
    count++;
    } catch (Exception e) {
        e.printStackTrace();
    }
    }
    1. public void process() {
    2.   File[] files = getFiles();
    3.   mkdirs();
    4.   for (int i = 0; i < files.length; i++) {
    5.    zoom(files[i]);
    6.   }
    7.   trail();
    8.   outputHtmlFile();
    9. }
    10. private File[] getFiles() {
    11.   File path = new File(srcPath);
    12.   File[] files = path.listFiles(new FileFilter() {
    13.    public boolean accept(File pathname) {
    14.     if (pathname == null)
    15.      return false;
    16.     String ext = pathname.getName().substring(pathname.getName().lastIndexOf(".") + 1).toUpperCase();
    17.     return ext.equals("JPG") || ext.equals("JPEG");
    18.    }
    19.   });
    20.   return files;
    21. }
    22. private void mkdirs() {
    23.   File zoomPath = new File(getOutputPath());
    24.   zoomPath.mkdirs();
    25. }
    26. private String getOutputPath() {
    27.   return srcPath + "/zoom";
    28. }
    复制代码
    private void init() {
       count = 0;
       html = new StringBuffer();
       html.append("<html>");
       html.append("
    <head>");
       html.append("
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">");
       html.append("
    <title>").append(getDirName()).append("</title>");
       html.append("
    </head>");
       html.append("

    <body>");
       html.append("
    <table width="75%" border="1">");
    }  private void trail() {
       int row = count % 3;
       if (row == 0) {
        html.append("
                    <td> </td>");
       }
       if (row == 1) {
        html.append("
                    <td> </td>");
        html.append("
                    <td> </td>");
       }
       html.append("
            </tr>");
       html.append("
    </table>");
       html.append("
    </body>");
       html.append("
    </html>");
    }
    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() {
    8.   FileWriter writer = null;
    9.   try {
    10.    File htmlFile = new File(srcPath + "/index.html");
    11.    writer = new FileWriter(srcPath + "/index.html");
    12.    writer.write(html.toString());
    13.    writer.flush();
    14.   } catch (IOException e) {
    15.    e.printStackTrace();
    16.   } finally {
    17.    if (writer != null) {
    18.     try {
    19.      writer.close();
    20.     } catch (IOException e1) {
    21.      e1.printStackTrace();
    22.     }
    23.    }
    24.   }
    25. }
    26. public static void main(String[] args) {
    27.   String srcPath = args[0];
    28.   if (srcPath==null){
    29.    printHelp();
    30.    return;
    31.   }
    32.   
    33.   Zoom zoom = new Zoom(srcPath);
    34.   zoom.process();
    35. }

    36. public static void printHelp(){
    37.   System.out.println("USAGE : java Zoom
    38.   ");
    39. }
    40. }
    复制代码


      
      
       
       

         
       

         
       
      

    复制代码

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

    使用道具 举报

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

    本版积分规则

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

    GMT+8, 2025-2-27 02:01 , Processed in 0.291612 second(s), 34 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

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