1.前言
ZXing是google官方推出的跨平臺的基于Java實現(xiàn)處理掃面二維碼或者條形碼的庫。支持很多格式热某,一維條碼支持UPC-A,UPC-E,EAN-8昔馋,Code 39筹吐,Code 93等格式,二維條碼支持QR Code秘遏,Data Matrix丘薛,PDF 417,MaxiCode等格式垄提。
我們通常使用ZXing掃描的時候都是一個一個的去掃榔袋,但是用戶的實際操作環(huán)境卻不盡然。比如說下圖:
image
2.解決方案
ZXing中有一個類GenericMultipleBarcodeReader
看下這個類的源碼:
public final class GenericMultipleBarcodeReader implements MultipleBarcodeReader {
private static final int MIN_DIMENSION_TO_RECUR = 100;
private static final int MAX_DEPTH = 4;
private final Reader delegate;
public GenericMultipleBarcodeReader(Reader delegate) {
this.delegate = delegate;
}
@Override
public Result[] decodeMultiple(BinaryBitmap image) throws NotFoundException {
return decodeMultiple(image, null);
}
@Override
public Result[] decodeMultiple(BinaryBitmap image, Map<DecodeHintType,?> hints)
throws NotFoundException {
List<Result> results = new ArrayList<>();
doDecodeMultiple(image, hints, results, 0, 0, 0);
if (results.isEmpty()) {
throw NotFoundException.getNotFoundInstance();
}
return results.toArray(new Result[results.size()]);
}
private void doDecodeMultiple(BinaryBitmap image,
Map<DecodeHintType,?> hints,
List<Result> results,
int xOffset,
int yOffset,
int currentDepth) {
if (currentDepth > MAX_DEPTH) {
return;
}
Result result;
try {
result = delegate.decode(image, hints);
} catch (ReaderException ignored) {
return;
}
boolean alreadyFound = false;
for (Result existingResult : results) {
if (existingResult.getText().equals(result.getText())) {
alreadyFound = true;
break;
}
}
if (!alreadyFound) {
results.add(translateResultPoints(result, xOffset, yOffset));
}
ResultPoint[] resultPoints = result.getResultPoints();
if (resultPoints == null || resultPoints.length == 0) {
return;
}
int width = image.getWidth();
int height = image.getHeight();
float minX = width;
float minY = height;
float maxX = 0.0f;
float maxY = 0.0f;
for (ResultPoint point : resultPoints) {
if (point == null) {
continue;
}
float x = point.getX();
float y = point.getY();
if (x < minX) {
minX = x;
}
if (y < minY) {
minY = y;
}
if (x > maxX) {
maxX = x;
}
if (y > maxY) {
maxY = y;
}
}
// Decode left of barcode
if (minX > MIN_DIMENSION_TO_RECUR) {
doDecodeMultiple(image.crop(0, 0, (int) minX, height),
hints, results,
xOffset, yOffset,
currentDepth + 1);
}
// Decode above barcode
if (minY > MIN_DIMENSION_TO_RECUR) {
doDecodeMultiple(image.crop(0, 0, width, (int) minY),
hints, results,
xOffset, yOffset,
currentDepth + 1);
}
// Decode right of barcode
if (maxX < width - MIN_DIMENSION_TO_RECUR) {
doDecodeMultiple(image.crop((int) maxX, 0, width - (int) maxX, height),
hints, results,
xOffset + (int) maxX, yOffset,
currentDepth + 1);
}
// Decode below barcode
if (maxY < height - MIN_DIMENSION_TO_RECUR) {
doDecodeMultiple(image.crop(0, (int) maxY, width, height - (int) maxY),
hints, results,
xOffset, yOffset + (int) maxY,
currentDepth + 1);
}
}
private static Result translateResultPoints(Result result, int xOffset, int yOffset) {
ResultPoint[] oldResultPoints = result.getResultPoints();
if (oldResultPoints == null) {
return result;
}
ResultPoint[] newResultPoints = new ResultPoint[oldResultPoints.length];
for (int i = 0; i < oldResultPoints.length; i++) {
ResultPoint oldPoint = oldResultPoints[i];
if (oldPoint != null) {
newResultPoints[i] = new ResultPoint(oldPoint.getX() + xOffset, oldPoint.getY() + yOffset);
}
}
Result newResult = new Result(result.getText(),
result.getRawBytes(),
result.getNumBits(),
newResultPoints,
result.getBarcodeFormat(),
result.getTimestamp());
newResult.putAllMetadata(result.getResultMetadata());
return newResult;
}
}
image
可以看到GenericMultipleBarcodeReader
中decodeMultiple
返回 Result[]
,所以我們可以根據(jù)這個類實現(xiàn)條形碼的多個返回铡俐。
3.實現(xiàn)過程
3.1 gradle引入ZXing包
implementation('com.journeyapps:zxing-android-embedded:3.6.0') { transitive = false }
implementation 'com.google.zxing:core:3.3.2'
3.2 調用ZXing的掃描頁面
IntentIntegrator intentIntegrator = new IntentIntegrator(MainActivity.this);
intentIntegrator
.setPrompt("")
.setBeepEnabled(false)
.setBarcodeImageEnabled(true)
.setDesiredBarcodeFormats(IntentIntegrator.CODE_128)
.initiateScan();
這里將setBarcodeImageEnabled(true)
設置成true凰兑,就可以在activity的回調里面獲取到掃描的二維碼的路徑。
3.3 在onActivityResult
中處理
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
LogUtils.i(TAG,result.toString());
Bitmap bMap = BitmapFactory.decodeFile(result.getBarcodeImagePath());
int[] data2 = new int[bMap.getWidth() * bMap.getHeight()];
bMap.getPixels(data2, 0, bMap.getWidth(), 0, 0, bMap.getWidth(), bMap.getHeight());
RGBLuminanceSource rgbLuminanceSource = new RGBLuminanceSource(bMap.getWidth(),bMap.getHeight(),data2);
LuminanceSource source = rgbLuminanceSource;
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object> ();
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
MultiFormatReader mreader = new MultiFormatReader();
GenericMultipleBarcodeReader multireader = new GenericMultipleBarcodeReader(mreader);
try {
Result[] result2 = multireader.decodeMultiple(bitmap,hints);
if(result != null){
for(Result kp : result2)
{
System.out.println(kp.toString());
LogUtils.i(TAG,kp.toString());
}
}
} catch (NotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
4.運行效果
在log中我們可以看到掃描的結果
image
image
5.結論
我們可以通過GenericMultipleBarcodeReader
來實現(xiàn)掃面多個條形碼审丘,但是效果可能不是很穩(wěn)定吏够,這個識別的效果跟相機掃描出來的圖片有關,先記下來滩报,以后再好好優(yōu)化下锅知。