/** * 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(); }
private Result[] decodeMulti(URI uri, Map<DecodeHintType,?> hints) throws IOException { BufferedImage image; try { image = ImageIO.read(uri.toURL()); } catch (IllegalArgumentException iae) { throw new FileNotFoundException("Resource not found: " + uri); } if (image == null) { System.err.println(uri.toString() + ": Could not load image"); return null; } try { LuminanceSource source; if (config.getCrop() == null) { source = new BufferedImageLuminanceSource(image); } else { int[] crop = config.getCrop(); source = new BufferedImageLuminanceSource(image, crop[0], crop[1], crop[2], crop[3]); } BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); if (config.isDumpBlackPoint()) { dumpBlackPoint(uri, image, bitmap); } MultiFormatReader multiFormatReader = new MultiFormatReader(); GenericMultipleBarcodeReader reader = new GenericMultipleBarcodeReader( multiFormatReader); Result[] results = reader.decodeMultiple(bitmap, hints); if (config.isBrief()) { System.out.println(uri.toString() + ": Success"); } else { for (Result result : results) { ParsedResult parsedResult = ResultParser.parseResult(result); System.out.println(uri.toString() + " (format: " + result.getBarcodeFormat() + ", type: " + parsedResult.getType() + "):\nRaw result:\n" + result.getText() + "\nParsed result:\n" + parsedResult.getDisplayResult()); System.out.println("Found " + result.getResultPoints().length + " result points."); for (int i = 0; i < result.getResultPoints().length; i++) { ResultPoint rp = result.getResultPoints()[i]; System.out.println(" Point " + i + ": (" + rp.getX() + ',' + rp.getY() + ')'); } } } return results; } catch (NotFoundException nfe) { System.out.println(uri.toString() + ": No barcode found"); return null; } }