Java控制圖片按比例縮放- (注意內存釋放)


package mytiny.com.common;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class ImageZipUtil {

public static void main(String[] args) {
zipWidthHeightImageFile(new File("D:\\3.png"), new File("D:\\3.png"), 375, 180, 0.8f);

// System.out.println("ok");
}

/**
* 根據設置的寬高等比例壓縮圖片文件<br>
* 先保存原文件,再壓縮、上傳
*
* @param oldFile
* 要進行壓縮的文件
* @param newFile
* 新文件
* @param width
* 寬度 //設置寬度時(高度傳入0,等比例縮放)
* @param height
* 高度 //設置高度時(寬度傳入0,等比例縮放)
* @param quality
* 質量
* @return 返回壓縮后的文件的全路徑
*/
public static String zipImageFile(File oldFile, File newFile, int width, int height, float quality) {
if (oldFile == null) {
return null;
}
try {
/** 對服務器上的臨時文件進行處理 */
Image srcFile = ImageIO.read(oldFile);
int w = srcFile.getWidth(null);
int h = srcFile.getHeight(null);
double bili;
if (width > 0) {
bili = width / (double) w;
height = (int) (h * bili);
} else {
if (height > 0) {
bili = height / (double) h;
width = (int) (w * bili);
}
}

String srcImgPath = newFile.getAbsoluteFile().toString();
// System.out.println(srcImgPath);
String subfix = "jpg";
subfix = srcImgPath.substring(srcImgPath.lastIndexOf(".") + 1, srcImgPath.length());

BufferedImage buffImg = null;
if (subfix.equals("png")) {
buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
} else {
buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
}

Graphics2D graphics = buffImg.createGraphics();
graphics.setBackground(new Color(255, 255, 255));
graphics.setColor(new Color(255, 255, 255));
graphics.fillRect(0, 0, width, height);
graphics.drawImage(srcFile.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);

ImageIO.write(buffImg, subfix, new File(srcImgPath));

} catch (Exception e) {
e.printStackTrace();
}
return newFile.getAbsolutePath();
}

/**
* 按設置的寬度高度壓縮圖片文件<br>
* 先保存原文件,再壓縮、上傳
*
* @param oldFile
* 要進行壓縮的文件全路徑
* @param newFile
* 新文件
* @param width
* 寬度
* @param height
* 高度
* @param quality
* 質量
* @return 返回壓縮后的文件的全路徑
*/
public static boolean zipWidthHeightImageFile(File oldFile, File newFile, int width, int height, float quality) {
if (oldFile == null) {
return false;
}
Image srcFile = null;
BufferedImage buffImg = null;
try {
/** 對服務器上的臨時文件進行處理 */
srcFile = ImageIO.read(oldFile);

String srcImgPath = newFile.getAbsoluteFile().toString();
// System.out.println(srcImgPath);
String subfix = "jpg";
subfix = srcImgPath.substring(srcImgPath.lastIndexOf(".") + 1, srcImgPath.length());

if (subfix.equals("png")) {
buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
} else {
buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
}

Graphics2D graphics = buffImg.createGraphics();
graphics.setBackground(new Color(255, 255, 255));
graphics.setColor(new Color(255, 255, 255));
graphics.fillRect(0, 0, width, height);
graphics.drawImage(srcFile.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);

ImageIO.write(buffImg, subfix, new File(srcImgPath));
return true;

} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (srcFile != null) {
srcFile.flush();
}
if (buffImg != null) {
buffImg.flush();
}

}

}

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM