使用zxing生成二維碼bitmap:
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
// 圖像數據轉換,使用了矩陣轉換
BitMatrix bitMatrix = new QRCodeWriter().encode(url,
BarcodeFormat.QR_CODE, width, height, hints);
int[] pixels = new int[width * height];
// 下面這裏按照二維碼的算法,逐個生成二維碼的圖片,
// 兩個for循環是圖片橫列掃描的結果
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (bitMatrix.get(x, y)) {
pixels[y * width + x] = 0xff000000;
} else {
pixels[y * width + x] = 0xffffffff;
}
}
}
// 生成二維碼圖片的格式,使用ARGB_8888
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
這是最普通的二維碼生成使用方法,用這個方法生成的二維碼發現白色邊距很大,圖像是根據矩陣生成的,那我們看一下這個矩陣是怎麼生成的,點擊QRCodeWriter().encode()進入源碼查看:
public BitMatrix encode(String contents,
BarcodeFormat format,
int width,
int height,
Map<EncodeHintType,?> hints) throws WriterException {
if (contents.isEmpty()) {
throw new IllegalArgumentException("Found empty contents");
}
if (format != BarcodeFormat.QR_CODE) {
throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format);
}
if (width < 0 || height < 0) {
throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' +
height);
}
ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L;
int quietZone = QUIET_ZONE_SIZE;
if (hints != null) {
if (hints.containsKey(EncodeHintType.ERROR_CORRECTION)) {
errorCorrectionLevel = ErrorCorrectionLevel.valueOf(hints.get(EncodeHintType.ERROR_CORRECTION).toString());
}
if (hints.containsKey(EncodeHintType.MARGIN)) {
quietZone = Integer.parseInt(hints.get(EncodeHintType.MARGIN).toString());
}
}
QRCode code = Encoder.encode(contents, errorCorrectionLevel, hints);
//看這裏,這裏返回的是我們需要的矩陣
return renderResult(code, width, height, quietZone);
}
從這裏可以看到,最終返回的矩陣是從另一個方法返回的,繼續點擊renderResult進去查看:
private static BitMatrix renderResult(QRCode code, int width, int height, int quietZone) {
ByteMatrix input = code.getMatrix();
if (input == null) {
throw new IllegalStateException();
}
//這是我們傳進來的寬高
int inputWidth = input.getWidth();
int inputHeight = input.getHeight();
//再看,就是這裏,最終的二維碼寬高竟然給加了2個邊距,乘以2是左右和上下2個邊距的意思
int qrWidth = inputWidth + (quietZone * 2);
int qrHeight = inputHeight + (quietZone * 2);
int outputWidth = Math.max(width, qrWidth);
int outputHeight = Math.max(height, qrHeight);
int multiple = Math.min(outputWidth / qrWidth, outputHeight / qrHeight);
// Padding includes both the quiet zone and the extra white pixels to accommodate the requested
// dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.
// If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will
// handle all the padding from 100x100 (the actual QR) up to 200x160.
int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
int topPadding = (outputHeight - (inputHeight * multiple)) / 2;
BitMatrix output = new BitMatrix(outputWidth, outputHeight);
for (int inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) {
// Write the contents of this row of the barcode
for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) {
if (input.get(inputX, inputY) == 1) {
output.setRegion(outputX, outputY, multiple, multiple);
}
}
}
return output;
}
從這裏看到了,最終的寬高被加了邊距,邊距quietZone是從調用處傳進來的,那我們回頭看一下encode這個方法傳進來的這個值是怎麼來的:
//這裏定義了一個默認值
int quietZone = QUIET_ZONE_SIZE;
if (hints != null) {
if (hints.containsKey(EncodeHintType.ERROR_CORRECTION)) {
errorCorrectionLevel = ErrorCorrectionLevel.valueOf(hints.get(EncodeHintType.ERROR_CORRECTION).toString());
}
//這裏判斷了hints是否包含margin
if (hints.containsKey(EncodeHintType.MARGIN)) {
//再看這裏,從hint中獲取了margin並賦值
quietZone = Integer.parseInt(hints.get(EncodeHintType.MARGIN).toString());
}
}
到了這裏就可以發現,我們的白邊其實是quietZone這個玩意兒搗鬼,還記得最開始的時候我們傳進來的hints嗎,我們在裏面添加了一個編碼格式hints.put(EncodeHintType.CHARACTER_SET, "utf-8"),同樣我們可以在這裏傳進來一個margin,這樣如果encode時可以取到我們的margin,那麼就會使用我們的margin,這樣就可以隨意控制白邊的寬度了,如下:
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.MARGIN,"2");
經過測試,一點白邊也沒有很醜,margin爲2時最好看,如果你不想要白邊,那麼margin賦值0即可。
完畢!