java 生成原比例缩略图
来源:互联网 发布:死飞自行车淘宝 编辑:程序博客网 时间:2024/05/16 23:43
最近做手机页面,因为手机的缓存很小,需要图片显示时,要将图片的大小进行调整。所以研究了一下,查了些
文章,以这个http://www.webjx.com/htmldata/2005-04-25/1114436950.html为基础进行了一些修改可以自定义大小。
下面是代码:
我把上面的代码封装成一个类。
调用时
文章,以这个http://www.webjx.com/htmldata/2005-04-25/1114436950.html为基础进行了一些修改可以自定义大小。
下面是代码:
import java.io.File;
import java.io.FileOutputStream;
import java.awt.Image;
import java.awt.image.BufferedImage;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class MakeBreviaryImage {
public void MakeBreviaryImage(File image, int formatWideth,
int formatHeight, String savePath) throws Exception {
File _file = image;// get image file
Image src = javax.imageio.ImageIO.read(_file); // construct image
int imageWideth = src.getWidth(null); // get wideth of image
int imageHeight = src.getHeight(null); // get height of image
int changeToWideth = 0;
int changeToHeight = 0;
// set Breviary image's size
if (imageWideth > 0 && imageHeight > 0) {
// flag=true;
if (imageWideth / imageHeight >= formatWideth / formatHeight) {
if (imageWideth > formatWideth) {
changeToWideth = formatWideth;
changeToHeight = (imageHeight * formatWideth) / imageWideth;
} else {
changeToWideth = imageWideth;
changeToHeight = imageHeight;
}
} else {
if (imageHeight > formatHeight) {
changeToHeight = formatHeight;
changeToWideth = (imageWideth * formatHeight) / imageHeight;
} else {
changeToWideth = imageWideth;
changeToHeight = imageHeight;
}
}
}
// ---------
BufferedImage bufferedImage = new BufferedImage(changeToWideth,
changeToHeight, BufferedImage.TYPE_INT_RGB);
bufferedImage.getGraphics().drawImage(src, 0, 0, changeToWideth,
changeToHeight, null); // chage image size
FileOutputStream out = new FileOutputStream(savePath); // out put the
// image
JPEGImageEncoder encoder = null;
encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(bufferedImage); // like JPEG encoding
// System.out.print(width+"*"+height);
out.close();
}
}
import java.io.FileOutputStream;
import java.awt.Image;
import java.awt.image.BufferedImage;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class MakeBreviaryImage {
public void MakeBreviaryImage(File image, int formatWideth,
int formatHeight, String savePath) throws Exception {
File _file = image;// get image file
Image src = javax.imageio.ImageIO.read(_file); // construct image
int imageWideth = src.getWidth(null); // get wideth of image
int imageHeight = src.getHeight(null); // get height of image
int changeToWideth = 0;
int changeToHeight = 0;
// set Breviary image's size
if (imageWideth > 0 && imageHeight > 0) {
// flag=true;
if (imageWideth / imageHeight >= formatWideth / formatHeight) {
if (imageWideth > formatWideth) {
changeToWideth = formatWideth;
changeToHeight = (imageHeight * formatWideth) / imageWideth;
} else {
changeToWideth = imageWideth;
changeToHeight = imageHeight;
}
} else {
if (imageHeight > formatHeight) {
changeToHeight = formatHeight;
changeToWideth = (imageWideth * formatHeight) / imageHeight;
} else {
changeToWideth = imageWideth;
changeToHeight = imageHeight;
}
}
}
// ---------
BufferedImage bufferedImage = new BufferedImage(changeToWideth,
changeToHeight, BufferedImage.TYPE_INT_RGB);
bufferedImage.getGraphics().drawImage(src, 0, 0, changeToWideth,
changeToHeight, null); // chage image size
FileOutputStream out = new FileOutputStream(savePath); // out put the
// image
JPEGImageEncoder encoder = null;
encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(bufferedImage); // like JPEG encoding
// System.out.print(width+"*"+height);
out.close();
}
}
调用时
public class TestMakeBreviaryImage {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
File testImage = new File("需要进行转换的图片文件路径");
MakeBreviaryImage test = new MakeBreviaryImage();
test.MakeBreviaryImage(testImage,60,50,"C:/newfile.jpg");//参数一次为:图片文件,需要的宽度,需要的高度,输出路径
}
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
File testImage = new File("需要进行转换的图片文件路径");
MakeBreviaryImage test = new MakeBreviaryImage();
test.MakeBreviaryImage(testImage,60,50,"C:/newfile.jpg");//参数一次为:图片文件,需要的宽度,需要的高度,输出路径
}
}
- java 生成原比例缩略图
- java生成原比例缩略图
- php原比例生成缩略图
- ASP.NET C# 按原图片大小等比例缩放生成缩略图
- 按模版比例生成缩略图
- php 等比例生成缩略图
- PHP等比例生成缩略图
- java实现等比例缩略图
- 用JavaScript自动生成等比例缩略图
- C#生成缩略图 等比例缩放
- C#生成缩略图 等比例缩放
- AspJpeg组件,按宽高比例,真正生成缩略图
- Java实现图片等比例缩略图
- [蛙蛙推荐]用JS自动生成等比例缩略图
- 用AspJpeg组件,按宽高比例,真正生成缩略图
- 用AspJpeg组件,按宽高比例,真正生成缩略图
- Asp.net 图片等比例生成缩略图,外带一个数学公式
- php实现等比例生成图片缩略图不失真
- window.external的使用
- The annual conference of JoyoCode Blog Website
- 十二月的梵高
- 高级 DAO 编程
- Transact_SQL的简单介绍
- java 生成原比例缩略图
- RSA算法详解及C语言实现
- Windows Mobile 5.0横竖屏自识别技术
- 钩子的原理
- 谈谈什么时候应该记录异常堆栈信息。
- 拓扑排序程序
- 记一次u盘病毒的清除
- 解决问题(十)——HashCodeBuilder在one-to-many关系中出现Lazy异常
- 养成总结的习惯