图片缩略图的实现,比较灵活[可直接使用]
来源:互联网 发布:适合做网络推广的行业 编辑:程序博客网 时间:2024/06/05 20:47
iLife's 博客http://blog.csdn.net/fei1502816
其中
originalPath是图片源地址,
newPath是生成缩略图的目的地址,
newWidth、newHeight为缩略图的宽度和高度。
看代码
- //改变图像宽和高,维持宽高比
- public static void changeImagePixel(String originalPath,String newPath,int newWidth,int newHeight){
- //读入内存
- BufferedImage bi=null;
- try {
- bi = ImageIO.read(new File(originalPath));
- //原始宽、高
- int originalWidth=bi.getWidth();
- int originalHeight=bi.getHeight();
- //宽、高比,默认1,即新宽、高和原始宽、高一样
- double ratio=1;
- //原始宽、高比,最终将维持该比例
- double originalRatio=(double)originalWidth/originalHeight;
- //文件后缀名
- String fileType = originalPath.substring(originalPath.lastIndexOf("."));
- String newFileType="jpg";
- if(fileType.equals("png") || fileType.equals("PNG")){
- newFileType="png";
- }
- //如果图片宽度或者高度超出给定范围
- if(originalWidth>newWidth || originalHeight>newHeight){
- if(newWidth < (int)(Math.floor(newHeight * originalRatio))){
- //以宽度为准,高度自动,维持原始比例
- ratio = (double)newWidth / originalWidth;
- } else {
- //以高度为准,宽度自动,维持原始比例
- ratio = (double)newHeight / originalHeight;
- }
- }
- AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
- Image newImage = op.filter(bi, null);
- try {
- //如果目录不存在,则创建
- //File newPathDir=new File(newPath.substring(0,newPath.lastIndexOf("\\")+1));
- //if(!newPathDir.exists()) newPathDir.mkdirs();
- ImageIO.write((BufferedImage) newImage, newFileType, new File(newPath));
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
0 0
- 图片缩略图的实现,比较灵活[可直接使用]
- 图片缩略图的实现,比较灵活[可直接使用]
- php封装类实现的图片上传可直接引用
- 使用libjpeg库实现jpeg图片的缩放(缩略图)
- 使用libjpeg库实现jpeg图片的缩放(缩略图)
- asp.net中使用Uploadify插件实现多张图片上传,上传后可显示缩略图、删除图片
- java 程序实现对图片的压缩生成缩略图并可设定长宽、尺寸压缩率、图片质量
- java 程序实现对图片的压缩生成缩略图并可设定长宽、尺寸压缩率、图片质量
- java 程序实现对图片的压缩生成缩略图并可设定长宽、尺寸压缩率、图片质量
- 可实时预览缩略图的上传图片界面
- 可预览缩略图(实时预览)的上传图片界面
- 可预览缩略图(实时预览)的上传图片界面
- FCKEditor 的图片浏览改进!!可查看大图。。有缩略图
- 图片上传生成缩略图,并使用JQuery实现鼠标移动到缩略图显示相应的大图的例子
- imagemagic实现gif图片的缩略图
- QListWidget实现图片缩略图形式的列表
- js实现带缩略图的图片转换
- php使用GD生成图片的缩略图
- java.lang.UnsatisfiedLinkError: D:\Tomcat\apache-tomcat-7.0.37\bin\tcnative-1.dll: Can't load AMD 6
- 优先队列C++实现和应用
- Swift 对象方法
- 关乎戴尔笔记本从win8.1换成win7
- java 数据库blob转word
- 图片缩略图的实现,比较灵活[可直接使用]
- 使用Picasso加载图片的内存优化实践
- CAD控件:界面控制说明和方法
- Netty源码分析之DelimiterBasedFrameDecoder
- locale internal
- 《转》《招聘一个靠谱的 iOS》—参考答案(下)
- freeswitch注册超时机制
- windows下面常用的渗透测试命令(安全工具)
- 小解差速齿轮