Android 二維碼開發功能實現(四)------基於Zxing實現編碼功能(生成二維碼,一維碼等)
Android 二維碼開發功能實現(四)------基於Zxing實現編碼功能(生成二維碼,一維碼等)
前言
關於Google的開源庫Zxing,前面也寫了幾遍文章進行介紹.我們先簡單的回顧一下!
- Android 二維碼的掃碼功能實現(一)
這篇文章主要介紹了,Zxing是什麼?怎麼上手?適合沒有接觸過條碼相關開發,0經驗的朋友. - Android 基於Zxing的掃碼功能實現(二)
這篇文章則介紹了,如何通過zxing來實現掃碼功能,並且對zxing的原始碼進行分析,介紹了掃碼的原理.還分享了一個基於Zxing開源的YZxing庫(YZxing ).方便大家學習,掌握. - Android 基於Zxing掃碼實現(三)、從相簿選取二維碼
這篇文章主要是對掃碼邏輯的一個優化處理,來提高掃碼的準確度和速度.以及介紹如何實現從相簿中選擇二維碼來掃碼的功能.
我們可以看到,之前的文章講的都是利用zxing來解碼(掃碼)的,所以這次我們說說編碼(生成條形碼)的功能實現. 我們先看一下實現的效果圖!
效果圖
核心程式碼
private void encode(String content) { Log.e(TAG, "encode: content = " + content); if (content == null) { return; } Map<EncodeHintType, Object> hints = null; String encoding = guessAppropriateEncoding(content); if (encoding != null) { hints = new EnumMap<>(EncodeHintType.class); hints.put(EncodeHintType.CHARACTER_SET, encoding); } BitMatrix result; try { result = new MultiFormatWriter().encode(content, getWantedCodeType(mType) , barcodeImageWidth, barcodeImageHeight, hints); int width = result.getWidth(); int height = result.getHeight(); int[] pixels = new int[width * height]; for (int y = 0; y < height; y++) { int offset = y * width; for (int x = 0; x < width; x++) { pixels[offset + x] = result.get(x, y) ? BLACK : WHITE; } } bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); mHandler.post(mUpdateImageRunnable); } catch (Exception iae) { // Unsupported format Log.e(TAG, "encode: " + iae.getMessage()); } }
實現編碼主要涉及到以下幾個類.
- MultiFormatWriter(這是一個工廠類,根據傳入的BarcodeFormat來找到最合適的一種**Writer類來編碼)
- QRCodeWriter (渲染二維碼)
- Encoder(編碼核心類)
- BitMatrix(相當於編碼後對資料進行渲染的一個容器,約束)
- MatrixUtil(渲染矩陣的工具類)
如果對編碼的底層邏輯有很深的需求的話,需要根據原始碼,我上面所提到的這幾個類,並且結合相關資料,可以自行了解.因為現實中開發任務,不涉及到很深的編碼演算法分析,本文是對編碼流程的一個簡單分析.
先看上面程式碼中的:
String encoding = guessAppropriateEncoding(content);
if (encoding != null) {
hints = new EnumMap<>(EncodeHintType.class);
hints.put(EncodeHintType.CHARACTER_SET, encoding);
}
這裡對編碼的內容進行判斷,來選擇合適的字元格式.這個格式引數在哪裡用到了呢?在Encoder中的encode方法中,有它的身影.
static final String DEFAULT_BYTE_MODE_ENCODING = "ISO-8859-1";
public static QRCode encode(String content,
ErrorCorrectionLevel ecLevel,
Map<EncodeHintType,?> hints) throws WriterException {
// Determine what character encoding has been specified by the caller, if any
String encoding = DEFAULT_BYTE_MODE_ENCODING;
if (hints != null && hints.containsKey(EncodeHintType.CHARACTER_SET)) {
encoding = hints.get(EncodeHintType.CHARACTER_SET).toString();
}
// Pick an encoding mode appropriate for the content. Note that this will not attempt to use
// multiple modes / segments even if that were more efficient. Twould be nice.
Mode mode = chooseMode(content, encoding);
...
省略...
...
}
我們可以看到,encoding在之前如果沒有傳入值的話,這邊會預設其值為"ISO-8859-1" ,並且,通過encoding和傳入的要編碼的內容來選擇,合適的Mode型別.選擇mode的方法是**chooseMode(String content, String encoding)**官方對該方法的解釋是尋找一個最合適的examining編碼內容的mode.選擇的邏輯還是比較簡單的,這裡看下就好.
private static Mode chooseMode(String content, String encoding) {
if ("Shift_JIS".equals(encoding) && isOnlyDoubleByteKanji(content)) {
// Choose Kanji mode if all input are double-byte characters
return Mode.KANJI;
}
boolean hasNumeric = false;
boolean hasAlphanumeric = false;
for (int i = 0; i < content.length(); ++i) {
char c = content.charAt(i);
if (c >= '0' && c <= '9') {
hasNumeric = true;
} else if (getAlphanumericCode(c) != -1) {
hasAlphanumeric = true;
} else {
return Mode.BYTE;
}
}
if (hasAlphanumeric) {
return Mode.ALPHANUMERIC;
}
if (hasNumeric) {
return Mode.NUMERIC;
}
return Mode.BYTE;
}
接著看這行程式碼:
BitMatrix result = new MultiFormatWriter().encode(content, getWantedCodeType(mType)
, barcodeImageWidth, barcodeImageHeight, hints);
這行程式碼是實現編碼功能的核心程式碼,
encode方法接受幾個引數:
- content -編碼內容
- format -編碼格式
- width -條碼影象寬度
- height -條碼影象高度
- Map<EncodeHintType, Object> -引數集 ,可選可不選,可以通過該引數集設定CHARACTER_SET引數,ERROR_CORRECTION引數等…
我們進入MultiFormatWriter的encode方法,發現這個方法會根據format的型別,繼續呼叫對應format的子集Writer的encode方法.
@Override
public BitMatrix encode(String contents,
BarcodeFormat format,
int width, int height,
Map<EncodeHintType,?> hints) throws WriterException {
Writer writer;
switch (format) {
case EAN_8:
writer = new EAN8Writer();
break;
case UPC_E:
writer = new UPCEWriter();
break;
case EAN_13:
writer = new EAN13Writer();
break;
case UPC_A:
writer = new UPCAWriter();
break;
case QR_CODE:
writer = new QRCodeWriter();
break;
case CODE_39:
writer = new Code39Writer();
break;
case CODE_93:
writer = new Code93Writer();
break;
case CODE_128:
writer = new Code128Writer();
break;
case ITF:
writer = new ITFWriter();
break;
case PDF_417:
writer = new PDF417Writer();
break;
case CODABAR:
writer = new CodaBarWriter();
break;
case DATA_MATRIX:
writer = new DataMatrixWriter();
break;
case AZTEC:
writer = new AztecWriter();
break;
default:
throw new IllegalArgumentException("No encoder available for format " + format);
}
return writer.encode(contents, format, width, height, hints);
}
因為我們主要研究QR Code 二維碼,所以我們看下,QRCodeWriter的encode方法.
@Override
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);
}
首先進行了,一些常規的錯誤排查判斷.然後設定了預設的錯誤糾正率級別,預設的是ErrorCorrectionLevel.L.
糾正率級別是一個列舉型別,有四種級別.級別越高,當你生成的二維碼被遮擋後,你掃碼成功的概率越大.
/** L = ~7% correction */
L(0x01),
/** M = ~15% correction */
M(0x00),
/** Q = ~25% correction */
Q(0x03),
/** H = ~30% correction */
H(0x02);
接著,通過Encoder的encode方法,對內容進行位元組編碼運算然後封裝成QRCode物件
.通過renderResult方法,把結果渲染封裝成BitMatrix物件,在這個方法裡用到了我們之前傳入的寬高參數,quietZone.這兩部分的具體邏輯有需求的話可以自己進原始碼看,邏輯不是很複雜,但是比較繁瑣,程式碼量挺多的,這邊就不在仔細分析了.
BitMatrix result;
try {
result = new MultiFormatWriter().encode(content, getWantedCodeType(mType)
, barcodeImageWidth, barcodeImageHeight, hints);
int width = result.getWidth();
int height = result.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
}
}
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
mHandler.post(mUpdateImageRunnable);
獲得的BitMatrix物件,在經過上面的bitmap物件生成操作之後,就會生成對應內容的條碼,生成條形碼的邏輯就完成了.
結語
通過zxing實現生成條碼的功能就完成了,本文所演示的程式碼和效果,已上傳至開源庫YZxing上,專案地址如下:
YZxing專案地址
如果需要對生成條形碼的深層演算法有興趣的話,可以參考文章二維碼生成細節 ,結合zxing原始碼,重點是我上文中提到的那幾個類的實現.