`

按比例压缩补白图片

阅读更多
之前是先补白后压缩,再一次改进后按比例压缩后补白生成图片。
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.awt.*;
import com.sun.image.codec.jpeg.*;
import java.awt.image.BufferedImage;
/**
*
* @author Administrator
*/
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);
            Tosmallerpic(url,new File(url+name),"_view",name,530,440,(float)0.7);
    }
/**
*
* @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);

               if(w2<=1||h2<=1){//源图比目标图小的时候
                    if(w2<=1){
                        if(h2<=1){
                            new_w=old_w;
                            new_h=old_h;
                        }else{
                            new_w=(int)Math.round(old_w/h2);
                            new_h=(int)Math.round(old_h/h2);
                        }
                    }else{
                        if(h2<=1){
                            if(w2<=1){
                                new_w=old_w;
                                new_h=old_h;
                            }else{
                                new_w=(int)Math.round(old_w/w2);
                                new_h=(int)Math.round(old_h/w2);
                            }
                        }
                    }
               }
               else//源图比目标图大的时候
               {
                   if(w2>=h2)
                   {
                       new_w=(int)Math.round(old_w/w2);
                       new_h=(int)Math.round(old_h/w2);
                   }else
                   {
                       new_w=(int)Math.round(old_w/h2);
                       new_h=(int)Math.round(old_h/h2);
                   }
               }
               BufferedImage tag = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
               //tag.getGraphics().drawImage(src,0,0,new_w,new_h,null); //绘制缩小后的图
               tag.getGraphics().setColor(Color.white);
               tag.getGraphics().fillRect(0,0,w,h);
               if(new_w==w&&new_h==h){
                   tag.getGraphics().drawImage(src.getScaledInstance(new_w, new_h,  Image.SCALE_SMOOTH), 0,0,null);
                }else{
                    if(new_w==w){
                        tag.getGraphics().drawImage(src.getScaledInstance(new_w, new_h,  Image.SCALE_SMOOTH), 0, Math.abs(h - new_h) / 2,null);
                    }else{
                        if(new_h==h){
                            tag.getGraphics().drawImage(src.getScaledInstance(new_w, new_h,  Image.SCALE_SMOOTH), Math.abs(w-new_w)/2,0,null);
                        }
                        else{
                            tag.getGraphics().drawImage(src.getScaledInstance(new_w, new_h,  Image.SCALE_SMOOTH), Math.abs(w-new_w)/2,Math.abs(h-new_h)/2,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="0e5465fc-025a-458d-8383-e9ced0c1e728.jpg";
        String f="F:\\200903300012\\pics\\201006\\";
        File file=new File(f);
       
        if(file.exists())
        {
            File[] filelist=file.listFiles();
            for(int i=0;i<filelist.length;i++)
            {
                String n=filelist[i].getName();
                Tosmallerpic(f,filelist[i],"_middle",n,185,160,(float)0.7);
                Tosmallerpic(f,filelist[i],"_small",n,45,45,(float)0.7);
                Tosmallerpic(f,filelist[i],"_smaller",n,116,100,(float)0.7);
                Tosmallerpic(f,filelist[i],"_view",n,530,440,(float)0.7);
            }
        }
    }*/
}
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics