再发一段java操作图像的代码
2013-10-09 09:19
267 查看
/** * http://auzll.iteye.com */ package z.tool.util; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import javax.imageio.ImageIO; import org.apache.commons.io.IOUtils; import org.apache.log4j.Logger; import z.tool.checker.HumanNeededError; import z.tool.checker.LogicError; /** * 图片工具类 * @author auzll */ public final class ImageUtil { private static final Logger LOG = Logger.getLogger(ImageUtil.class); /** * 从{@link javax.imageio.spi.ImageReaderWriterSpi}的实现类中整理出来所有支持的图片类型 */ public static enum ImageType { JPEG, PNG, GIF, WBMP } /** * 等比例缩放图片(Jpeg) */ public static void resizeAsJpeg(File srcFile, File destFile, int maxNewHeight, int maxNewWidth) { resize(srcFile, ImageType.JPEG, destFile, maxNewHeight, maxNewWidth); } /** * 等比例缩放图片(Png) */ public static void resizeAsPng(File srcFile, File destFile, int maxNewHeight, int maxNewWidth) { resize(srcFile, ImageType.PNG, destFile, maxNewHeight, maxNewWidth); } /** * 等比例缩放图片(Gif) */ public static void resizeAsGif(File srcFile, File destFile, int maxNewHeight, int maxNewWidth) { resize(srcFile, ImageType.GIF, destFile, maxNewHeight, maxNewWidth); } /** * 等比例缩放图片(Wbmp) */ public static void resizeAsWbmp(File srcFile, File destFile, int maxNewHeight, int maxNewWidth) { resize(srcFile, ImageType.WBMP, destFile, maxNewHeight, maxNewWidth); } /** * 等比例缩放图片 */ public static void resize(File srcFile, ImageType destType, File destFile, int maxNewHeight, int maxNewWidth) { if (null == srcFile || !srcFile.exists()) { if (LOG.isDebugEnabled()) { LOG.debug("method:resize,srcFile:" + (null != srcFile ? srcFile.getAbsolutePath() : null) + ",descr:file not found"); } throw new LogicError("缩放的源文件不存在"); } if (null == destType) { throw new LogicError("未指定缩放图片类型"); } if (null == destFile) { throw new LogicError("缩放的目标文件为空"); } if (maxNewHeight < 1 | maxNewWidth < 1) { throw new LogicError("缩放尺寸错误,不能小于0"); } try { Image srcImage = ImageIO.read(srcFile); // 原始图片大小 int srcImageWidth = srcImage.getWidth(null); int srcImageHeight = srcImage.getHeight(null); // 原始宽高超过设定的最大宽高 if (srcImageWidth > maxNewWidth || srcImageHeight > maxNewHeight) { // 计算缩放大小 if (srcImageWidth >= srcImageHeight) { // 原始图片的宽度大于高度,需要缩小缩放后的最大高度 maxNewHeight = (int) Math.round((srcImageHeight * maxNewWidth * 1.0 / srcImageWidth)); } else { // 否则,缩小缩放后的最大宽度 maxNewWidth = (int) Math.round((srcImageWidth * maxNewHeight * 1.0 / srcImageHeight)); } } BufferedImage distImage = new BufferedImage(maxNewWidth, maxNewHeight, BufferedImage.TYPE_INT_RGB); // 绘制新图 distImage.getGraphics().drawImage(srcImage.getScaledInstance(maxNewWidth, maxNewHeight, Image.SCALE_SMOOTH), 0, 0, null); // 输出到文件流 OutputStream os = null; try { os = new FileOutputStream(destFile); ImageIO.write(distImage, destType.name(), os); os.flush(); } finally { IOUtils.closeQuietly(os); } } catch (IOException e) { LOG.error("method:resize,srcFile:" + (null != srcFile ? srcFile.getAbsolutePath() : null) + ",destFile:" + (null != destFile ? destFile.getAbsolutePath() : null) + ",destType:" + destType + ",maxNewHeight:" + maxNewHeight + ",maxNewWidth:" + maxNewWidth + ",errorMsg:" + e.getMessage(), e); throw new HumanNeededError("图片缩放失败"); } } }
发布在 http://auzll.iteye.com
相关文章推荐
- Java中有关日期的操作,昨天晚上赴约,搞到12点多才回来,今天写这一小段代码都花了一段漫长的时间,哎。。
- Java中有关日期的操作,昨天晚上赴约,搞到12点多才回来,今天写这一小段代码都花了一段漫长的时间,哎。。
- JAVA-----myeclipse快捷操作:一段代码自动生成方法 refactor重构extract method抽取方法
- JAVA-----myeclipse快捷操作:一段代码自动生成方法 refactor重构extract method抽取方法
- Java中有关日期的操作,昨天晚上赴约,搞到12点多才回来,今天写这一小段代码都花了一段漫长的时间,哎。。
- Java 模拟并发操作压力测试代码
- java操作mysql入门代码实例(含插入、更新和查询)
- 不使用spring的情况下用java原生代码操作mongodb数据库的两种方式
- Java8 Lambda表达式和流操作如何让你的代码变慢5倍
- Java获取彩色图像中的主色彩的实例代码
- JavaScript 图像映射 【每日一段代码71】
- java中计算一段代码执行的时间
- java代码上传图片到FTP服务器——图像“ftp://****/images/**.jpg"因存在错误而无法显示
- 在jsp中默认写上的一段java代码表示basePath 的路径的具体的意思是什么?
- java 生成静态html的一段代码
- 数据库操作:java连接MYSQL实例代码演示
- 一段非常值得珍藏的Java代码,Socket模拟容器,尽管简单但却很有意义
- hbase-0.98整合hadoop-2.6,附java操作代码
- 总结android音频视频操作 (java层代码)
- Java OCR tesseract 图像智能字符识别技术 Java代码实现