看过别人制作缩略图的方法。
http://caiceclb.iteye.com/blog/627219
http://hi.baidu.com/pep525/blog/item/7d068e3b9aa900ef15cecba4.html
有点不太满意:
无法识别原始图像的格式,要么全转为jpg,要么显式写死转换格式。
参考
http://www.exampledepot.com/egs/javax.imageio/DiscType.html
稍微改版了一下。图片转换为原始图像的格式。
省却网站编辑的时候图片上传之前需要缩小适合页面,节省时间。
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
public class ImageUtils {
/**
* 按原始比例缩小图片至targetLength大小,并写入源文件(覆盖)。如果图片目标实际小于targetLength,则保持图像不变。
* isWidth参数表示targetLength 指的是宽度还是高度,true为宽度。
* 此方法在WEB应用中,可以方便裁剪提交上来的过大的图像,而不失真。
* @param imgsrc
* @param targetLength
* @param isWidth
*/
public static void reduceImg(String imgsrc, int targetLength,boolean isWidth) {
try {
File srcfile = new File(imgsrc);
if (!srcfile.exists()) {
return;
}
Image src = ImageIO.read(srcfile);
//原始图像高和宽
int width = src.getWidth(null);
int height = src.getHeight(null);
int widthdist = 0;
int heightdist = 0;
//确定图像的缩放后的高和宽
if(isWidth){
if(targetLength >= width) return;
double scale = targetLength * 1.0/ width;
widthdist = targetLength;
heightdist = (int) (height*scale);
}else{
if(targetLength >= height) return;
double scale = targetLength * 1.0/ height;
widthdist = (int) (width*scale);
heightdist = targetLength;
}
BufferedImage tag= new BufferedImage((int) widthdist, (int) heightdist,
BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src.getScaledInstance(widthdist, heightdist, Image.SCALE_FAST), 0, 0, null); //根据缩略图要求品质可以选择 Image.SCALE_SMOOTH
String formatName = getFormatName(srcfile);//此句必须在new FileOutputStream之前,因为是替换原图,FileOutputStream对象未关闭之前,ImageInputStream无法获得文件格式。
FileOutputStream out = new FileOutputStream(srcfile);
ImageIO.write(tag, formatName, out);
out.flush();
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
private static String getFormatName(File o) {
try {
// Create an image input stream on the image
ImageInputStream iis = ImageIO.createImageInputStream(o);
// Find all image readers that recognize the image format
Iterator iter = ImageIO.getImageReaders(iis);
if (!iter.hasNext()) {
// No readers found
return null;
}
// Use the first reader
ImageReader reader = (ImageReader)iter.next();
// Close stream
iis.close();
// Return the format name
return reader.getFormatName();
} catch (IOException e) {
}
// The image could not be read
return null;
}
public static void main(String[] args){
reduceImg("d:/test.jpg", 600,true);
}
}
分享到:
相关推荐
php按比例缩放图片 php按比例缩放图片 php按比例缩放图片
CAD中实现缩放地形图的比例时而高程不变的方法,保持等高线等的高程不变
可以根据比例对图片进行缩放,通过缩放图片的大小,并可对图片进行移动
更有效率的处理图片。 有与要求不一样比例的图片的时候,按原图比例缩小。 只有一个静态方法。看注解。
按比例缩放图片
在WEB开发中,当指定图片的大小时,会使图片变形,用JS脚本实现图片的按比例缩放.
刚写的一个上传图片大小比例缩放,希望大家继续改良,改良了要通知我啊.交流交流,
asp上传图片后按比例缩小图片,不变形。测试可用。
图片如果直接限制其高度或宽度,就会使图片变形,失真 这里提供按比例缩放图片解决的这个问题 js文件很小,只有0.5KB 但是这个还有其他方法,就是上传的时候按比例生产缩略图
主要介绍了JS图片等比例缩放方法,结合完整实例形式分析了javascript针对页面图片元素属性操作的相关技巧,需要的朋友可以参考下
js实现图片等比例缩放 我将网络上流传的,改成了横向居中和纵向居中,非常实用。
按比例缩放图片,JavaScript代码.rar按比例缩放图片,JavaScript代码.rar按比例缩放图片,JavaScript代码.rar按比例缩放图片,JavaScript代码.rar按比例缩放图片,JavaScript代码.rar按比例缩放图片,JavaScript代码...
图片等比例缩放,jQuery和js的两种图片等比例缩放效果,简单实用。
使用一个方法可以等比例缩小你的,让图片不失去原本的效果。
从开源代码中总结的php图片等比例缩放 测试可用
NULL 博文链接:https://vipshow.iteye.com/blog/1848912
目标检测批量按比例缩放图片。
java 处理图片大小等比例缩放;图片裁剪;支持图片压缩不失真;支持图片添加水印;支持动态图的处理;支持自定义图片大小的转换 等 压缩包中内附使用说明
本工具由.NET编写,可以批量缩放图片,也可批量更改图片的格式,可限制最大尺寸进行等比例缩放,可编辑保存脚本
图像等比例缩放 等比例缩放图片。。。。。。。。。。。。。。。。。。。。。。