为不同大小的图片添加纯色背景

先看效果

 调用代码:

 File file = new File("需要加背景的图片的全路径");  //比如"D:\\Download\\test.png"
InputStream in = new FileInputStream(file);
BufferedImage bufferedImage = ImageUtil.reSquareAddBackground(in, 背景图宽, 背景图高, 背景颜色);
//比如 ImageUtil.reSquareAddBackground(in, 1024, 1024, Color.PINK(粉色)/Color.WHITE(白色))
File newFile = new File("生成的新图片的全路径");  //比如"D:\\Download\\test-pink.png"
ImageIO.write(bufferedImage, "生成图片格式/后缀", newFile);  //比如 "png"

处理前:

处理后:

白色背景不太明显,换个粉色的~

完整代码

    /**
     * 将不同大小的图片按比例缩放到一张纯色背景图上
     *
     * @param downloadImage 需要添加背景图片
     * @param backWidth     背景宽
     * @param backHeight    背景高
     * @param color         背景颜色
     * @return
     * @throws IOException
     */
    public static BufferedImage reSquareAddBackground(InputStream downloadImage, Integer backWidth, Integer backHeight, Color color) throws IOException {
        // 先创建一个纯色背景图
        BufferedImage biNew = new BufferedImage(backWidth, backHeight, BufferedImage.TYPE_INT_RGB);
        Graphics g = biNew.getGraphics();
        g.setColor(color);  //g.setColor(Color.white);
        g.fillRect(0, 0, backWidth, backHeight);   // 矩形填充
        g.dispose();

        BufferedImage bi = ImageIO.read(downloadImage);
        int width = bi.getWidth();
        int height = bi.getHeight();
        int stepH = 0; //上下间隙
        int stepW = 0; //左右间隙
        double ratioW = width * 1.0 / backWidth;
        double ratioH = height * 1.0 / backHeight;
        if (ratioH > ratioW) {
            //按高缩放
            height = backHeight;
            width = (int) (width / ratioH);
            stepW = (backWidth - width) / 2;
            //获取缩放后的Image对象
            bi = getZoomScaleBufferedImage(bi, width, height);
        } else {
            //按宽缩放
            //获取缩放后的长和宽
            width = backWidth;
            height = (int) (height / ratioW);
            stepH = (backHeight - height) / 2;
            //获取缩放后的Image对象
            bi = getZoomScaleBufferedImage(bi, width, height);
        }

        int minX = bi.getMinX();
        int minY = bi.getMinY();
        //遍历长和宽上的每个像素
        for (int i = minX; i < width; i++) {
            for (int j = minY; j < height; j++) {
                //得到指定像素(i,j)上的RGB值,
                int pixel = bi.getRGB(i, j);
                //判断改像素是否有值(也可以通过位运算分别求得RGB值再进行判断)
                if (pixel == -16777216) {
                    continue;
                }
                biNew.setRGB(i + stepW, j + stepH, pixel);
            }
        }
        System.out.println("完成^ ^");
        return biNew;
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

AQin1012

求小鱼干呢~~

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

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

打赏作者

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

抵扣说明:

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

余额充值