mirror of
https://github.com/zxing/zxing.git
synced 2025-01-12 11:47:26 -08:00
Add pure barcode support to web app and command line
git-svn-id: https://zxing.googlecode.com/svn/trunk@1228 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
1a12e1d03e
commit
aa8ad02e16
|
@ -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<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);
|
||||
|
@ -98,7 +98,8 @@ public final class CommandLineRunner {
|
|||
|
||||
// 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);
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -93,6 +93,7 @@ public final class DecodeServlet extends HttpServlet {
|
|||
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);
|
||||
|
@ -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<DecodeHintType, Object>(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) {
|
||||
|
|
Loading…
Reference in a new issue