java将图片与二维码合成

/**
     * 图片与二维码合成
     * @param background 背景图片信息流
     * @param bufferedImage  二维码信息流
     */
    public static BufferedImage combineCodeAndPicToFile(BufferedImage background,BufferedImage bufferedImage) {
        System.out.println("开始合成:");
        try {
            //背景图路径
           // BufferedImage big = ImageIO.read(new File(inputFile));

            //url扫二维码显示的内容
            Graphics2D g = background.createGraphics();  //合成的图片

            //二维码或小图在大图的左上角坐标
            int x = (background.getWidth() - bufferedImage.getWidth())-30; //加是向右,减是向左
            int y = (background.getHeight() - bufferedImage.getHeight())-100; //加是向下,减是向上
            //将二维码花在背景图上
            g.drawImage(bufferedImage, x, y, bufferedImage.getWidth(), bufferedImage.getHeight(), null);
            //结束绘画
            g.dispose();
            //存储图片 为了保证大图背景不变色,formatName必须为"png" 存储路径
            //ImageIO.write(background, "png", new FileOutputStream(outputFile));
            System.out.println("结束合成:");
            return background;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以使用ZXing库来生成二维码,以下是一个使用Java代码生成二维码的例子: ```java import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.imageio.ImageIO; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; public class QRCodeGenerator { public static void main(String[] args) { String text = "https://www.csdn.net/"; // 二维码内容 int width = 300; // 二维码宽度 int height = 300; // 二维码高度 String format = "png"; // 二维码格式 // 定义二维码参数 Map<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 编码方式 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); // 纠错等级 hints.put(EncodeHintType.MARGIN, 2); // 边距 try { // 生成二维码 BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints); // 保存二维码到文件 File outputFile = new File("qrcode.png"); ImageIO.write(MatrixToImageWriter.toBufferedImage(bitMatrix), format, outputFile); } catch (WriterException | IOException e) { e.printStackTrace(); } } } ``` 在上面的例子中,我们使用了ZXing库来生成二维码,并将结果保存到了一个png格式的文件中。你可以根据需要修改代码来适应你的具体需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

想养一只!

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

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

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

打赏作者

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

抵扣说明:

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

余额充值