Java实现使用底图和指定图片生成一张复合图片代码

/**
     * 底图叠加
     * @param basemapUrl                低图地址
     * @param targetPath                生成文件路径
     * @param base64OverlayImage        base64文件
     * 设置添加的图片位置和大小
     * @param rightTopX                 覆盖图片左上角的X坐标
     * @param rightTopY                 覆盖图片左上角的Y坐标
     * @param overWidth                 覆盖图片的宽度
     * @param overHeight                覆盖图片的高度
     */
    public static void imageOverlay(String basemapUrl, String targetPath, String base64OverlayImage, int rightTopX,int rightTopY, int overWidth, int overHeight) throws IOException {
        // 读取底图
        BufferedImage baseImage = ImageIO.read(new URL(basemapUrl));

        // 读取需要添加的图片
        byte[] overlayImageBytes = Base64.getDecoder().decode(base64OverlayImage.split(",")[1]);
        ByteArrayInputStream overlayImageInputStream = new ByteArrayInputStream(overlayImageBytes);
        BufferedImage overlayImage = ImageIO.read(overlayImageInputStream);

        // 创建一个新的图像,大小为底图的大小
        BufferedImage combinedImage = new BufferedImage(baseImage.getWidth(), baseImage.getHeight(), BufferedImage.TYPE_INT_ARGB);

        // 获取Graphics2D对象
        Graphics2D graphics = combinedImage.createGraphics();

        // 画底图
        graphics.drawImage(baseImage, 0, 0, null);

        // 画上去的图片
        graphics.drawImage(overlayImage, rightTopX, rightTopY, overWidth, overHeight, null);

        // 释放Graphics2D对象
        graphics.dispose();

        // 保存合成后的图像
        ImageIO.write(combinedImage, "PNG", new File(targetPath));
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值