/** * Decode all barcodes in the image */ private String multiple(BufferedImage img) throws NotFoundException, ChecksumException, FormatException { long begin = System.currentTimeMillis(); LuminanceSource source = new BufferedImageLuminanceSource(img); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); com.google.zxing.Reader reader = new MultiFormatReader(); MultipleBarcodeReader bcReader = new GenericMultipleBarcodeReader(reader); Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(); hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE); StringBuilder sb = new StringBuilder(); for (Result result : bcReader.decodeMultiple(bitmap, hints)) { sb.append(result.getText()).append(" "); } SystemProfiling.log(null, System.currentTimeMillis() - begin); log.trace("multiple.Time: {}", System.currentTimeMillis() - begin); return sb.toString(); }
@Test public void testMultiQRCodes() throws Exception { // Very basic test for now Path testBase = AbstractBlackBoxTestCase.buildTestBase("src/test/resources/blackbox/multi-qrcode-1"); Path testImage = testBase.resolve("1.png"); BufferedImage image = ImageIO.read(testImage.toFile()); LuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); MultipleBarcodeReader reader = new QRCodeMultiReader(); Result[] results = reader.decodeMultiple(bitmap); assertNotNull(results); assertEquals(4, results.length); Set<String> barcodeContents = new HashSet<>(); for (Result result : results) { barcodeContents.add(result.getText()); assertEquals(BarcodeFormat.QR_CODE, result.getBarcodeFormat()); Map<ResultMetadataType,Object> metadata = result.getResultMetadata(); assertNotNull(metadata); } Set<String> expectedContents = new HashSet<>(); expectedContents.add("You earned the class a 5 MINUTE DANCE PARTY!! Awesome! Way to go! Let's boogie!"); expectedContents.add("You earned the class 5 EXTRA MINUTES OF RECESS!! Fabulous!! Way to go!!"); expectedContents.add("You get to SIT AT MRS. SIGMON'S DESK FOR A DAY!! Awesome!! Way to go!! Guess I better clean up! :)"); expectedContents.add("You get to CREATE OUR JOURNAL PROMPT FOR THE DAY! Yay! Way to go! "); assertEquals(expectedContents, barcodeContents); }