Java 背景图片拼接 根据内容生成的二维码

本文介绍了一种使用Java实现图片拼接和二维码生成的方法。通过整合底层图片与二维码,实现了带有水印的二维码图片生成。代码示例展示了如何利用Google ZXing库创建二维码,并将其叠加到指定图片上。
部署运行你感兴趣的模型镜像

Java 图片拼接根据内容生成的二维码   直接上代码:此类可作为工具类 自己琢磨自学  不喜请喷。

此为Java web项目  在pom.xml文件添加:(谷歌官方API  可以百度搜索 下载Jar 包)

  <!-- 图片组合二维码 -->

     <dependency>

    <groupId>com.google.zxing</groupId>

    <artifactId>core</artifactId>

    <version>3.1.0</version>

</dependency>




public class ImageAndQRcode {


    private staticfinal int BLACK = 0xFF000000;

    private staticfinal int WHITE = 0xFFFFFFFF;


    ImageAndQRcode() {

    }


    private static BufferedImage toBufferedImage(BitMatrixmatrix) {

        int width =matrix.getWidth();

        int height =matrix.getHeight();

        BufferedImage image = new BufferedImage(width, height,

                BufferedImage.TYPE_INT_RGB);

        for (intx = 0; x < width; x++) {

            for (inty = 0; y < height; y++) {

                image.setRGB(x,y, matrix.get(x,y) ? BLACK :WHITE);

            }

        }

        return image;

    }


    private staticvoid writeToFile(BitMatrix matrix, String format, File file)

            throws IOException {

        BufferedImage image = toBufferedImage(matrix);

        if (!ImageIO.write(image,format, file)) {

            thrownew IOException("Could not write an image of format "

                    + format + " to " + file);

        }

    }

    /**

     * @Title: 构造图片

     * @Description: 生成水印并返回java.awt.image.BufferedImage

     * @param file源文件(图片)

     * @param waterFile水印文件(图片)

     * @param x距离右下角的X偏移量

     * @param y 距离右下角的Y偏移量

     * @param alpha 透明度, 选择值从0.0~1.0: 完全透明~完全不透明

     * @return BufferedImage

     * @throws IOException

     */

    private static BufferedImage watermark(Filefile, File waterFile,int x, int y, floatalpha) throws IOException {

        // 获取底图

        BufferedImage buffImg = ImageIO.read(file);

        // 获取层图

        BufferedImage waterImg = ImageIO.read(waterFile);

        // 创建Graphics2D对象,用在底图对象上绘图

        Graphics2D g2d = buffImg.createGraphics();

        int waterImgWidth =waterImg.getWidth();// 获取层图的宽度

        int waterImgHeight =waterImg.getHeight();// 获取层图的高度

        // 在图形和图像中实现混合和透明效果

        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,alpha));

        // 绘制

        g2d.drawImage(waterImg,x, y, waterImgWidth, waterImgHeight,null);

        g2d.dispose();// 释放图形上下文使用的系统资源

        return buffImg;

    }


    /**

     * 输出水印图片

     * 

     * @param buffImg 图像加水印之后的BufferedImage对象

     * @param savePath 图像加水印之后的保存路径

     */

    private void generateWaterFile(BufferedImagebuffImg, String savePath) {

        int temp =savePath.lastIndexOf(".") + 1;

        try {

            ImageIO.write(buffImg, savePath.substring(temp), new File(savePath));

        } catch (IOException e1) {

            e1.printStackTrace();

        }

    }


    /**

     * 

     * @param text 二维码内容

     * @param width 二维码图片宽度

     * @param height 二维码图片高度

     * @param format 二维码的图片格式

     * @param sourceFilePath 底层图片路径

     * @param waterFilePath 二维码路径

     * @param saveFilePath 合成图片路径

     * @param maginx  二维码距离底图x轴距离

     * @param maginy  二维码距离底图y轴距离

     * @throws Exception

     */

    public void addImageQRcode(Stringtext,int width,int height,Stringformat

    String sourceFilePath,String waterFilePath, StringsaveFilePath,intmaginx,intmaginy)throws Exception{

    

    Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();

    hints.put(EncodeHintType.CHARACTER_SET,"utf-8"); // 内容所使用字符集编码

    

    BitMatrix bitMatrix =new MultiFormatWriter().encode(text,

    BarcodeFormat.QR_CODE,width, height,hints);

    // 生成二维码

    File outputFile = new File("/Users/chenfenqian/Desktop" + File.separator +"new.jpg");

    ImageAndQRcode.writeToFile(bitMatrix,format, outputFile);

    

    ImageAndQRcode newImageUtils = new ImageAndQRcode();

    // 构建叠加层

    BufferedImage buffImg = ImageAndQRcode.watermark(new File(sourceFilePath),new File(waterFilePath),maginx, maginy, 1.0f);

    // 输出水印图片

    newImageUtils.generateWaterFile(buffImg,saveFilePath);

    }

测试代码如下 图片随机找的  说明问题就可以了;

 public staticvoid main(String[] args) throws IOException {


    

    ImageAndQRcode add = new ImageAndQRcode();

    try {

add.addImageQRcode("http://www.baidu.com", 300, 300,"jpg", "/Users/chenfenqian/Documents/图片素材/welcome0.png"

"/Users/chenfenqian/Desktop/new.jpg","/Users/chenfenqian/Desktop/new1.png",780,1620);

} catch (Exceptione) {

// TODO Auto-generated catch block

e.printStackTrace();

}

    }



您可能感兴趣的与本文相关的镜像

Qwen-Image

Qwen-Image

图片生成
Qwen

Qwen-Image是阿里云通义千问团队于2025年8月发布的亿参数图像生成基础模型,其最大亮点是强大的复杂文本渲染和精确图像编辑能力,能够生成包含多行、段落级中英文文本的高保真图像

若要在Java根据链接生成包含输入参数名称的二维码,能够借助ZXing库来实现。实现思路为:先把链接和输入参数名称拼接成一个字符串,再用这个字符串生成二维码。 以下是示例代码: ```java import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class GenerateQRCodeWithParams { public static void main(String[] args) { String url = "https://www.example.com"; String paramName = "param1=value1&param2=value2"; String content = url + "?" + paramName; int width = 200; int height = 200; String format = "PNG"; // 创建QRCodeWriter对象 QRCodeWriter qrCodeWriter = new QRCodeWriter(); // 设置编码参数 Map<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); try { // 将链接和参数拼接后的内容转换为二维码矩阵 BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints); // 输出为PNG格式的图像文件 File outputFile = new File("qrcode_with_params.png"); MatrixToImageWriter.writeToPath(bitMatrix, format, outputFile.toPath()); } catch (WriterException | IOException e) { e.printStackTrace(); } } } ``` 在上述代码中,`url` 为基础链接,`paramName` 是输入参数名称及其对应的值,将它们拼接成完整的 `content` 后,使用ZXing库生成二维码并保存为 `qrcode_with_params.png` 文件。
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值