图片压缩 2

public class Img_Middle {
    public void img_change(String url,String name)
    {
            Tosmallerpic(url,new File(url+name),"_middle",name,188,165,(float)0.7);
            Tosmallerpic(url,new File(url+name),"_small",name,45,45,(float)0.7);
            Tosmallerpic(url,new File(url+name),"_smaller",name,116,100,(float)0.7);
    }

   private static void img_change11(String url,String name)
    {
            File  f=new File(url+name);
            float per =(300*1024)/f.length();
            if(f.length()>300*1024){
                per =(float)(300*1024)/f.length();
                per =(float)(per+(1-per)/5);
            }else{
               
                per=(float)1.0;
            }
            System.out.println("图片大小:"+f.length()/1024+"K" );
            System.out.println("压宿比例:"+per );
            //Tosmallerpic(url,new File(url+name),"_middle",name,1440,900,(float)0.7);
            Tosmallerpic(url,new File(url+name),"_small",name,1024,1024,(float)0.7);
            Tosmallerpic(url,new File(url+name),"_smaller",name,1024,500,(float)0.7);
            Tosmallerpic(url,new File(url+name),"_middle",name,1024,640,(float)per);
    }
/**
*
* @param f 图片所在的文件夹路径
* @param filelist 图片路径
* @param ext 扩展名
* @param n 图片名
* @param w 目标宽
* @param h 目标高
* @param per 百分比
*/
      private static void  Tosmallerpic(String f,File filelist,String ext,String n,int w,int h,float per){
            Image src;
            try {
                src = javax.imageio.ImageIO.read(filelist); //构造Image对象

               String img_midname=f+n.substring(0,n.indexOf("."))+ext+n.substring(n.indexOf("."));
               int old_w=src.getWidth(null); //得到源图宽
               int old_h=src.getHeight(null);
               int new_w=0;
               int new_h=0; //得到源图长

               double w2=(old_w*1.00)/(w*1.00);
               double h2=(old_h*1.00)/(h*1.00);

               //图片跟据长宽留白,成一个正方形图。
               BufferedImage oldpic;
               /*
               if(old_w>old_h)
               {
                   oldpic=new BufferedImage(old_w,old_w,BufferedImage.TYPE_INT_RGB);
               }else{
                  if(old_w<old_h){
                     oldpic=new BufferedImage(old_h,old_h,BufferedImage.TYPE_INT_RGB);
                  }else{
                    oldpic=new BufferedImage(old_w,old_h,BufferedImage.TYPE_INT_RGB);
                 }    
               }*/
                oldpic=new BufferedImage(old_w,old_h,BufferedImage.TYPE_INT_RGB); 
                Graphics2D g = oldpic.createGraphics();
                g.setColor(Color.white);
               
                if(old_w>old_h)
                {
                    g.fillRect(0, 0, old_w, old_w);
                    g.drawImage(src, 0, 0, old_w, old_h, Color.white, null);
                    //g.fillRect(0, 0, old_w, old_w);
                    //g.drawImage(src, 0, (old_w - old_h) / 2, old_w, old_h, Color.white, null);
                }else{
                    if(old_w<old_h){
                    g.fillRect(0,0,old_h,old_h);
                    g.drawImage(src, 0, 0, old_w, old_h, Color.white, null);
                    //g.fillRect(0,0,old_h,old_h);
                    //g.drawImage(src, (old_h - old_w) / 2, 0, old_w, old_h, Color.white, null);
                    }else{
                        //g.fillRect(0,0,old_h,old_h);
                        g.drawImage(src.getScaledInstance(old_w, old_h,  Image.SCALE_SMOOTH), 0,0,null);
                    }
                }  
               
                g.fillRect(0,0,old_w,old_h);
                g.drawImage(src, 0, 0, old_w, old_h, Color.white, null);           
                g.dispose();
                src = oldpic;
                //图片调整为方形结束
               if(old_w>w)
               new_w=(int)Math.round(old_w/w2);
               else
                   new_w=old_w;
               if(old_h>h)
                   new_h=(int)Math.round(old_h/h2);//计算新图长宽
               else
                   new_h=old_h;
               new_h=h;
               new_w=w;
              
               BufferedImage tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);       
               //tag.getGraphics().drawImage(src,0,0,new_w,new_h,null); //绘制缩小后的图
               tag.getGraphics().drawImage(src.getScaledInstance(new_w, new_h,  Image.SCALE_SMOOTH), 0,0,null);
               FileOutputStream newimage=new FileOutputStream(img_midname); //输出到文件流
               JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
               JPEGEncodeParam jep=JPEGCodec.getDefaultJPEGEncodeParam(tag);
                /* 压缩质量 */
               jep.setQuality(per, true);
               encoder.encode(tag, jep);
               //encoder.encode(tag); //近JPEG编码
               newimage.close();
               } catch (IOException ex) {
                Logger.getLogger(Img_Middle.class.getName()).log(Level.SEVERE, null, ex);
            }
    }

    /**/public static void main(String args[]){
        String n="aa.jpg";
        String f="e:\\";
        File file=new File(f);
       

        img_change11(f,n) ;

       
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值