您的位置:首页 > 编程语言 > Java开发

使用java将网页保存为mht格式(1)

2009-03-12 21:20 435 查看
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import com.sun.image.codec.jpeg.*;

public class ImageUtil {
private String srcFile;
private String destFile;
private int width;
private int height;
private Image img;

public ImageUtil(String srcFile) throws IOException {
File _file = new File(srcFile);
this.srcFile = srcFile;
this.destFile = srcFile;
img = javax.imageio.ImageIO.read(_file);
width = img.getWidth(null);
height = img.getHeight(null);
}

public void resize(int w, int h) throws IOException {
BufferedImage _image = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB);
_image.getGraphics().drawImage(img, 0, 0, w, h, null);
FileOutputStream out = new FileOutputStream(destFile);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(_image);
out.close();
}

public void resize(double t) throws IOException {
int w = (int) (width * t);
int h = (int) (height * t);
resize(w, h);
}

public void resizeByWidth(int w) throws IOException {
int h = (int) (height * w / width);
resize(w, h);
}

public void resizeByHeight(int h) throws IOException {
int w = (int) (width * h / height);
resize(w, h);
}

public void resizeFix(int w, int h) throws IOException {
if (width / height > w / h) {
resizeByWidth(w);
} else {
resizeByHeight(h);
}
}

public int getSrcWidth() {
return width;
}

public int getSrcHeight() {
return height;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: