thumbnails 变黑_压缩图片 解决图片背景透明压缩后变黑色问题

这篇博客介绍了如何使用Java代码解决PNG图片在缩放和压缩过程中背景透明部分变为黑色的问题。通过调整图像处理方法,确保在等比缩放时保持透明度,并使用Transparency.TRANSLUCENT参数来保持透明背景。
摘要由CSDN通过智能技术生成

public class Picture {

// TODO Auto-generated constructor stub

public static void resizePNG(String fromFile, String toFile, int outputWidth, int outputHeight,boolean proportion) {

try {

File f2 = new File(fromFile);

BufferedImage bi2 = ImageIO.read(f2);

int newWidth;

int newHeight;

// 判断是否是等比缩放

if (proportion == true) {

// 为等比缩放计算输出的图片宽度及高度

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; // 输出

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值