diff --git a/javase/src/com/google/zxing/client/j2se/CommandLineRunner.java b/javase/src/com/google/zxing/client/j2se/CommandLineRunner.java index 61ca47a94..03ba80481 100644 --- a/javase/src/com/google/zxing/client/j2se/CommandLineRunner.java +++ b/javase/src/com/google/zxing/client/j2se/CommandLineRunner.java @@ -18,13 +18,10 @@ package com.google.zxing.client.j2se; import com.google.zxing.BarcodeFormat; import com.google.zxing.BinaryBitmap; -import com.google.zxing.ChecksumException; import com.google.zxing.DecodeHintType; -import com.google.zxing.FormatException; import com.google.zxing.LuminanceSource; import com.google.zxing.MultiFormatReader; import com.google.zxing.NotFoundException; -import com.google.zxing.ReaderException; import com.google.zxing.Result; import com.google.zxing.client.result.ParsedResult; import com.google.zxing.client.result.ResultParser; @@ -69,12 +66,15 @@ public final class CommandLineRunner { } boolean tryHarder = false; + boolean pureBarcode = false; boolean productsOnly = false; boolean dumpResults = false; boolean dumpBlackPoint = false; for (String arg : args) { if ("--try_harder".equals(arg)) { tryHarder = true; + } else if ("--pure_barcode".equals(arg)) { + pureBarcode = true; } else if ("--products_only".equals(arg)) { productsOnly = true; } else if ("--dump_results".equals(arg)) { @@ -88,7 +88,7 @@ public final class CommandLineRunner { } } - Hashtable hints = buildHints(tryHarder, productsOnly); + Hashtable hints = buildHints(tryHarder, pureBarcode, productsOnly); for (String arg : args) { if (!arg.startsWith("--")) { decodeOneArgument(arg, hints, dumpResults, dumpBlackPoint); @@ -98,7 +98,8 @@ public final class CommandLineRunner { // Manually turn on all formats, even those not yet considered production quality. private static Hashtable buildHints(boolean tryHarder, - boolean productsOnly) { + boolean pureBarcode, + boolean productsOnly) { Hashtable hints = new Hashtable(3); Vector vector = new Vector(8); vector.addElement(BarcodeFormat.UPC_A); @@ -117,6 +118,9 @@ public final class CommandLineRunner { if (tryHarder) { hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE); } + if (pureBarcode) { + hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE); + } return hints; } diff --git a/zxingorg/src/com/google/zxing/web/DecodeServlet.java b/zxingorg/src/com/google/zxing/web/DecodeServlet.java index 3926f0f8c..dce1be793 100644 --- a/zxingorg/src/com/google/zxing/web/DecodeServlet.java +++ b/zxingorg/src/com/google/zxing/web/DecodeServlet.java @@ -93,6 +93,7 @@ public final class DecodeServlet extends HttpServlet { private static final Logger log = Logger.getLogger(DecodeServlet.class.getName()); static final Hashtable HINTS; + static final Hashtable HINTS_PURE; static { HINTS = new Hashtable(5); @@ -110,6 +111,8 @@ public final class DecodeServlet extends HttpServlet { possibleFormats.add(BarcodeFormat.DATAMATRIX); possibleFormats.add(BarcodeFormat.PDF417); HINTS.put(DecodeHintType.POSSIBLE_FORMATS, possibleFormats); + HINTS_PURE = new Hashtable(HINTS); + HINTS_PURE.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE); } private HttpParams params; @@ -245,6 +248,7 @@ public final class DecodeServlet extends HttpServlet { private static void processStream(InputStream is, ServletRequest request, HttpServletResponse response) throws ServletException, IOException { + BufferedImage image = ImageIO.read(is); if (image == null) { response.sendRedirect("badimage.jspx"); @@ -252,32 +256,42 @@ public final class DecodeServlet extends HttpServlet { } Reader reader = new MultiFormatReader(); - Result result; + LuminanceSource source = new BufferedImageLuminanceSource(image); + BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source)); + Result result = null; + ReaderException savedException = null; + try { - LuminanceSource source = new BufferedImageLuminanceSource(image); - BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source)); - result = reader.decode(bitmap, HINTS); + // Look for pure barcode + result = reader.decode(bitmap, HINTS_PURE); } catch (ReaderException re) { + savedException = re; + } + + if (result == null) { + try { + // Look for normal barcode in photo + result = reader.decode(bitmap, HINTS); + } catch (ReaderException re) { + savedException = re; + } + } + + if (result == null) { try { // Try again with other binarizer - LuminanceSource source = new BufferedImageLuminanceSource(image); - BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); - result = reader.decode(bitmap, HINTS); - } catch (NotFoundException nfe) { - log.info("Not found: " + re.toString()); - response.sendRedirect("notfound.jspx"); - return; - } catch (FormatException fe) { - log.info("Format problem: " + re.toString()); - response.sendRedirect("format.jspx"); - return; - } catch (ChecksumException ce) { - log.info("Checksum problem: " + re.toString()); - response.sendRedirect("format.jspx"); - return; + BinaryBitmap hybridBitmap = new BinaryBitmap(new HybridBinarizer(source)); + result = reader.decode(hybridBitmap, HINTS); + } catch (ReaderException re) { + savedException = re; } } + if (result == null) { + handleException(savedException, response); + return; + } + if (request.getParameter("full") == null) { response.setContentType("text/plain"); response.setCharacterEncoding("UTF8"); @@ -313,6 +327,22 @@ public final class DecodeServlet extends HttpServlet { } } + private static void handleException(ReaderException re, HttpServletResponse response) throws IOException { + if (re instanceof NotFoundException) { + log.info("Not found: " + re); + response.sendRedirect("notfound.jspx"); + } else if (re instanceof FormatException) { + log.info("Format problem: " + re); + response.sendRedirect("format.jspx"); + } else if (re instanceof ChecksumException) { + log.info("Checksum problem: " + re); + response.sendRedirect("format.jspx"); + } else { + log.info("Unknown problem: " + re); + response.sendRedirect("notfound.jspx"); + } + } + private static boolean isSizeOK(HttpMessage getResponse) { Header lengthHeader = getResponse.getLastHeader("Content-Length"); if (lengthHeader != null) {