Android生成二維碼工具類封裝及使用
一、生成二維碼工具類封裝
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<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.MARGIN, 0); // 設(shè)置邊距為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應(yīng)該放置的位置(中心) 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<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.MARGIN, 0); // 設(shè)置邊距為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應(yīng)該放置的位置(中心) 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; }
總結(jié)
到此這篇關(guān)于Android生成二維碼工具類封裝及使用的文章就介紹到這了,更多相關(guān)Android生成二維碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android開發(fā)使用自定義view實現(xiàn)ListView下拉的視差特效功能
這篇文章主要介紹了Android開發(fā)使用自定義view實現(xiàn)ListView下拉的視差特效功能,結(jié)合實例形式詳細(xì)分析了Android重寫ListView控件實現(xiàn)ListView下拉視差效果的步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-10-10Android拖拽助手ViewDragHelper的創(chuàng)建與使用實例
ViewDragHelper是針對 ViewGroup 中的拖拽和重新定位 views 操作時提供了一系列非常有用的方法和狀態(tài)追蹤,下面這篇文章主要給大家介紹了關(guān)于Android拖拽助手ViewDragHelper的創(chuàng)建與使用的相關(guān)資料,需要的朋友可以參考下2022-05-05Android shape和selector 結(jié)合使用實例代碼
本篇文章主要介紹了Android shape和selector 的使用,這里提供了shape 和selector 的詳細(xì)介紹,并附有代碼實例,有興趣的朋友可以參考下2016-07-07Recyclerview添加頭布局和尾布局、item點擊事件詳解
這篇文章主要為大家詳細(xì)介紹了Recyclerview添加頭布局和尾布局、item點擊事件的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08Android創(chuàng)建服務(wù)之started service詳細(xì)介紹
這篇文章主要介紹了Android創(chuàng)建服務(wù)之started service,需要的朋友可以參考下2014-02-02Android Service(不和用戶交互應(yīng)用組件)案例分析
Service是在一段不定的時間運(yùn)行在后臺,不和用戶交互應(yīng)用組件,本文將詳細(xì)介紹,需要了解的朋友可以參考下2012-12-12詳解OpenGL Shader彩虹條紋效果的實現(xiàn)
這篇文章主要為大家介紹了如何通過OpenGL Shader實現(xiàn)彩虹條紋效果,最后的效果和圖片處理軟件colorow中的彩虹效果濾鏡相似,需要的可以參考一下2022-02-02