程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> JAVA版的相冊制作程序

JAVA版的相冊制作程序

編輯:關於JAVA

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;
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>");
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved