java压缩图片设置宽高

package html2pdf_2;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; 

import javax.imageio.ImageIO; 

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder; 

public class ImageZipUtil { 

    /**
     * 等比例压缩图片文件<br> 先保存原文件,再压缩、上传
     * @param oldFile  要进行压缩的文件
     * @param newFile  新文件
     * @param width  宽度 //设置宽度时(高度传入0,等比例缩放)
     * @param height 高度 //设置高度时(宽度传入0,等比例缩放)
     * @param quality 质量
     * @return 返回压缩后的文件的全路径
     */
    public static String zipImageFile(File oldFile,File newFile, int width, int height,
            float quality) {
        if (oldFile == null) {
            return null;
        }
        try {
            /** 对服务器上的临时文件进行处理 */
            Image srcFile = ImageIO.read(oldFile);
            int w = srcFile.getWidth(null);
        //  System.out.println(w);
            int h = srcFile.getHeight(null);
        //  System.out.println(h);
            double bili;
            if(width>0){
                bili=width/(double)w;
                height = (int) (h*bili);
            }else{
                if(height>0){
                    bili=height/(double)h;
                    width = (int) (w*bili);
                }
            }
            /** 宽,高设定 */
            BufferedImage tag = new BufferedImage(width, height,
                    BufferedImage.TYPE_INT_RGB);
            tag.getGraphics().drawImage(srcFile, 0, 0, width, height, null);
            //String filePrex = oldFile.getName().substring(0, oldFile.getName().indexOf(‘.‘));
            /** 压缩后的文件名 */
            //newImage = filePrex + smallIcon+  oldFile.getName().substring(filePrex.length()); 

            /** 压缩之后临时存放位置 */
            FileOutputStream out = new FileOutputStream(newFile); 

            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
            /** 压缩质量 */
            jep.setQuality(quality, true);
            encoder.encode(tag, jep);
            out.close(); 

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return newFile.getAbsolutePath();
    } 

    /**
     * 按宽度高度压缩图片文件<br> 先保存原文件,再压缩、上传
     * @param oldFile  要进行压缩的文件全路径
     * @param newFile  新文件
     * @param width  宽度
     * @param height 高度
     * @param quality 质量
     * @return 返回压缩后的文件的全路径
     */
    public static String zipWidthHeightImageFile(File oldFile,File newFile, int width, int height,
            float quality) {
        if (oldFile == null) {
            return null;
        }
        try {
            /** 对服务器上的临时文件进行处理 */
            Image srcFile = ImageIO.read(oldFile);
            int w = srcFile.getWidth(null);
            int h = srcFile.getHeight(null); 

            /** 宽,高设定 */
            BufferedImage tag = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
            tag.getGraphics().drawImage(srcFile, 0, 0, width, height, null);
            /** 压缩之后临时存放位置 */
            FileOutputStream out = new FileOutputStream(newFile); 

            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
            /** 压缩质量 */
            jep.setQuality(quality, true);
            encoder.encode(tag, jep);
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return newFile.toString();
    }

     /**
      *
      * @projectname  项目名称: html2pdf_2
      * @packageclass 包及类名: html2pdf_2.ImageZipUtil.java
      * @description  功能描述: 测试
      * @author       作        者: zhouzhiwei
      * @param                      参        数: @param args
      * @param                      参        数: @throws IOException
      * @return       返回类型: void
      * @createdate   建立日期: 2015年6月3日下午4:47:06
      */
        public static void main(String args[]) throws IOException {
        System.out.println(ImageZipUtil.zipImageFile(new File("d:/QQ截图20150602175637.png"),new File("E:/c2.jpg"), 400, 128, 3f));
  }
}
时间: 2024-08-01 09:55:38

java压缩图片设置宽高的相关文章

java读取远程url图片,得到宽高

链接地址:http://blog.sina.com.cn/s/blog_407a68fc0100nrb6.html import java.io.IOException;import java.awt.image.BufferedImage;import java.net.URL;import java.io.BufferedInputStream;import java.io.OutputStream;import java.io.FileOutputStream;import java.io

iOS学习-压缩图片(改变图片的宽高)

压缩图片,图片的大小与我们期望的宽高不一致时,我们可以将其处理为我们想要的宽高. 传入想要修改的图片,以及新的尺寸 -(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize { // Create a graphics image context UIGraphicsBeginImageContext(newSize); // Tell the old image to draw in this new cont

根据图片url地址获取图片的宽高

1 /** 2 * 根据img获取图片的宽高 3 * @param img 图片地址 4 * @return 图片的对象,对象中图片的真实宽高 5 */ 6 public BufferedImage getBufferedImage(String imgurl) { 7 URL url = null; 8 InputStream is = null; 9 BufferedImage img = null; 10 try { 11 url = new URL(imgurl); 12 HttpURL

js判断图片加载完成后获取图片实际宽高

通常,我们会用jq的.width()/.height()方法获取图片的宽度/高度或者用js的.offsetwidth/.offsetheight方法来获取图片的宽度/高度,但这些方法在我们通过样式设置了图片的宽高后获取的就不是图片的实际宽高,这显然在有些时候不是我们想要的结果,那么有没有一种方法来获取这样的实际宽高呢?答案是有的.下面的代码就能解决这样的问题: <img src="01.jpg" id="test" width="250px"

可替换元素—行级元素可以设置宽高?

上篇文章讲的是行级元素与块级元素,明明说了行级元素不可设置width,height属性,而为何img.input等行级元素可以设置宽.高??? 看官莫急,下面我会详细解答. 可替换元素 1. 替换元素就是浏览器根据元素的标签和属性,来决定元素的具体显示内容.例如浏览器会根据<img>标签的src属性的值来读取图片信息并显示出来,而 如果查看(X)HTML代码,则看不到图片的实际内容:又例如根据<input>标签的type属性来决定是显示输入框,还是单选按钮等. 2. 他们区别一般i

根据图片的链接获取图片的宽高

//根据图片的链接获取图片的宽高 - (CGSize)downloadImageSizeWithURL:(id)imageURL { NSURL *URL = nil; if([imageURL isKindOfClass:[NSURL class]]){ URL = imageURL; } if([imageURL isKindOfClass:[NSString class]]){ URL = [NSURL URLWithString:imageURL]; } if(URL == nil) r

不设置宽高,使元素居中的方法

在某些特定的场合,在没有知道宽度的情况下却要求水平居中,今找到些解决方法,现转过来一起学习学习 方案一.利用浮动的包裹性和相对定位百分比数据值特性,传说称之为“相对浮动” .unknow_width_center1 {position:relative; left:50%; float:left;}.unknow_width_center1 li {position:relative; right:50%; z-index:2; float:left}<</span>ul class=&

&lt;canvas&gt;设置宽高遇到的问题

在使用<canvas>元素时必须设置宽度和高度,指定可以绘画的区域大小.但是这里设置宽度和高度的时候有一个小问题. 样例代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>canvas绘图</title> </head> <body> <canvas id="drawimg"

行内元素能设置宽高吗

除了行内非替换元素,table-rows,and row groups 其他元素都能设置宽 除了行内非替换元素,table-colums,colum-groups 其他元素都能设置高 行内元素包含行内替换元素(img等)和行内非替换元素(a span等) 行内替换元素是可以设置宽高的,行内非替换元素是不能设置宽高的. 当给行内元素设置 float:left; float:right; 或者 position:absolute; position:fixed; 行内非替换元素也是可以设置宽高的,究