当前位置:编程学习 > JAVA >>

图片缩略图的实现,比较灵活[可直接使用]

 其中

originalPath是图片源地址,

newPath是生成缩略图的目的地址,

newWidth、newHeight为缩略图的宽度和高度。

看代码

[java] //改变图像宽和高,维持宽高比     
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();    
    }    
}   
//改变图像宽和高,维持宽高比  
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";  
        }  
     &nbs

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,