use of java.awt.image in project JFramework by gugumall.
the class JUtilImage method resize.
/**
* 将srcFile调整为newWidth*newHeight大小,并保存为destFile
*
* @param srcFile
* @param destFile
* @param newWidth
* @param newHeight
* @param imageFormat
* @return
* @throws Exception
*/
public void resize(File srcFile, File destFile, int newWidth, int newHeight, String imageFormat) throws Exception {
if (!chkImageFormat(imageFormat)) {
throw new Exception("图片类型不合法");
}
if (imageFormat.equalsIgnoreCase(FORMAT_GIF) || srcFile.getName().toLowerCase().endsWith(".gif")) {
resizeGifImage(srcFile, destFile, newWidth, newHeight, true);
return;
}
Image src = Toolkit.getDefaultToolkit().getImage(srcFile.getAbsolutePath());
src = new ImageIcon(src).getImage();
BufferedImage tag = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = tag.createGraphics();
// 设置透明
if (imageFormat.equals(JUtilImage.FORMAT_PNG)) {
tag = g2d.getDeviceConfiguration().createCompatibleImage(newWidth, newHeight, Transparency.TRANSLUCENT);
}
g2d.dispose();
g2d = tag.createGraphics();
// 设置透明 end
// 绘制缩小后的图
g2d.drawImage(src, 0, 0, newWidth, newHeight, this);
// 如果父目录不存在,则创建目录
if (!destFile.getParentFile().exists()) {
destFile.getParentFile().mkdirs();
}
// 输出到文件流
FileOutputStream os = new FileOutputStream(destFile);
ImageWriter writer = (ImageWriter) ImageIO.getImageWritersByFormatName(imageFormat).next();
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
writer.setOutput(ios);
ImageWriteParam param = new JPEGImageWriteParam(Locale.getDefault());
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(quality);
writer.write(null, new IIOImage(tag, null, null), param);
writer.dispose();
ios.flush();
ios.close();
os.close();
}
use of java.awt.image in project JFramework by gugumall.
the class JUtilImage method zoomToWidth.
/**
* 将srcFile按比例缩放,使其宽度等于width,并保存为destFile
*
* @param srcFile
* @param destFile
* @param width
* @param imageFormat
* @return
* @throws Exception
*/
public void zoomToWidth(File srcFile, File destFile, int width, String imageFormat) throws Exception {
if (!chkImageFormat(imageFormat)) {
throw new Exception("图片类型不合法");
}
Image src = Toolkit.getDefaultToolkit().getImage(srcFile.getAbsolutePath());
src = new ImageIcon(src).getImage();
double oldWidth = (double) src.getWidth(this);
double scale = width / oldWidth;
zoom(srcFile, destFile, scale, imageFormat);
}
use of java.awt.image in project JFramework by gugumall.
the class JUtilImage method zoomToSize.
/**
* 将srcFile按比例缩放,使其较长一边的长度等于longerSideSize,并保存为destFile
*
* @param srcFile
* @param destFile
* @param longerSideSize
* @param imageFormat
* @return
* @throws Exception
*/
public void zoomToSize(File srcFile, File destFile, int longerSideSize, String imageFormat) throws Exception {
if (!chkImageFormat(imageFormat)) {
throw new Exception("图片类型不合法");
}
Image src = Toolkit.getDefaultToolkit().getImage(srcFile.getAbsolutePath());
src = new ImageIcon(src).getImage();
double oldWidth = (double) src.getWidth(this);
double oldHeight = (double) src.getHeight(this);
double scale = 1;
if (oldWidth > oldHeight) {
scale = longerSideSize / oldWidth;
} else {
scale = longerSideSize / oldHeight;
}
zoom(srcFile, destFile, scale, imageFormat);
}
use of java.awt.image in project JFramework by gugumall.
the class JUtilImage method zoomToHeight.
/**
* 将srcFile按比例缩放,使其高度等于height,并保存为destFile
*
* @param srcFile
* @param destFile
* @param height
* @param imageFormat
* @return
* @throws Exception
*/
public void zoomToHeight(File srcFile, File destFile, int height, String imageFormat) throws Exception {
if (!chkImageFormat(imageFormat)) {
throw new Exception("图片类型不合法");
}
Image src = Toolkit.getDefaultToolkit().getImage(srcFile.getAbsolutePath());
src = new ImageIcon(src).getImage();
double oldHeight = (double) src.getHeight(this);
double scale = height / oldHeight;
zoom(srcFile, destFile, scale, imageFormat);
}
use of java.awt.image in project JFramework by gugumall.
the class JUtilImage method zoomToSizeByShorterSideIfLarger.
/**
* 将srcFile按比例缩放,使其较长一边的长度等于longerSideSize,并保存为destFile
*
* @param srcFile
* @param destFile
* @param longerSideSize
* @param imageFormat
* @return
* @throws Exception
*/
public void zoomToSizeByShorterSideIfLarger(File srcFile, File destFile, int shorterSideSize, String imageFormat) throws Exception {
if (!chkImageFormat(imageFormat)) {
throw new Exception("图片类型不合法");
}
Image src = Toolkit.getDefaultToolkit().getImage(srcFile.getAbsolutePath());
src = new ImageIcon(src).getImage();
double oldWidth = (double) src.getWidth(this);
double oldHeight = (double) src.getHeight(this);
if (oldWidth <= shorterSideSize && oldHeight <= shorterSideSize) {
JDFSFile.saveStream(new FileInputStream(srcFile), destFile.getAbsolutePath());
return;
}
double scale = 1;
if (oldWidth < oldHeight) {
scale = shorterSideSize / oldWidth;
} else {
scale = shorterSideSize / oldHeight;
}
zoom(srcFile, destFile, scale, imageFormat);
}
Aggregations