hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //設(shè)置空白邊距的寬度 hints.put(EncodeHintType.MARGIN, 2);
//default is 4 // 圖像數(shù)據(jù)轉(zhuǎn)換,使用了矩陣轉(zhuǎn)換 BitMatrix bitMatrix =
new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, widthPix, heightPix, hints); int[] pixels =
new int[widthPix *
heightPix]; // 下面這里按照二維碼的算法,逐個(gè)生成二維碼的圖片, // 兩個(gè)for循環(huán)是圖片橫列掃描的結(jié)果 for (
int y = 0; y < heightPix; y++
) { for (
int x = 0; x < widthPix; x++
) { if (bitMatrix.get(x, y)) { pixels[y * widthPix + x] = 0xff000000
; } else { pixels[y * widthPix + x] = 0xffffffff
; } } } // 生成二維碼圖片的格式,使用ARGB_8888 Bitmap bitmap =
Bitmap.createBitmap(widthPix, heightPix, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, widthPix, 0, 0
, widthPix, heightPix); if (logoBm !=
null) { bitmap =
addLogo(bitmap, logoBm); } //必須使用compress方法將bitmap保存到文件中再進(jìn)行讀取。直接返回的bitmap是沒(méi)有任何壓縮的,內(nèi)存消耗巨大! return bitmap !=
null && bitmap.compress(Bitmap.CompressFormat.JPEG, 100,
new FileOutputStream(filePath)); } catch (WriterException |
IOException e) { e.printStackTrace(); } return false; }
②在二維碼中間添加Logo圖案
2/9 首頁(yè) 上一頁(yè) 1 2 3 4 5 6 下一頁(yè) 尾頁(yè)