图片的压缩和缩放

 /**
     * 对文件进行压缩,调用方法
     * @throws Exception
     * @author wud
     * @param path(图片的全路径)
     * */
    public static void compressPicture(String path) throws Exception{
     
        if (StringUtil.isNullOrEmpty(path)) {
            throw new BaseException("上传图片路径错误");
        }
        
        String format = FileUtil.getFilename(path);
        File file=new File(path);
        float flt=0.7f;
        int wh=1024;
        dealPictures(file,format,wh,flt,path);
    }
   /**
    * 文件压缩与缩放
    * @param file    文件路径(file)
    * @param format  文件格式
    * @param wh      高宽最大尺寸
    * @param per     百分比
    * @param path    文件路径(String)
    *
    *
    *
    *
    * */
    public static void dealPictures(File file,String format,int wh,float per,String path){
       Image   src;
                 try {
                    src=ImageIO.read(file);
                       //构造Image对象
                     
                 int old_w= src.getWidth(null);    //源图宽
                 int old_h= src.getHeight(null);
         Image newIm = null;                                 //定义处理后的Image
                 
           if (old_w>1024 || old_h>1024) {
                    if(old_w>old_h){
                        newIm = src.getScaledInstance(wh, (wh * old_h)  
                                / old_w, Image.SCALE_SMOOTH);
                    }
                    else{
                        newIm = src.getScaledInstance((wh * old_w) / old_h,  
                                wh, Image.SCALE_SMOOTH);  
                    }
               
                 
                 Image temp = new ImageIcon(newIm).getImage();         //获取图片
                
                 BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null),  
                         temp.getHeight(null), BufferedImage.TYPE_INT_RGB);
                
                 Graphics g = bufferedImage.createGraphics();          //获取绘图对象
                 g.setColor(Color.white);
              
                 g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));    //绘制背景。
                 g.drawImage(temp, 0, 0, null);  
                 g.dispose();  
                 
           /*BufferedImage image_to_save = new BufferedImage(temp.getWidth(null), bufferedImage.getHeight(),
                   BufferedImage.TYPE_INT_RGB);*/
                  bufferedImage.getGraphics().drawImage(src.getScaledInstance(temp.getWidth(null), temp.getHeight(null),
                         Image.SCALE_SMOOTH), 0, 0, null);
                  FileOutputStream fos;
                
                    fos = new FileOutputStream(path);
          
               
               //新方法
    
                saveAsJPEG(100, bufferedImage, per, fos,format);

              fos.close();
           }
           //高宽都小于1024
           else{
               BufferedImage bi2 = new BufferedImage(old_w, old_h,
                       BufferedImage.TYPE_INT_RGB);
               //创建图像上下文的抽象类
               Graphics2D g2d = bi2.createGraphics();
               
               // 绘制指定图像中当前可用的图像。
               g2d.drawImage(src, 0, 0, null);
               
               // 释放此图形的上下文以及它使用的所有系统资源
                g2d.dispose();
                
               //数据输出                
                 ImageIO.write(bi2,format,file);
               
           }
                 }
                 catch (IOException e1) {
                     // TODO Auto-generated catch block
                     e1.printStackTrace();
                 }  
                 
    }
    public static void saveAsJPEG(Integer dpi, BufferedImage image_to_save, float JPEGcompression, FileOutputStream fos,String format)
            throws IOException {

        ImageWriter imageWriter = ImageIO.getImageWritersBySuffix("jpg").next();//此处格式“jpg”是固定的
        ImageOutputStream ios = ImageIO.createImageOutputStream(fos);
        imageWriter.setOutput(ios);
        //and metadata
        IIOMetadata imageMetaData = imageWriter.getDefaultImageMetadata(new ImageTypeSpecifier(image_to_save), null);

        if (dpi != null && !dpi.equals("")) {
    
            //new metadata
            Element tree = (Element) imageMetaData.getAsTree("javax_imageio_jpeg_image_1.0");
            Element jfif = (Element) tree.getElementsByTagName("app0JFIF").item(0);
            jfif.setAttribute("Xdensity", Integer.toString(dpi));
            jfif.setAttribute("Ydensity", Integer.toString(dpi));

        }

        if (JPEGcompression >= 0 && JPEGcompression <= 1f) {

           
            JPEGImageWriteParam jpegParams = (JPEGImageWriteParam) imageWriter.getDefaultWriteParam();
            jpegParams.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT);
            jpegParams.setCompressionQuality(JPEGcompression);

        }

        //old write and clean
        //jpegEncoder.encode(image_to_save, jpegEncodeParam);

        //new Write and clean up
        imageWriter.write(imageMetaData, new IIOImage(image_to_save, null, null), null);
        ios.close();
        imageWriter.dispose();

    }
   
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值