Java裁剪壓縮PNG圖片,透明背景色變黑的解決方案
更新時(shí)間:2021年11月15日 09:59:45 作者:新成同學(xué)
這篇文章主要介紹了Java裁剪壓縮PNG圖片,透明背景色變黑的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
Java裁剪壓縮PNG圖片,透明背景色變黑
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
/**
* 圖片工具類
*/
public class ImageUtil {
/**
* 裁剪PNG圖片工具類
*
* @param fromFile
* 源文件
* @param toFile
* 裁剪后的文件
* @param outputWidth
* 裁剪寬度
* @param outputHeight
* 裁剪高度
* @param proportion
* 是否是等比縮放
*/
public static void resizePng(File fromFile, File toFile, int outputWidth, int outputHeight,
boolean proportion) {
try {
BufferedImage bi2 = ImageIO.read(fromFile);
int newWidth;
int newHeight;
// 判斷是否是等比縮放
if (proportion) {
// 為等比縮放計(jì)算輸出的圖片寬度及高度
double rate1 = ((double) bi2.getWidth(null)) / (double) outputWidth + 0.1;
double rate2 = ((double) bi2.getHeight(null)) / (double) outputHeight + 0.1;
// 根據(jù)縮放比率大的進(jìn)行縮放控制
double rate = rate1 < rate2 ? rate1 : rate2;
newWidth = (int) (((double) bi2.getWidth(null)) / rate);
newHeight = (int) (((double) bi2.getHeight(null)) / rate);
} else {
newWidth = outputWidth; // 輸出的圖片寬度
newHeight = outputHeight; // 輸出的圖片高度
}
BufferedImage to = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = to.createGraphics();
to = g2d.getDeviceConfiguration().createCompatibleImage(newWidth, newHeight,
Transparency.TRANSLUCENT);
g2d.dispose();
g2d = to.createGraphics();
@SuppressWarnings("static-access")
Image from = bi2.getScaledInstance(newWidth, newHeight, bi2.SCALE_AREA_AVERAGING);
g2d.drawImage(from, 0, 0, null);
g2d.dispose();
ImageIO.write(to, "png", toFile);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 測(cè)試
*/
public static void main(String[] args) throws Exception {
File fromFile = new File("c:/wxcking/pic/D77E37CB.png");
File toFile = new File("c:/wxcking/pic/D77E37CB_thumb.png");
resizePng(fromFile, toFile, 100, 100, false);
}
}
java ImageIO.write圖片上傳變色及背景變黑
public void getImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
response.setHeader("Pragma", "no-cache");
response.setContentType("image/jpeg");
String uri = request.getRequestURI();
String imgPath = uri.replace("/ofc/rest/img", "");
File image = new File(imgPath);
BufferedImage bi = ImageIO.read(image);
BufferedImage newBi = resizeImagePng(bi.getWidth(), bi.getHeight(),bi);
Graphics2D g2d = newBi.createGraphics();
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));
ServletOutputStream out = response.getOutputStream();
g2d.drawImage(newBi,0,0,null);
ImageIO.write(newBi,"png", out);
//ImageIO.write(bi, "jpg", out);
//saveAs(bi,"jpg", out);
}
public static BufferedImage resizeImagePng(int x, int y, BufferedImage bfi) {
BufferedImage bufferedImage = new BufferedImage(x, y, BufferedImage.TYPE_INT_ARGB);
bufferedImage.getGraphics().drawImage(
bfi.getScaledInstance(x, y, Image.SCALE_SMOOTH), 0, 0, null);
return bufferedImage;
}
public static void saveAs(BufferedImage image, String formatName, ServletOutputStream outFile) throws IOException {
if (formatName.equalsIgnoreCase("jpg") || formatName.equalsIgnoreCase("jpeg")) {
BufferedImage tag = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_BGR);
Graphics g = tag.getGraphics();
// Graphics2D gg=tag.createGraphics();
// tag = gg.getDeviceConfiguration().createCompatibleImage(image.getWidth(), image.getHeight(),Transparency.TRANSLUCENT);
// gg.dispose();
// gg = tag.createGraphics();
// gg.drawImage(image, 0, 0,null);
g.drawImage(image, 0, 0,image.getWidth(), image.getHeight(),image.getGraphics().getColor(), null); // 繪制縮小后的圖
g.dispose();
image = tag;
}
ImageIO.write(image, formatName, outFile);
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot整合 xxl-job的項(xiàng)目實(shí)踐
XL-JOB是一個(gè)分布式任務(wù)調(diào)度平臺(tái),用于解決分布式系統(tǒng)中的任務(wù)調(diào)度和管理問(wèn)題,它包括調(diào)度中心、執(zhí)行器和Web管理控制臺(tái),本文就來(lái)介紹一下springboot整合 xxl-job的項(xiàng)目實(shí)踐,感興趣的可以了解一下2024-09-09
Spring 使用Validation 驗(yàn)證框架的問(wèn)題詳解
Spring Boot在內(nèi)部通過(guò)集成hibernate-validation已經(jīng)實(shí)現(xiàn)了JSR-349驗(yàn)證規(guī)范接口,在Spring Boot項(xiàng)目中只要直接使用就行了。 一般用在Controller中用于驗(yàn)證前端傳來(lái)的參數(shù)。這篇文章給大家介紹Spring Validation 驗(yàn)證框架的相關(guān)知識(shí),感興趣的朋友一起看看吧2021-07-07
Springboot實(shí)現(xiàn)Shiro整合JWT的示例代碼
這篇文章主要介紹了Springboot實(shí)現(xiàn)Shiro整合JWT的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
詳解mybatis批量插入10萬(wàn)條數(shù)據(jù)的優(yōu)化過(guò)程
這篇文章主要介紹了詳解mybatis批量插入10萬(wàn)條數(shù)據(jù)的優(yōu)化過(guò)程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
SpringBoot實(shí)現(xiàn)多文件上傳的詳細(xì)示例代碼
文件上傳中并沒(méi)有什么太多的知識(shí)點(diǎn),下面這篇文章主要給大家介紹了關(guān)于SpringBoot實(shí)現(xiàn)多文件上傳的詳細(xì)示例,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03

