ZXing是google官方推出的跨平臺的基於Java實現處理掃面二維碼或者條形碼的庫。支持不少格式,一維條碼支持UPC-A,UPC-E,EAN-8,Code 39,Code 93等格式,二維條碼支持QR Code,Data Matrix,PDF 417,MaxiCode等格式。java
咱們一般使用ZXing掃描的時候都是一個一個的去掃,可是用戶的實際操做環境卻不盡然。好比說下圖:android
ZXing中有一個類GenericMultipleBarcodeReader
app
看下這個類的源碼:ide
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; } }
能夠看到GenericMultipleBarcodeReader
中decodeMultiple
返回 Result[]
,因此咱們能夠根據這個類實現條形碼的多個返回。gradle
implementation('com.journeyapps:zxing-android-embedded:3.6.0') { transitive = false } implementation 'com.google.zxing:core:3.3.2'
IntentIntegrator intentIntegrator = new IntentIntegrator(MainActivity.this); intentIntegrator .setPrompt("") .setBeepEnabled(false) .setBarcodeImageEnabled(true) .setDesiredBarcodeFormats(IntentIntegrator.CODE_128) .initiateScan();
這裏將setBarcodeImageEnabled(true)
設置成true,就能夠在activity的回調裏面獲取到掃描的二維碼的路徑。優化
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(); } }
在log中咱們能夠看到掃描的結果 this
咱們能夠經過GenericMultipleBarcodeReader
來實現掃面多個條形碼,可是效果可能不是很穩定,這個識別的效果跟相機掃描出來的圖片有關,先記下來,之後再好好優化下。google