码迷,mamicode.com
首页 > 其他好文 > 详细

对图片的统一格式转换处理

时间:2015-05-07 01:11:29      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:inputstream   graphics   

package com.eshore.rbss.commons;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import org.apache.log4j.Logger;

/**
 * 1.对图片时行格式转换

 * 2.对图片进行适度的大小裁剪
 *
 */
public class ImageUtil {

 /**
  *
  * @param infile 输入文件
  * @param outfile 输出文件
  * @param srcFormat 源格式
  * @param destFormat 输出格式
  * @return
  * @throws Exception
  */
 public static boolean convertFormat(InputStream infile,OutputStream outfile, String srcFormat, String destFormat, int width ,int height) throws Exception {
  boolean flag = false;
  BufferedImage src = ImageIO.read(infile);
  if(height > 0  && width > 0) {// compress the origin image if width and height are non-zero
   height = src.getHeight() > height ? height: src.getHeight();
   width = src.getWidth() > width ? width : src.getWidth();
   Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT);//这个是用来进行图片大小调整的
 
   BufferedImage tag = new BufferedImage(width, height,
     BufferedImage.TYPE_INT_RGB);
 
   Graphics g = tag.getGraphics();
   //可在下面对图片进行绘制和更改
   g.drawImage(image, 0, 0, null); // 绘制缩小后的图
 
   g.dispose();
   tag.flush();
   flag = ImageIO.write(tag, destFormat, outfile);// 输出到经过缩放的文件流
  } else {
   flag = ImageIO.write(src, destFormat, outfile);//输出原分辨率的图片
  }
  Logger.getLogger(ImageUtil.class).info("图片转换成功: 从[" + srcFormat + "]到[" + destFormat + "]");
  return flag;
 }
}

本文出自 “杨海龙的博客” 博客,请务必保留此出处http://7218743.blog.51cto.com/7208743/1643560

对图片的统一格式转换处理

标签:inputstream   graphics   

原文地址:http://7218743.blog.51cto.com/7208743/1643560

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!