欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java基于Google zxing生成帶logo的二維碼圖片

 更新時(shí)間:2023年10月15日 11:16:44   作者:smileNicky  
zxing是一個(gè)開放源碼的,用java實(shí)現(xiàn)的多種格式的1D/2D條碼圖像處理庫,本文主要介紹了Java基于Google zxing生成帶logo的二維碼圖片,具有一定的參考價(jià)值,感興趣的可以了解一下

環(huán)境準(zhǔn)備

  • 開發(fā)環(huán)境

    • JDK 1.8
    • SpringBoot2.2.1
    • Maven 3.2+
  • 開發(fā)工具

    • IntelliJ IDEA
    • smartGit
    • Navicat15

添加maven配置

<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.4.0</version>
</dependency>
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.4.0</version>
</dependency>

創(chuàng)建比特矩陣

先創(chuàng)建比特矩陣,設(shè)置默認(rèn)的寬度、高度、后綴名等等

 private static final String DEFAULT_CHAR_SET = "UTF-8";

private static final String DEFAULT_FORMAT_NAME = "JPG";


// 二維碼寬度
private static final int DEFAULT_QR_CODE_WIDTH = 300;
// 二維碼高度
private static final int DEFAULT_QR_CODE_HEIGHT = 300;

/**
 * 創(chuàng)建BitMatrix比特矩陣
 * @Date 2023/09/24 22:29
 * @Param contents 二維碼里的內(nèi)容
 * @Param width 二維碼寬度
 * @param height 二維碼高度
 * @return com.google.zxing.common.BitMatrix
 */
public static  BitMatrix createBitMatrix(String contents , int width , int height) throws WriterException, IOException {
    if (ObjectUtil.isNull(width)) {
        width = DEFAULT_QR_CODE_WIDTH;
    }
    if (ObjectUtil.isNull(height)) {
        height = DEFAULT_QR_CODE_HEIGHT;
    }

    Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 糾錯(cuò)等級L,M,Q,H
    hints.put(EncodeHintType.CHARACTER_SET, DEFAULT_CHAR_SET);// 編碼utf-8
    hints.put(EncodeHintType.MARGIN, 1);  // 邊距

    // 創(chuàng)建比特矩陣
    BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,
            BarcodeFormat.QR_CODE, width, height, hints);
    return bitMatrix;

}

轉(zhuǎn)換為BufferedImage

創(chuàng)建好比特矩陣后,轉(zhuǎn)換為BufferedImage

 /**
 * 轉(zhuǎn)換為BufferedImage
 * @Date 2023/09/24 22:32
 * @Param [bitMatrix]
 * @return java.awt.image.BufferedImage
 */
public static BufferedImage toBufferedImage(BitMatrix bitMatrix) throws IOException, WriterException {
    MatrixToImageConfig matrixToImageConfig = new MatrixToImageConfig(0xFF000001, 0xFFFFFFFF);
    BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix, matrixToImageConfig);
    return bufferedImage;
}

加上二維碼logo

給創(chuàng)建的二維碼BufferedImage加上logo

 /**
 * 給二維碼添加logo
  * @Date 2023/09/24 22:33
  * @Param [bufferedImage, logoFile]
  * @return java.awt.image.BufferedImage
  */
 public static BufferedImage addQrCodeLogo(BufferedImage bufferedImage, File logoFile) throws IOException {
     Graphics2D graphics = bufferedImage.createGraphics();
     int matrixWidth = bufferedImage.getWidth();
     int matrixHeigh = bufferedImage.getHeight();

     // 讀取logo圖片文件
     BufferedImage logo = ImageIO.read(logoFile);
     int logoWidth = logo.getWidth();
     int logoHeight = logo.getHeight();

     //  計(jì)算logo放置位置
     int x = bufferedImage.getWidth()  / 5*2;
     int y = bufferedImage.getHeight() / 5*2;
     int width = matrixWidth / 5;
     int height = matrixHeigh / 5;

     // 開始繪制圖片
     graphics.drawImage(logo, x, y, width, height, null);
     graphics.drawRoundRect(x, y, logoWidth, logoHeight, 15, 15);
     graphics.setStroke(new BasicStroke(5.0F, 1, 1));
     graphics.setColor(Color.white);
     graphics.drawRect(x, y, logoWidth, logoHeight);

     graphics.dispose();
     bufferedImage.flush();
     return bufferedImage;
 }

測試

public static void main(String[] args) throws Exception {
     BufferedImage bufferedImage = toBufferedImage(createBitMatrix("https://blog.csdn.net", 300, 300));
     ImageIO.write(bufferedImage, "png", new File("D:/qrcode.jpg"));

     System.out.println(decodeQrCode(bufferedImage));

     BufferedImage logoQrCode = addQrCodeLogo(bufferedImage, new File("D://logo.png"));
     ImageIO.write(logoQrCode, "png", new File("D:/logoQrcode.jpg"));
 }

創(chuàng)建不帶logo的二維碼圖片

在這里插入圖片描述

創(chuàng)建帶logo的二維碼圖片

在這里插入圖片描述

附錄

package com.example.common.util.qrcode;


import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;

public class QrCodeGenerator {


    private static final String DEFAULT_CHAR_SET = "UTF-8";

    private static final String DEFAULT_FORMAT_NAME = "JPG";


    // 二維碼寬度
    private static final int DEFAULT_QR_CODE_WIDTH = 300;
    // 二維碼高度
    private static final int DEFAULT_QR_CODE_HEIGHT = 300;

    /**
     * 創(chuàng)建BitMatrix比特矩陣
     * @Date 2023/09/24 22:29
     * @Param contents 二維碼里的內(nèi)容
     * @Param width 二維碼寬度
     * @param height 二維碼高度
     * @return com.google.zxing.common.BitMatrix
     */
    public static  BitMatrix createBitMatrix(String contents , int width , int height) throws WriterException, IOException {
        if (ObjectUtil.isNull(width)) {
            width = DEFAULT_QR_CODE_WIDTH;
        }
        if (ObjectUtil.isNull(height)) {
            height = DEFAULT_QR_CODE_HEIGHT;
        }

        Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 糾錯(cuò)等級L,M,Q,H
        hints.put(EncodeHintType.CHARACTER_SET, DEFAULT_CHAR_SET);// 編碼utf-8
        hints.put(EncodeHintType.MARGIN, 1);  // 邊距

        // 創(chuàng)建比特矩陣
        BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,
                BarcodeFormat.QR_CODE, width, height, hints);
        return bitMatrix;

    }

    /**
     * 創(chuàng)建二維碼,返回字節(jié)數(shù)組
     * @Date 2023/09/24 22:30
     * @Param contents 二維碼里的內(nèi)容
     * @Param imageFormat 圖片后綴名
     * @Param width 二維碼寬度
     * @param height 二維碼高度
     * @return byte[]
     */
    public static byte[] createQrCode(String contents , String imageFormat , int width , int height) throws WriterException, IOException {
        if (StrUtil.isBlank(imageFormat)){
            imageFormat = DEFAULT_FORMAT_NAME;
        }
        BitMatrix bitMatrix = createBitMatrix(contents , width, height);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(bitMatrix, imageFormat, os);
        return os.toByteArray();
    }

    /**
     * 創(chuàng)建二維碼,返回base64字符串
     * @Date 2023/09/24 22:30
     * @Param contents 二維碼里的內(nèi)容
     * @Param imageFormat 圖片后綴名
     * @Param width 二維碼寬度
     * @param height 二維碼高度
     * @return byte[]
     */
    public static String createQrCodeBase64(String contents , String imageFormat , int width , int height) throws WriterException, IOException {
        byte[] bytes =createQrCode(contents , imageFormat , width, height);
        return Base64.encode(bytes);
    }

    /**
     * 解碼二維碼
     * @Date 2023/09/24 22:32
     * @Param [image]
     * @return java.lang.String
     */
    public static String decodeQrCode(BufferedImage image) throws Exception {
        if (image == null) return StrUtil.EMPTY;
        BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
        hints.put(DecodeHintType.CHARACTER_SET, DEFAULT_CHAR_SET);
        Result result = new MultiFormatReader().decode(bitmap, hints);
        return result.getText();
    }

    /**
     * 轉(zhuǎn)換為BufferedImage
     * @Date 2023/09/24 22:32
     * @Param [bitMatrix]
     * @return java.awt.image.BufferedImage
     */
    public static BufferedImage toBufferedImage(BitMatrix bitMatrix) throws IOException, WriterException {
        MatrixToImageConfig matrixToImageConfig = new MatrixToImageConfig(0xFF000001, 0xFFFFFFFF);
        BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix, matrixToImageConfig);
        return bufferedImage;
    }

    /**
     * 給二維碼添加logo
     * @Date 2023/09/24 22:33
     * @Param [bufferedImage, logoFile]
     * @return java.awt.image.BufferedImage
     */
    public static BufferedImage addQrCodeLogo(BufferedImage bufferedImage, File logoFile) throws IOException {
        Graphics2D graphics = bufferedImage.createGraphics();
        int matrixWidth = bufferedImage.getWidth();
        int matrixHeigh = bufferedImage.getHeight();

        // 讀取logo圖片文件
        BufferedImage logo = ImageIO.read(logoFile);
        int logoWidth = logo.getWidth();
        int logoHeight = logo.getHeight();

        //  計(jì)算logo放置位置
        int x = bufferedImage.getWidth()  / 5*2;
        int y = bufferedImage.getHeight() / 5*2;
        int width = matrixWidth / 5;
        int height = matrixHeigh / 5;

        // 開始繪制圖片
        graphics.drawImage(logo, x, y, width, height, null);
        graphics.drawRoundRect(x, y, logoWidth, logoHeight, 15, 15);
        graphics.setStroke(new BasicStroke(5.0F, 1, 1));
        graphics.setColor(Color.white);
        graphics.drawRect(x, y, logoWidth, logoHeight);

        graphics.dispose();
        bufferedImage.flush();
        return bufferedImage;
    }

    public static void main(String[] args) throws Exception {
        BufferedImage bufferedImage = toBufferedImage(createBitMatrix("https://blog.csdn.net", 300, 300));
        ImageIO.write(bufferedImage, "png", new File("D:/qrcode.jpg"));

        System.out.println(decodeQrCode(bufferedImage));

        BufferedImage logoQrCode = addQrCodeLogo(bufferedImage, new File("D://logo.png"));
        ImageIO.write(logoQrCode, "png", new File("D:/logoQrcode.jpg"));
    }

}

到此這篇關(guān)于Java基于Google zxing生成帶logo的二維碼圖片的文章就介紹到這了,更多相關(guān)Java zxing生成帶logo二維碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • springboot項(xiàng)目讀取resources目錄下的文件的9種方式

    springboot項(xiàng)目讀取resources目錄下的文件的9種方式

    本文主要介紹了springboot項(xiàng)目讀取resources目錄下的文件的9種方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • Java中對list map根據(jù)map某個(gè)key值進(jìn)行排序的方法

    Java中對list map根據(jù)map某個(gè)key值進(jìn)行排序的方法

    今天小編就為大家分享一篇Java中對list map根據(jù)map某個(gè)key值進(jìn)行排序的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Kotlin中l(wèi)et、run、with、apply及also的用法和差別

    Kotlin中l(wèi)et、run、with、apply及also的用法和差別

    作用域函數(shù)是Kotlin比較重要的一個(gè)特性,分為5種let、run、with、apply及also,這五個(gè)函數(shù)的工作方式非常相似,但是我們需要了解這5種函數(shù)的差異,以便在不同的場景更好的利用它,這篇文章主要介紹了Kotlin中l(wèi)et、run、with、apply及also的差別,需要的朋友可以參考下
    2023-11-11
  • Java switch 語句如何使用 String 參數(shù)

    Java switch 語句如何使用 String 參數(shù)

    這篇文章主要介紹了Java switch 語句如何使用 String 參數(shù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,,需要的朋友可以參考下
    2019-06-06
  • Java使用Jasypt進(jìn)行加密和解密的技術(shù)指南

    Java使用Jasypt進(jìn)行加密和解密的技術(shù)指南

    Jasypt (Java Simplified Encryption) 是一個(gè)簡化 Java 應(yīng)用中加密工作的庫,它支持加密和解密操作,易于與 Spring Boot 集成,通過 Jasypt,可以安全地管理敏感信息,比如數(shù)據(jù)庫密碼、API 密鑰等,本文介紹了Java使用Jasypt進(jìn)行加密和解密的技術(shù)指南,需要的朋友可以參考下
    2025-03-03
  • 10個(gè)避免Java內(nèi)存泄露的最佳實(shí)踐分享

    10個(gè)避免Java內(nèi)存泄露的最佳實(shí)踐分享

    即使有垃圾回收器的幫助,Java應(yīng)用程序仍然可能遭遇內(nèi)存泄漏問題,本文將介紹10個(gè)避免Java內(nèi)存泄漏的最佳實(shí)踐,大家可以根據(jù)需求自己進(jìn)行選擇
    2025-04-04
  • MyBatis中foreach標(biāo)簽的collection屬性的取值方式

    MyBatis中foreach標(biāo)簽的collection屬性的取值方式

    這篇文章主要介紹了MyBatis中foreach標(biāo)簽的collection屬性的取值方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • Java變量常量聲明和定義原理解析

    Java變量常量聲明和定義原理解析

    這篇文章主要介紹了Java變量常量聲明和定義原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • Java對象初始化過程代碼塊和構(gòu)造器的調(diào)用順序

    Java對象初始化過程代碼塊和構(gòu)造器的調(diào)用順序

    這篇文章主要介紹了Java對象初始化過程代碼塊和構(gòu)造器的調(diào)用順序,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-08-08
  • 實(shí)例解析使用Java實(shí)現(xiàn)基本的音頻播放器的編寫要點(diǎn)

    實(shí)例解析使用Java實(shí)現(xiàn)基本的音頻播放器的編寫要點(diǎn)

    這篇文章主要介紹了使用Java實(shí)現(xiàn)基本的音頻播放器的代碼要點(diǎn)實(shí)例分享,包括音頻文件的循環(huán)播放等功能實(shí)現(xiàn)的關(guān)鍵點(diǎn),需要的朋友可以參考下
    2016-01-01

最新評論