java实现图像缩略
来源:互联网 发布:网站建设用什么软件 编辑:程序博客网 时间:2024/04/27 22:30
/**
* 根据新输入的长宽生成图片缩略图
* @param fileName
* @param width
* @param height
* @return
* @throws IOException
*/
public static boolean setShrinkPhoto(String fileName, int width, int height)
throws IOException {
File file = new File(fileName); // 读入文件
Image src = javax.imageio.ImageIO.read(file); // 构造Image对象
int srcWidth = src.getWidth(null); // 得到源图宽
int srcHeight = src.getHeight(null); // 得到源图长
if (srcWidth > width || srcHeight > height) { // 如果文件本身较小则取消缩略图
BufferedImage tag = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0, width, height, null); // 绘制缩小后的图
FileOutputStream out = new FileOutputStream(fileName); // 输出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag); // 近JPEG编码
out.close();
}
return true;
}
/**
* 根据新输入的缩放比例生成图片缩略图
* @param fileName
* @param width
* @param height
* @return
* @throws IOException
*/
public static boolean setShrinkPhoto(String fileName, float width, float height)
throws IOException {
File file = new File(fileName); // 读入文件
Image src = javax.imageio.ImageIO.read(file); // 构造Image对象
int srcWidth = src.getWidth(null); // 得到源图宽
int srcHeight = src.getHeight(null); // 得到源图长
BufferedImage tag = new BufferedImage((int) (srcWidth * width),
(int) (srcHeight * height), BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0, (int) (srcWidth * width),
(int) (srcHeight * height), null); // 绘制缩小后的图
FileOutputStream out = new FileOutputStream(fileName); // 输出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag); // 近JPEG编码
out.close();
return true;
}
- java实现图像缩略
- GDI创建缩略图像
- 图像缩略与加水印
- Java生成缩略图片
- C#图像处理(剪切、合成、缩略)
- C#图像处理(剪切、合成、缩略)
- C#图像处理(剪切、合成、缩略)
- css实现标题缩略成"..."
- Java 自动生成缩略图片
- java script 显示缩略词
- java实现图像识别
- Java实现-旋转图像
- css实现标题缩略显示方法
- JAVA 图片处理(缩略、裁剪)
- Java源码-图片缩略小工具
- 【PHP】PHP图像裁剪缩略裁切类源代码及使用方法
- Java实现图像全屏显示
- 使用Java实现图像分割
- 房子
- java生成缩略图代码
- 常用的JS代码
- Ogre的骨骼动画
- 对jpeg格式图像进行压缩
- java实现图像缩略
- 续
- 文摘
- 最新行程安排[08月07日-08月14日]
- 来个自我介绍吧
- 使用 .htaccess 提高 WordPress 的安全性和可用性
- XmlDocument 读取中 InnerText 和 InnerXml 的区别
- 关于#Pragma
- #ifndef和#pragma once的不同