欧美free性护士vide0shd,老熟女,一区二区三区,久久久久夜夜夜精品国产,久久久久久综合网天天,欧美成人护士h版

首頁綜合 正文
目錄

柚子快報激活碼778899分享:Android 生成二維碼

柚子快報激活碼778899分享:Android 生成二維碼

http://yzkb.51969.com/

一、生成二維碼工具類封裝

?1、二維碼庫

// 二維碼

implementation 'com.journeyapps:zxing-android-embedded:4.3.0'

2、工具類

/**

* 二維碼

* 處理工具

*/

public class QRCodeDealUtils {

/**

* @param content 字符串內(nèi)容

* @param size 位圖寬&高(單位:px)

* @param logo 二維碼logo

* @param logoPercent 二維碼logo的占比 [0,1]

* @return

*/

public static Bitmap createQRCodeBitmapLogo(String content, int size, Bitmap logo, float logoPercent) {

Bitmap qrCodeBitmap = null;

Bitmap bitmap;

try {

// 不帶logo二維碼

qrCodeBitmap = generateQRCodeWithoutMargin(content, size, size);

// 帶logo 二維碼

bitmap = addLogoToQRCode(qrCodeBitmap, logo, logoPercent);

} catch (WriterException e) {

throw new RuntimeException(e);

}

return bitmap;

}

/**

* 生成

* 無白色邊框

* 二維碼

*/

public static Bitmap generateQRCodeWithoutMargin(String text, int width, int height) throws WriterException {

QRCodeWriter qrCodeWriter = new QRCodeWriter();

Map hints = new HashMap<>();

hints.put(EncodeHintType.MARGIN, 0); // 設置邊距為0

BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height, hints);

int[] pixels = new int[width * height];

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

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

if (bitMatrix.get(x, y)) {

pixels[y * width + x] = Color.BLACK; // 黑色像素

} else {

pixels[y * width + x] = Color.TRANSPARENT; // 透明像素,去除白邊

}

}

}

Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

bitmap.setPixels(pixels, 0, width, 0, 0, width, height);

return bitmap;

}

/**

* 給二維碼

* 添加logo

*/

public static Bitmap addLogoToQRCode(Bitmap srcBitmap, Bitmap logoBitmap, float logoPercent) {

// 計算Logo相對于二維碼的尺寸

int logoWidth = Math.round(srcBitmap.getWidth() * logoPercent);

int logoHeight = Math.round(srcBitmap.getHeight() * logoPercent);

// 確保Logo尺寸不會超過二維碼尺寸

if (logoWidth > srcBitmap.getWidth() || logoHeight > srcBitmap.getHeight()) {

throw new IllegalArgumentException("Logo size is too large for the QR code.");

}

// 創(chuàng)建一個新的Bitmap來保存帶Logo的二維碼

Bitmap resultBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), srcBitmap.getConfig());

Canvas canvas = new Canvas(resultBitmap);

// 繪制原始二維碼

canvas.drawBitmap(srcBitmap, 0, 0, null);

// 創(chuàng)建一個Matrix對象來縮放Logo

Matrix matrix = new Matrix();

matrix.postScale(logoWidth / (float) logoBitmap.getWidth(),

logoHeight / (float) logoBitmap.getHeight());

// 計算Logo應該放置的位置(中心)

int xOffset = (srcBitmap.getWidth() - logoWidth) / 2;

int yOffset = (srcBitmap.getHeight() - logoHeight) / 2;

// 在二維碼上繪制Logo

canvas.drawBitmap(logoBitmap, xOffset, yOffset, null);

return resultBitmap;

}

}

二、方法說明

?1、不帶logo

/**

* 生成

* 無白色邊框

* 二維碼

*/

public static Bitmap generateQRCodeWithoutMargin(String text, int width, int height) throws WriterException {

QRCodeWriter qrCodeWriter = new QRCodeWriter();

Map hints = new HashMap<>();

hints.put(EncodeHintType.MARGIN, 0); // 設置邊距為0

BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height, hints);

int[] pixels = new int[width * height];

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

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

if (bitMatrix.get(x, y)) {

pixels[y * width + x] = Color.BLACK; // 黑色像素

} else {

pixels[y * width + x] = Color.TRANSPARENT; // 透明像素,去除白邊

}

}

}

Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

bitmap.setPixels(pixels, 0, width, 0, 0, width, height);

return bitmap;

}

? ?

2、給二維碼添加logo的方法

/**

* 給二維碼

* 添加logo

*/

public static Bitmap addLogoToQRCode(Bitmap srcBitmap, Bitmap logoBitmap, float logoPercent) {

// 計算Logo相對于二維碼的尺寸

int logoWidth = Math.round(srcBitmap.getWidth() * logoPercent);

int logoHeight = Math.round(srcBitmap.getHeight() * logoPercent);

// 確保Logo尺寸不會超過二維碼尺寸

if (logoWidth > srcBitmap.getWidth() || logoHeight > srcBitmap.getHeight()) {

throw new IllegalArgumentException("Logo size is too large for the QR code.");

}

// 創(chuàng)建一個新的Bitmap來保存帶Logo的二維碼

Bitmap resultBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), srcBitmap.getConfig());

Canvas canvas = new Canvas(resultBitmap);

// 繪制原始二維碼

canvas.drawBitmap(srcBitmap, 0, 0, null);

// 創(chuàng)建一個Matrix對象來縮放Logo

Matrix matrix = new Matrix();

matrix.postScale(logoWidth / (float) logoBitmap.getWidth(),

logoHeight / (float) logoBitmap.getHeight());

// 計算Logo應該放置的位置(中心)

int xOffset = (srcBitmap.getWidth() - logoWidth) / 2;

int yOffset = (srcBitmap.getHeight() - logoHeight) / 2;

// 在二維碼上繪制Logo

canvas.drawBitmap(logoBitmap, xOffset, yOffset, null);

return resultBitmap;

}

? 3、調(diào)用方式

? ??

/**

* @param content 字符串內(nèi)容

* @param size 位圖寬&高(單位:px)

* @param logo 二維碼logo

* @param logoPercent 二維碼logo的占比 [0,1]

* @return

*/

public static Bitmap createQRCodeBitmapLogo(String content, int size, Bitmap logo, float logoPercent) {

Bitmap qrCodeBitmap = null;

Bitmap bitmap;

try {

// 不帶logo二維碼

qrCodeBitmap = generateQRCodeWithoutMargin(content, size, size);

// 帶logo 二維碼

bitmap = addLogoToQRCode(qrCodeBitmap, logo, logoPercent);

} catch (WriterException e) {

throw new RuntimeException(e);

}

return bitmap;

}

到此結束

柚子快報激活碼778899分享:Android 生成二維碼

http://yzkb.51969.com/

好文閱讀

評論可見,查看隱藏內(nèi)容

本文內(nèi)容根據(jù)網(wǎng)絡資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。

轉載請注明,如有侵權,聯(lián)系刪除。

本文鏈接:http://gantiao.com.cn/post/19232242.html

發(fā)布評論

您暫未設置收款碼

請在主題配置——文章設置里上傳

掃描二維碼手機訪問

文章目錄