Java将一张图片放在另一张图片上(位置可选)

本文介绍了一种使用Java实现的图片合成方法,通过将两张图片进行叠加,一张小图片可以被放置在另一张大图片的指定位置上。此方法涉及图片读取、创建图形上下文、定位小图片并进行绘制,最终保存合成后的图片。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
*param qrcodePath : 最后图片保存路劲
*/
public static void overlapImage(String qrcodePath) {
        try {
            BufferedImage big = new BufferedImage(1080, 1920, BufferedImage.TYPE_INT_RGB);  
            Graphics2D gd = big.createGraphics(); 
            //gd.setBackground(Color.white);
            gd.setColor(Color.white);
            gd.dispose();
            //BufferedImage big = ImageIO.read(new File(screenPath));
            //BufferedImage small = ImageIO.read(new File(qrcodePath));
            BufferedImage small = new BufferedImage(540, 540,BufferedImage.TYPE_INT_ARGB); 
            Graphics2D g = big.createGraphics();
            int x = (big.getWidth() - small.getWidth()) / 2;
            int y = 200 ;
            g.drawImage(small, x, y, small.getWidth(), small.getHeight(), null);
            g.dispose();
            ImageIO.write(big, "jpg", new File(qrcodePath));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

java实现简单的俩张图片,一张贴在另一张上面的方法。

图片的拼接。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值