JAVA 图片的缩放,和压缩,PNG背景透明

来源:互联网 发布:中金云金融大数据公司 编辑:程序博客网 时间:2024/04/29 23:18
public class ImgFactory {
   private Logger log=LoggerFactory.getLogger(ImgFactory.class);
   private File _saveFile;
   private BufferedImage _sourceBufferedImage;
   
   
   
   public static final int LETF=1;
   public static final int RIGHT=2;
   public static final int CENTER=3;
   public static final int ABOVE=4;
   public static final int BELOW=5;
   
   private BufferedImage _waterMarker;
   
   private int markerX;
   private int markerY;
   
   private int markerWidth;
   private int markerHeight;
   public ImgFactory(String soursePath,String savePath) throws IOException{
       this(new File(soursePath),new File(savePath));
   }
   
   public ImgFactory(File _sourseFile,File _saveFile) throws IOException{
       if(_sourseFile==null || _saveFile==null){
           throw new NullPointerException("图片文件不能为null");
       }
       if(!_sourseFile.exists()){
           throw new FileNotFoundException(_sourseFile.getAbsolutePath()+"not found!");
       }
       this._sourceBufferedImage=ImageIO.read(_sourseFile);
       this._saveFile=_saveFile;
   }
   
   public void compress(int width,int height) throws IOException{
       //---缩放图片
       Image _compressImage=_sourceBufferedImage.getScaledInstance(width, height,Image.SCALE_SMOOTH);
       //----图像透明,设置
       BufferedImage _bufferedImage=new BufferedImage(width, height,this._sourceBufferedImage.getType());
       Graphics graphics=_bufferedImage.getGraphics();
       Graphics2D graphics2d=(Graphics2D) graphics;
       //---绘制图片
       graphics2d.drawImage(_compressImage, 0, 0, null);
       saveImg(_bufferedImage);
       
   }
   
   
   public void compress(float radio) throws IOException{
       int _width=_sourceBufferedImage.getWidth();
       int _height=_sourceBufferedImage.getHeight();
       float _toWidth=_width;
       float _toHeight=_height;
       if(radio!=0){
           _toWidth*=radio;
           _toHeight*=radio;
       }
       compress(Math.round(_toWidth), Math.round(_toHeight));
   }
   
   public void setWaterMarker(String imgPath,int x,int y,int width,int height) throwsIOException{
       this._waterMarker=ImageIO.read(new File(imgPath));
       this.markerX=x;
       this.markerY=y;
       this.markerWidth=width;
       this.markerHeight=height;
   }
   
   
   public void zoom(int width,int height,boolean isCompress) throws IOException{
       if(isCompress){
           compress(width, height);
       }else{
           BufferedImage _bufferedImage=new BufferedImage(width, height,this._sourceBufferedImage.getType());
           Graphics graphics=_bufferedImage.getGraphics();
           Graphics2D graphics2d=(Graphics2D) graphics;
           //---绘制图片
           graphics2d.drawImage(this._sourceBufferedImage,0,0,width,height,null);
           //--
           saveImg(_bufferedImage);
       }
   }
   
   public void zoom(float radio,boolean isCompress) throws IOException{
       int _width=_sourceBufferedImage.getWidth();
       int _height=_sourceBufferedImage.getHeight();
       float _toWidth=_width;
       float _toHeight=_height;
       if(radio!=0){
           _toWidth*=radio;
           _toHeight*=radio;
       }
       zoom(Math.round(_toWidth), Math.round(_toHeight) ,isCompress);
   }
   
   public String getFileSuffix(File file) {
       if (file == null) {
           return null;
       }
       
       return getFileSuffix(file.getName());
   }
   
   
   public String getFileSuffix(String fileName) {
       if (fileName == null){
           return null;
       }
       String suffix = "";
       int i = fileName.lastIndexOf('.');
       if (i > 0 && i < fileName.length() - 1) {
           suffix = fileName.substring(i + 1);
       }
       return suffix;
   }
   
   public void setSourceFile(File sourseFile) throws IOException{
       this._sourceBufferedImage=ImageIO.read(sourseFile);
   }
   
   public void setSourceFile(String path) throws IOException{
       setSaveFile(new File(path));
   }
   
   
   public void setSaveFile(File _saveFile) {
       this._saveFile = _saveFile;
   }

   public void setSaveFile(String path){
       setSaveFile(new File(path));
   }
   
   public void saveImg(BufferedImage bufferedImage) throws IOException{
       Graphics2D graphics2d=(Graphics2D)bufferedImage.getGraphics();
       //----水印图片不等于NULL,给图片添加水印
       if(this._waterMarker!=null){
           if(bufferedImage.getWidth() < this.markerWidth || bufferedImage.getHeight() <this.markerHeight){
               log.error("图片小于水印图片大小!添加失败");
           }else{
               //---绘制水印
               graphics2d.drawImage(this._waterMarker, markerX, markerY, markerWidth,markerHeight, null);
           }
       }
       graphics2d.dispose();
       ImageIO.write(bufferedImage, getFileSuffix(_saveFile), _saveFile);
   }
   //---测试
   public static void main(String[] args) {
       try {
           ImgFactory ifs= new ImgFactory("D:/1.png", "D:/2.png");
           ifs.setWaterMarker("D:/3.png", 0, 0, 50, 50);
           ifs.zoom(0.5f,false);
           System.out.println("成功!!!");
       } catch(IOException e) {
           e.printStackTrace();
       }
   }
}

注:png透明设置,BufferedImage _bufferedImage=new BufferedImage(width, height, BufferedImage.TRANSLUCENT);
转自:http://blog.sina.com.cn/s/blog_bca9d7e80101cdcr.html
原创粉丝点击