JAVA版的相册制作程序

一次,偶然在 ZhangLiHai.com上发现了一个相册制作程序,核心功能基本上有了,我只是稍加修改了生成html文件的代码。
在此基础上稍加修改,还可以在相片上追加文字、图片等,各位就根据自己的需要再加工吧!
 

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 com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
 * @author 丁令 Zhanglihai
 *
 * 此程序在Zhanglihai相册制作程序基础上修改而来
 *
 * 程序会在指定目录下生成index.html文件及略缩图目录
 * 具体效果请参见: http://www.dingl.com/photo/dingl/20041219-borland-fb/
 */
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);
   //根据目标图片建立一个缓存图片
   JPEGImageDecoder decoderFile = JPEGCodec.createJPEGDecoder(imageStream);
   BufferedImage imageFile = decoderFile.decodeAsBufferedImage();
   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;
    }
   });
   //编码输出
   JPEGImageEncoder jpeg = JPEGCodec.createJPEGEncoder(out);
   jpeg.encode(bufImage);
   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'><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("/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() {
  count = 0;
  html = new StringBuffer();
  html.append("<html>");
  html.append("/n<head>");
  html.append("/n<meta http-equiv=/"Content-Type/" content=/"text/html; charset=gb2312/">");
  html.append("/n<title>").append(getDirName()).append("</title>");
  html.append("/n</head>");
  html.append("/n/n<body>");
  html.append("/n<table width='75%' border='1'>");
 }
 private void trail() {
  int row = count % 3;
  if (row == 0) {
   html.append("/n/t/t<td>&nbsp;</td>");
  }
  if (row == 1) {
   html.append("/n/t/t<td>&nbsp;</td>");
   html.append("/n/t/t<td>&nbsp;</td>");
  }
  html.append("/n/t</tr>");
  html.append("/n</table>");
  html.append("/n</body>");
  html.append("/n</html>");
 }
 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 <FILEPATH>");
 }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值