2 * Copyright 2008 ZXing authors
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.zxing.web;
19 import com.google.zxing.BarcodeFormat;
20 import com.google.zxing.BinaryBitmap;
21 import com.google.zxing.ChecksumException;
22 import com.google.zxing.DecodeHintType;
23 import com.google.zxing.FormatException;
24 import com.google.zxing.LuminanceSource;
25 import com.google.zxing.MultiFormatReader;
26 import com.google.zxing.NotFoundException;
27 import com.google.zxing.Reader;
28 import com.google.zxing.ReaderException;
29 import com.google.zxing.Result;
30 import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
31 import com.google.zxing.common.GlobalHistogramBinarizer;
32 import com.google.zxing.common.HybridBinarizer;
34 import com.google.zxing.multi.GenericMultipleBarcodeReader;
35 import com.google.zxing.multi.MultipleBarcodeReader;
36 import org.apache.commons.fileupload.FileItem;
37 import org.apache.commons.fileupload.FileUploadException;
38 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
39 import org.apache.commons.fileupload.servlet.ServletFileUpload;
40 import org.apache.http.Header;
41 import org.apache.http.HttpMessage;
42 import org.apache.http.HttpResponse;
43 import org.apache.http.HttpVersion;
44 import org.apache.http.HttpEntity;
45 import org.apache.http.client.HttpClient;
46 import org.apache.http.client.methods.HttpGet;
47 import org.apache.http.client.methods.HttpUriRequest;
48 import org.apache.http.conn.scheme.PlainSocketFactory;
49 import org.apache.http.conn.scheme.Scheme;
50 import org.apache.http.conn.scheme.SchemeRegistry;
51 import org.apache.http.conn.ssl.SSLSocketFactory;
52 import org.apache.http.conn.ClientConnectionManager;
53 import org.apache.http.impl.client.DefaultHttpClient;
54 import org.apache.http.impl.conn.SingleClientConnManager;
55 import org.apache.http.params.BasicHttpParams;
56 import org.apache.http.params.HttpParams;
57 import org.apache.http.params.HttpProtocolParams;
59 import java.awt.image.BufferedImage;
60 import java.io.IOException;
61 import java.io.InputStream;
62 import java.io.OutputStreamWriter;
63 import java.io.Writer;
64 import java.net.SocketException;
66 import java.net.URISyntaxException;
67 import java.net.UnknownHostException;
68 import java.util.ArrayList;
69 import java.util.Arrays;
70 import java.util.Collection;
71 import java.util.Hashtable;
72 import java.util.List;
73 import java.util.Vector;
74 import java.util.logging.Logger;
76 import javax.imageio.ImageIO;
77 import javax.servlet.ServletConfig;
78 import javax.servlet.ServletException;
79 import javax.servlet.ServletRequest;
80 import javax.servlet.http.HttpServlet;
81 import javax.servlet.http.HttpServletRequest;
82 import javax.servlet.http.HttpServletResponse;
85 * {@link HttpServlet} which decodes images containing barcodes. Given a URL, it will
86 * retrieve the image and decode it. It can also process image files uploaded via POST.
90 public final class DecodeServlet extends HttpServlet {
92 private static final long MAX_IMAGE_SIZE = 2000000L;
94 private static final Logger log = Logger.getLogger(DecodeServlet.class.getName());
96 static final Hashtable<DecodeHintType, Object> HINTS;
97 static final Hashtable<DecodeHintType, Object> HINTS_PURE;
100 HINTS = new Hashtable<DecodeHintType, Object>(5);
101 HINTS.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
102 Collection<BarcodeFormat> possibleFormats = new Vector<BarcodeFormat>();
103 possibleFormats.add(BarcodeFormat.UPC_A);
104 possibleFormats.add(BarcodeFormat.UPC_E);
105 possibleFormats.add(BarcodeFormat.EAN_8);
106 possibleFormats.add(BarcodeFormat.EAN_13);
107 possibleFormats.add(BarcodeFormat.CODE_39);
108 possibleFormats.add(BarcodeFormat.CODE_128);
109 possibleFormats.add(BarcodeFormat.ITF);
110 possibleFormats.add(BarcodeFormat.RSS14);
111 possibleFormats.add(BarcodeFormat.QR_CODE);
112 possibleFormats.add(BarcodeFormat.DATAMATRIX);
113 possibleFormats.add(BarcodeFormat.PDF417);
114 HINTS.put(DecodeHintType.POSSIBLE_FORMATS, possibleFormats);
115 HINTS_PURE = new Hashtable<DecodeHintType, Object>(HINTS);
116 HINTS_PURE.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
119 private HttpParams params;
120 private SchemeRegistry registry;
121 private DiskFileItemFactory diskFileItemFactory;
124 public void init(ServletConfig servletConfig) {
126 Logger logger = Logger.getLogger("com.google.zxing");
127 logger.addHandler(new ServletContextLogHandler(servletConfig.getServletContext()));
129 params = new BasicHttpParams();
130 HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
132 registry = new SchemeRegistry();
133 registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
134 registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
136 diskFileItemFactory = new DiskFileItemFactory();
138 log.info("DecodeServlet configured");
142 protected void doGet(HttpServletRequest request, HttpServletResponse response)
143 throws ServletException, IOException {
144 String imageURIString = request.getParameter("u");
145 if (imageURIString == null || imageURIString.length() == 0) {
146 response.sendRedirect("badurl.jspx");
150 if (!(imageURIString.startsWith("http://") || imageURIString.startsWith("https://"))) {
151 imageURIString = "http://" + imageURIString;
156 imageURI = new URI(imageURIString);
157 } catch (URISyntaxException urise) {
158 response.sendRedirect("badurl.jspx");
162 ClientConnectionManager connectionManager = new SingleClientConnManager(params, registry);
163 HttpClient client = new DefaultHttpClient(connectionManager, params);
165 HttpUriRequest getRequest = new HttpGet(imageURI);
166 getRequest.addHeader("Connection", "close"); // Avoids CLOSE_WAIT socket issue?
170 HttpResponse getResponse;
172 getResponse = client.execute(getRequest);
173 } catch (IllegalArgumentException iae) {
174 // Thrown if hostname is bad or null
176 response.sendRedirect("badurl.jspx");
178 } catch (SocketException se) {
179 // Thrown if hostname is bad or null
181 response.sendRedirect("badurl.jspx");
183 } catch (UnknownHostException uhe) {
185 response.sendRedirect("badurl.jspx");
189 if (getResponse.getStatusLine().getStatusCode() != HttpServletResponse.SC_OK) {
190 response.sendRedirect("badurl.jspx");
193 if (!isSizeOK(getResponse)) {
194 response.sendRedirect("badimage.jspx");
198 log.info("Decoding " + imageURI);
199 HttpEntity entity = getResponse.getEntity();
200 InputStream is = entity.getContent();
202 processStream(is, request, response);
204 entity.consumeContent();
209 connectionManager.shutdown();
215 protected void doPost(HttpServletRequest request, HttpServletResponse response)
216 throws ServletException, IOException {
218 if (!ServletFileUpload.isMultipartContent(request)) {
219 response.sendRedirect("badimage.jspx");
223 ServletFileUpload upload = new ServletFileUpload(diskFileItemFactory);
224 upload.setFileSizeMax(MAX_IMAGE_SIZE);
228 for (FileItem item : (List<FileItem>) upload.parseRequest(request)) {
229 if (!item.isFormField()) {
230 if (item.getSize() <= MAX_IMAGE_SIZE) {
231 log.info("Decoding uploaded file");
232 InputStream is = item.getInputStream();
234 processStream(is, request, response);
239 response.sendRedirect("badimage.jspx");
244 } catch (FileUploadException fue) {
245 response.sendRedirect("badimage.jspx");
250 private static void processStream(InputStream is, ServletRequest request,
251 HttpServletResponse response) throws ServletException, IOException {
253 BufferedImage image = ImageIO.read(is);
255 response.sendRedirect("badimage.jspx");
259 Reader reader = new MultiFormatReader();
260 LuminanceSource source = new BufferedImageLuminanceSource(image);
261 BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
262 Collection<Result> results = new ArrayList<Result>(1);
263 ReaderException savedException = null;
266 // Look for multiple barcodes
267 MultipleBarcodeReader multiReader = new GenericMultipleBarcodeReader(reader);
268 Result[] theResults = multiReader.decodeMultiple(bitmap, HINTS);
269 if (theResults != null) {
270 results.addAll(Arrays.asList(theResults));
272 } catch (ReaderException re) {
276 if (results.isEmpty()) {
278 // Look for pure barcode
279 Result theResult = reader.decode(bitmap, HINTS_PURE);
280 if (theResult != null) {
281 results.add(theResult);
283 } catch (ReaderException re) {
288 if (results.isEmpty()) {
290 // Look for normal barcode in photo
291 Result theResult = reader.decode(bitmap, HINTS);
292 if (theResult != null) {
293 results.add(theResult);
295 } catch (ReaderException re) {
300 if (results.isEmpty()) {
302 // Try again with other binarizer
303 BinaryBitmap hybridBitmap = new BinaryBitmap(new HybridBinarizer(source));
304 Result theResult = reader.decode(hybridBitmap, HINTS);
305 if (theResult != null) {
306 results.add(theResult);
308 } catch (ReaderException re) {
313 if (results.isEmpty()) {
314 handleException(savedException, response);
318 if (request.getParameter("full") == null) {
319 response.setContentType("text/plain");
320 response.setCharacterEncoding("UTF8");
321 Writer out = new OutputStreamWriter(response.getOutputStream(), "UTF8");
323 for (Result result : results) {
324 out.write(result.getText());
331 request.setAttribute("results", results);
332 request.getRequestDispatcher("decoderesult.jspx").forward(request, response);
336 private static void handleException(ReaderException re, HttpServletResponse response) throws IOException {
337 if (re instanceof NotFoundException) {
338 log.info("Not found: " + re);
339 response.sendRedirect("notfound.jspx");
340 } else if (re instanceof FormatException) {
341 log.info("Format problem: " + re);
342 response.sendRedirect("format.jspx");
343 } else if (re instanceof ChecksumException) {
344 log.info("Checksum problem: " + re);
345 response.sendRedirect("format.jspx");
347 log.info("Unknown problem: " + re);
348 response.sendRedirect("notfound.jspx");
352 private static boolean isSizeOK(HttpMessage getResponse) {
353 Header lengthHeader = getResponse.getLastHeader("Content-Length");
354 if (lengthHeader != null) {
355 long length = Long.parseLong(lengthHeader.getValue());
356 if (length > MAX_IMAGE_SIZE) {
364 public void destroy() {
365 log.config("DecodeServlet shutting down...");