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;
}
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)) {
}
}
- Hashtable<DecodeHintType, Object> hints = buildHints(tryHarder, productsOnly);
+ Hashtable<DecodeHintType, Object> hints = buildHints(tryHarder, pureBarcode, productsOnly);
for (String arg : args) {
if (!arg.startsWith("--")) {
decodeOneArgument(arg, hints, dumpResults, dumpBlackPoint);
// Manually turn on all formats, even those not yet considered production quality.
private static Hashtable<DecodeHintType, Object> buildHints(boolean tryHarder,
- boolean productsOnly) {
+ boolean pureBarcode,
+ boolean productsOnly) {
Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
Vector<BarcodeFormat> vector = new Vector<BarcodeFormat>(8);
vector.addElement(BarcodeFormat.UPC_A);
if (tryHarder) {
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
}
+ if (pureBarcode) {
+ hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
+ }
return hints;
}
private static final Logger log = Logger.getLogger(DecodeServlet.class.getName());
static final Hashtable<DecodeHintType, Object> HINTS;
+ static final Hashtable<DecodeHintType, Object> HINTS_PURE;
static {
HINTS = new Hashtable<DecodeHintType, Object>(5);
possibleFormats.add(BarcodeFormat.DATAMATRIX);
possibleFormats.add(BarcodeFormat.PDF417);
HINTS.put(DecodeHintType.POSSIBLE_FORMATS, possibleFormats);
+ HINTS_PURE = new Hashtable<DecodeHintType, Object>(HINTS);
+ HINTS_PURE.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
}
private HttpParams params;
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");
}
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 {
- // Try again with other binarizer
- LuminanceSource source = new BufferedImageLuminanceSource(image);
- BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
+ // Look for normal barcode in photo
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;
+ } catch (ReaderException re) {
+ savedException = re;
+ }
+ }
+
+ if (result == null) {
+ try {
+ // Try again with other binarizer
+ 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");
}
}
+ 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) {