Java裁剪压缩PNG图片,透明背景色变黑的解决方案
作者:新成同学 时间:2023-11-25 13:21:27
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) {
// 为等比缩放计算输出的图片宽度及高度
double rate1 = ((double) bi2.getWidth(null)) / (double) outputWidth + 0.1;
double rate2 = ((double) bi2.getHeight(null)) / (double) outputHeight + 0.1;
// 根据缩放比率大的进行缩放控制
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();
}
}
/**
* 测试
*/
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);
}
来源:https://blog.csdn.net/ya2dan/article/details/49802279
标签:Java,PNG图片,背景色,变黑
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android+Flutter实现文字跑马灯特效
2022-11-24 05:57:58
![](https://img.aspxhome.com/file/2023/3/120053_0s.png)
Java多线程编程中ThreadLocal类的用法及深入
2022-03-17 03:21:29
Android动画之小球拟合动画实例
2023-06-11 00:47:46
![](https://img.aspxhome.com/file/2023/0/138090_0s.gif)
老生常谈Java中List与ArrayList的区别
2023-03-26 19:52:33
![](https://img.aspxhome.com/file/2023/6/60916_0s.png)
介绍Jersey-Jersey入门基础
2023-11-18 06:34:26
Java 使用Docker时经常遇到的五个问题
2023-12-10 07:20:21
![](https://img.aspxhome.com/file/2023/7/79317_0s.jpg)
JAVA多线程并发下的单例模式应用
2022-09-15 01:27:31
JavaSwing BorderLayout 边界布局的实现代码
2023-04-23 08:03:40
![](https://img.aspxhome.com/file/2023/2/75002_0s.png)
Java Spring5学习之JdbcTemplate详解
2023-11-25 20:17:23
![](https://img.aspxhome.com/file/2023/2/60412_0s.png)
Java 开启多线程常见的4种方法
2023-11-23 02:30:10
![](https://img.aspxhome.com/file/2023/0/58580_0s.png)
java实现科学计算器的全过程与代码
2022-04-09 07:49:52
![](https://img.aspxhome.com/file/2023/0/132170_0s.png)
Java实现TFIDF算法代码分享
2023-12-23 20:54:45
![](https://img.aspxhome.com/file/2023/7/125497_0s.png)
Jenkins自动化打包为war包
2021-08-08 20:25:49
![](https://img.aspxhome.com/file/2023/2/103992_0s.jpg)
Mybatis模糊查询之三种定义参数方法和聚合查询、主键回填实现方法
2023-06-27 03:23:44
![](https://img.aspxhome.com/file/2023/6/104706_0s.jpg)
Android应用开发中使用Fragment的入门学习教程
2023-05-17 04:30:59
![](https://img.aspxhome.com/file/2023/4/139114_0s.png)
Java注解Annotation原理及自定义注解代码实例
2023-04-30 10:18:09
win7配置adb环境变量的方法步骤
2022-11-10 16:45:12
![](https://img.aspxhome.com/file/2023/1/139301_0s.png)
java并发编程工具类JUC之ArrayBlockingQueue
2023-07-04 21:02:20
Java jvm中Code Cache案例详解
2022-02-04 17:00:53
Android编程实现图片平铺的方法分析
2022-05-27 06:37:40