缩略图片

package  utils;

import  java.awt.Image;

import  java.awt.image.BufferedImage;
import  java.awt.image.RenderedImage;
import  java.io.File;
import  java.io.OutputStream;
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  Photo {

    
public   static   void  reduceImg(OutputStream out, String path,  int  width,  int  height) {
        
try  {
            File srcfile 
=   new  File(path);
            
if  ( ! srcfile.exists()) {
                System.out.println(
" 缩放图片文件不存在! " );
                
return ;
            }
            BufferedImage src 
=  ImageIO.read(srcfile);

            
//  得到原始宽和高
             int  w  =  src.getWidth();
            
int  h  =  src.getHeight();
            
double  sx  =  ( double ) width  /  w;
            
double  sy  =  ( double ) height  /  h;
            
if  (sx  <  sy) {
                sy 
=  sx;
                height 
=  ( int ) (sy  *  h);
            } 
else  {
                sx 
=  sy;
                width 
=  ( int ) (sy  *  w);
            }
            System.out.println(String.format(
" %s, %s " , w, h));
            System.out.println(String.format(
" %s, %s " , width, height));

            BufferedImage source 
=   new  BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            
//  Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢
            source.getGraphics().drawImage(
                    src.getScaledInstance(width, height, Image.SCALE_SMOOTH), 
0 0 null );
            JPEGImageEncoder encoder 
=  JPEGCodec.createJPEGEncoder(out);
            JPEGEncodeParam param 
=  encoder.getDefaultJPEGEncodeParam(source);
            param.setQuality(70f, 
true );
            encoder.encode(source, param);
        } 
catch  (IOException ex) {
            ex.printStackTrace();
        }
    }

    
/**
     * 根据图片路径 读取图片文件
     * 
     * 
@param  fileName
     * 
@return
     
*/
    
public   static  BufferedImage readImage(String fileName) {
        BufferedImage bi 
=   null ;
        
try  {
            bi 
=  ImageIO.read( new  File(fileName));
        } 
catch  (IOException ioe) {
            ioe.printStackTrace();
        }
        
return  bi;
    }

    
/**
     * 生成新的图片文件
     * 
     * 
@param  im
     * 
@param  formatName
     * 
@param  fileName
     * 
@return
     
*/
    
public   static   boolean  writeImage(RenderedImage im, String formatName, String fileName) {
        
boolean  result  =   false ;
        
try  {
            result 
=  ImageIO.write(im, formatName,  new  File(fileName));
        } 
catch  (IOException ioe) {
            ioe.printStackTrace();
        }
        
return  result;
    }

    
/**
     * 转换图片格式 到 jpg
     * 
     * 
@param  im
     * 
@param  fileName
     * 
@return
     
*/
    
public   static   boolean  writeJPEGImage(RenderedImage im, String fileName) {
        
return  writeImage(im,  " JPEG " , fileName);
    }

    
/**
     * 转换图片格式 到 gif 不知到好用不
     * 
     * 
@param  im
     * 
@param  fileName
     * 
@return
     
*/
    
public   static   boolean  writeGIFImage(RenderedImage im, String fileName) {
        
return  writeImage(im,  " GIF " , fileName);
    }

    
public   static   boolean  writePNGImage(RenderedImage im, String fileName) {
        
return  writeImage(im,  " PNG " , fileName);
    }

    
public   static   boolean  writeBMPImage(RenderedImage im, String fileName) {
        
return  writeImage(im,  " BMP " , fileName);
    }
}
调用:
Code

你可能感兴趣的:(缩略图)