/*
* Copyright Huawei Symantec Technologies Co.,Ltd. 2008-2009. All rights reserved.
*
*
*/
package com.huaweisymantec.core.utils;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* 图像处理工具
*
* @author
*/
public final class ImageUtils {
private static Logger logger = LoggerFactory.getLogger(ImageUtils.class);
private ImageUtils() {
}
/**
* 改变图片大小
*
* @param srcImg 源图片路径
* @param newImg 生成图片路径
* @param width 新生成图片宽度
* @param height 新生成图片高度
* @throws IOException 如果图片处理失败抛出IO异常
*/
public static void resize(String srcImg, String newImg, int width, int height) throws IOException {
File file = new File(srcImg);
FileOutputStream newImageOutStream = null;
try {
Image src = ImageIO.read(file);
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0, width, height, null);
newImageOutStream = new FileOutputStream(newImg);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newImageOutStream);
encoder.encode(tag);
} finally {
IOUtils.closeQuietly(newImageOutStream);
}
}
/**
* 改变文件流图片大小返回字符数组
*
* @param inputStream 源图片真实路径
* @param width 新生成图片宽度
* @param height 新生成图片高度
* @throws IOException 如果图片处理失败抛出IO异常
*/
public static byte[] resize(String srcImg, int width, int height) throws IOException {
File file = new File(srcImg);
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
byte[] data = null;
try {
Image src = ImageIO.read(file);
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0, width, height, null);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(byteOut);
encoder.encode(tag);
data = byteOut.toByteArray();
} catch (Exception ex) {
logger.error("resize error:" + ex.toString());
throw new IOException(ex);
} finally {
IOUtils.closeQuietly(byteOut);
}
return data;
}
}
* Copyright Huawei Symantec Technologies Co.,Ltd. 2008-2009. All rights reserved.
*
*
*/
package com.huaweisymantec.core.utils;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* 图像处理工具
*
* @author
*/
public final class ImageUtils {
private static Logger logger = LoggerFactory.getLogger(ImageUtils.class);
private ImageUtils() {
}
/**
* 改变图片大小
*
* @param srcImg 源图片路径
* @param newImg 生成图片路径
* @param width 新生成图片宽度
* @param height 新生成图片高度
* @throws IOException 如果图片处理失败抛出IO异常
*/
public static void resize(String srcImg, String newImg, int width, int height) throws IOException {
File file = new File(srcImg);
FileOutputStream newImageOutStream = null;
try {
Image src = ImageIO.read(file);
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0, width, height, null);
newImageOutStream = new FileOutputStream(newImg);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newImageOutStream);
encoder.encode(tag);
} finally {
IOUtils.closeQuietly(newImageOutStream);
}
}
/**
* 改变文件流图片大小返回字符数组
*
* @param inputStream 源图片真实路径
* @param width 新生成图片宽度
* @param height 新生成图片高度
* @throws IOException 如果图片处理失败抛出IO异常
*/
public static byte[] resize(String srcImg, int width, int height) throws IOException {
File file = new File(srcImg);
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
byte[] data = null;
try {
Image src = ImageIO.read(file);
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0, width, height, null);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(byteOut);
encoder.encode(tag);
data = byteOut.toByteArray();
} catch (Exception ex) {
logger.error("resize error:" + ex.toString());
throw new IOException(ex);
} finally {
IOUtils.closeQuietly(byteOut);
}
return data;
}
}