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

Java 生成帶Logo和文字的二維碼

 更新時間:2021年04月23日 09:57:25   作者:攻城獅·正  
這篇文章主要介紹了Java 生成帶Logo和文字的二維碼的方法,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下

ZXing 是一個開放源碼的,用Java實現(xiàn)的多種格式的1D/2D條碼圖像處理庫,它包含了聯(lián)系到其他語言的端口。Zxing 可以實現(xiàn)使用手機的內(nèi)置的攝像頭完成條形碼的掃描及解碼。本章講解用 ZXing 生成和掃碼二維碼。

依賴

在Java項目中pom.xml加入:

<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>${version}</version>
</dependency>
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>${version}</version>
</dependency>

當(dāng)前最新版本是3.4.1,如果是Java開發(fā)的Android項目則引入 android-core 。

生成二維碼

生成普通二維碼

// 二維碼內(nèi)容
String text = "https://engr-z.com";
// 二維碼大小
int width = 500, height = 500;
// 二維碼輸出文件
File file = new File("/home/engr-z/qrcode.png");

QRCodeWriter writer = new QRCodeWriter();
BitMatrix m = writer.encode(text, BarcodeFormat.QR_CODE, width, height);
MatrixToImageWriter.writeToPath(m, "png", file.toPath());

如果內(nèi)容較多,需要增大二維碼尺寸。尺寸小內(nèi)容多,二維碼圖形越復(fù)雜越難識別。

生成帶Logo二維碼

// 二維碼內(nèi)容
String text = "https://engr-z.com";
// 二維碼大小
int width = 500, height = 500;
// 二維碼參數(shù)
CodeStyle style = new CodeStyle();
style.setWidth(width);
style.setHeight(height);

Map<EncodeHintType, Object> hints = new HashMap<>();
//內(nèi)容編碼格式
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
// 指定糾錯等級
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
//設(shè)置二維碼邊的空度,非負數(shù)
hints.put(EncodeHintType.MARGIN, 1);

// 生成二維碼圖片
QRCodeWriter writer = new QRCodeWriter();
BitMatrix bm = writer.encode(text, BarcodeFormat.QR_CODE, style.getWidth(), style.getHeight(), hints);

int margin = style.getMargin();
int tempM = margin*2;
int[] rec = bm.getEnclosingRectangle();   //獲取二維碼圖案的屬性
int resWidth = rec[2] + tempM;
int resHeight = rec[3] + tempM;
BitMatrix resMatrix = new BitMatrix(resWidth, resHeight); // 按照自定義邊框生成新的BitMatrix
resMatrix.clear();
for (int i = margin; i < resWidth - margin; i++) {   //循環(huán),將二維碼圖案繪制到新的bitMatrix中
    for (int j = margin; j < resHeight - margin; j++){
        if (bm.get(i - margin + rec[0], j - margin + rec[1])){
            resMatrix.set(i, j);
        }
    }
}
bm = resMatrix;

int w = bm.getWidth();
int h = bm.getHeight();
BufferedImage qrcodeBuffImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);

// 開始利用二維碼數(shù)據(jù)創(chuàng)建Bitmap圖片
for (int x = 0; x < w; x++) {
    for (int y = 0; y < h; y++) {
        qrcodeBuffImg.setRGB(x, y, bm.get(x, y) ? style.getCodeColor() : style.getBackgroundColor());
    }
}

/**
 * 讀取Logo圖片
 */
File logoFile = new File("/home/engr-z/logo.png");
BufferedImage logo = ImageIO.read(logoFile);
/**
 * 設(shè)置logo的大小,設(shè)置為二維碼圖片的20%
 */
int widthLogo = logo.getWidth(null) > qrcodeBuffImg.getWidth() * 3 / 10 ? (qrcodeBuffImg.getWidth() * 3 / 10)
        : logo.getWidth(null),
        heightLogo = logo.getHeight(null) > qrcodeBuffImg.getHeight() * 3 / 10 ? (qrcodeBuffImg.getHeight() * 3 / 10) : logo.getWidth(null);

/**
 * logo在二維碼的位置
 */
int x = (qrcodeBuffImg.getWidth() - widthLogo) / 2;
int y = (qrcodeBuffImg.getHeight() - heightLogo) / 2;

Graphics2D qrcodeOutg = qrcodeBuffImg.createGraphics();
// 把logo寫到二維碼圖片中間
qrcodeOutg.drawImage(logo, x, y, widthLogo, heightLogo, null);
qrcodeOutg.dispose();
qrcodeBuffImg.flush();

// 新的圖片,把帶logo的二維碼下面加上文字
String desc = "https://engr-z.com";
int textHeight = 26;
int textMargin = 10;
BufferedImage outImage = new BufferedImage(qrcodeBuffImg.getWidth(), qrcodeBuffImg.getHeight() + textHeight + (textMargin * 2), BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D outg = outImage.createGraphics();
// 畫二維碼到新的面板
outg.drawImage(qrcodeBuffImg, 0, 0, qrcodeBuffImg.getWidth(), qrcodeBuffImg.getHeight(), null);
outg.setFont(new Font("宋體", Font.BOLD, 26)); // 字體、字型、字號
int strWidth = outg.getFontMetrics().stringWidth(desc);
outg.setColor(Color.BLACK);
outg.drawString(desc, (outImage.getWidth() - strWidth) / 2, outImage.getHeight() - textMargin);
outg.dispose();

// 二維碼輸出文件
File file = new File("/home/engr-z/qrcode.png");
ImageIO.write(outImage, "png", file);

CodeStyle是我封裝的類,用來設(shè)置二維碼樣式:

/**
 * @author Engr-Z
 * @since 2020/12/18
 */
@Data
public class CodeStyle {

    /**
     * 背景顏色,如:0xFFFFFFFF
     */
    private int backgroundColor = 0xFFFFFFFF;

    /**
     * 碼顏色,如:0xFF000000
     */
    private int codeColor = 0xFF000000;

    /**
     * 二維碼寬,px
     */
    private int width;

    /**
     * 二維碼高,px
     */
    private int height;

    /**
     * 邊框大小
     */
    private int margin = 5;

}

以下是我執(zhí)行生成的二維碼:

讀取二維碼

File qrcodeFile = new File("D:/qrcode.png");
BufferedImage qrcodeImg = ImageIO.read(qrcodeFile);

MultiFormatReader formatReader = new MultiFormatReader();
//讀取指定的二維碼文件
BinaryBitmap binaryBitmap= new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(qrcodeImg)));
//定義二維碼參數(shù)
Map<DecodeHintType, Object> hints= new HashMap<>();
hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
// 讀取
Result result = formatReader.decode(binaryBitmap, hints);
log.info("格式類型:{}", result.getBarcodeFormat());
log.info("二維碼內(nèi)容:{}", result.getText());

鏈接

ZXing GitHub:https://github.com/zxing/zxing

以上就是Java 生成帶Logo和文字的二維碼的詳細內(nèi)容,更多關(guān)于Java 生成二維碼的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Spring-Retry(重試機制)解讀

    Spring-Retry(重試機制)解讀

    這篇文章主要介紹了Spring-Retry(重試機制),具有很好的價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • 深入了解Java中的類加載機制

    深入了解Java中的類加載機制

    通常,在關(guān)于Java的類加載部分會遇到以上疑問,本文將對類加載重要部分做詳細介紹,包括重要的基礎(chǔ)概念和應(yīng)用場景,在編寫過程中也幫助作者重新熟悉并加固了知識點,希望在看完后對大家能有所幫助
    2022-11-11
  • Mybatis逆工程jar包的修改和打包

    Mybatis逆工程jar包的修改和打包

    這篇文章主要介紹了Mybatis逆工程jar包的修改和打包的相關(guān)資料,需要的朋友可以參考下
    2016-06-06
  • Java基礎(chǔ)之?dāng)?shù)組模擬循環(huán)隊列

    Java基礎(chǔ)之?dāng)?shù)組模擬循環(huán)隊列

    這篇文章主要介紹了Java基礎(chǔ)之?dāng)?shù)組模擬循環(huán)隊列,文中有非常詳細的代碼示例,對正在學(xué)習(xí)java基礎(chǔ)的小伙伴們也有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • 解決MultipartFile.transferTo(dest) 報FileNotFoundExcep的問題

    解決MultipartFile.transferTo(dest) 報FileNotFoundExcep的問題

    這篇文章主要介紹了解決MultipartFile.transferTo(dest) 報FileNotFoundExcep的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Java中線程安全有哪些實現(xiàn)思路

    Java中線程安全有哪些實現(xiàn)思路

    在 Java 多線程編程中,線程安全是一個非常重要的概念,本文主要介紹了Java中線程安全有哪些實現(xiàn)思路,非常具有實用價值,需要的朋友可以參考下
    2023-05-05
  • SpringBoot項目啟動時增加自定義Banner的簡單方法

    SpringBoot項目啟動時增加自定義Banner的簡單方法

    最近看到springboot可以自定義啟動時的banner,然后自己試了一下,下面這篇文章主要給大家介紹了SpringBoot項目啟動時增加自定義Banner的簡單方法,需要的朋友可以參考下
    2022-01-01
  • Java如何判斷一個整數(shù)有多少位

    Java如何判斷一個整數(shù)有多少位

    這篇文章主要介紹了Java如何判斷一個整數(shù)有多少位問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • java實現(xiàn)PDF轉(zhuǎn)圖片的方法

    java實現(xiàn)PDF轉(zhuǎn)圖片的方法

    這篇文章主要為大家詳細介紹了java實現(xiàn)PDF轉(zhuǎn)圖片的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • Springboot中使用攔截器、過濾器、監(jiān)聽器的流程分析

    Springboot中使用攔截器、過濾器、監(jiān)聽器的流程分析

    Javaweb三大組件:servlet、Filter(過濾器)、?Listener(監(jiān)聽器),這篇文章主要介紹了Springboot中使用攔截器、過濾器、監(jiān)聽器的流程分析,感興趣的朋友跟隨小編一起看看吧
    2023-12-12

最新評論