Java程序Png图片保持透明度处理

该代码实现了一个Java程序,它读取指定路径的图片,根据给定的面部宽度参数调整图片尺寸,然后保存处理后的图片。主要使用了BufferedImage类和Graphics2D接口进行图像操作,包括按比例缩放和保存为PNG格式。
摘要由CSDN通过智能技术生成

核心代码

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class A {
    public static BufferedImage resize(int faceWidth, BufferedImage srcImg) throws IOException {
        int imgWidth = 0;
        if (faceWidth > 70 && faceWidth < 120) {
            imgWidth = faceWidth * 7 / 8;
        } else if (faceWidth > 60 && faceWidth < 70) {
            imgWidth = faceWidth * 11 / 10;
        } else if (faceWidth < 60) {
            imgWidth = faceWidth * 6 / 5;
        } else {
            imgWidth = faceWidth * 4 / 5;
        }
        int imgHeight = imgWidth * srcImg.getHeight() / srcImg.getWidth();
        BufferedImage resizedImg = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D resizedG = resizedImg.createGraphics();
        resizedImg = resizedG.getDeviceConfiguration().createCompatibleImage(imgWidth, imgHeight, Transparency.TRANSLUCENT);
        resizedG.dispose();
        resizedG = resizedImg.createGraphics();
        Image from = srcImg.getScaledInstance(imgWidth, imgHeight, srcImg.SCALE_AREA_AVERAGING);
        resizedG.drawImage(from, 0, 0, null);
        resizedG.dispose();
        return resizedImg;
    }

    public static void main(String[] args) {
        try {
            //读入图片
            BufferedImage image = (BufferedImage) ImageIO.read(new File("C:\\Users\\Administrator\\Desktop\\zjb.png"));
            //处理图片
            BufferedImage resize = resize(1769, image);
            //输出图片
            File outputfile = new File("C:\\Users\\Administrator\\Desktop\\zjb_deal.png");
            ImageIO.write(resize, "png", outputfile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乘风御浪云帆之上

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值