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.color.CMMException;
60 import java.awt.image.BufferedImage;
61 import java.io.IOException;
62 import java.io.InputStream;
63 import java.io.OutputStreamWriter;
64 import java.io.Writer;
66 import java.net.URISyntaxException;
67 import java.util.ArrayList;
68 import java.util.Arrays;
69 import java.util.Collection;
70 import java.util.Hashtable;
71 import java.util.List;
72 import java.util.Vector;
73 import java.util.logging.Logger;
75 import javax.imageio.ImageIO;
76 import javax.servlet.ServletConfig;
77 import javax.servlet.ServletException;
78 import javax.servlet.ServletRequest;
79 import javax.servlet.http.HttpServlet;
80 import javax.servlet.http.HttpServletRequest;
81 import javax.servlet.http.HttpServletResponse;
84 * {@link HttpServlet} which decodes images containing barcodes. Given a URL, it will
85 * retrieve the image and decode it. It can also process image files uploaded via POST.
89 public final class DecodeServlet extends HttpServlet {
91 // No real reason to let people upload more than a 2MB image
92 private static final long MAX_IMAGE_SIZE = 2000000L;
93 // No real reason to deal with more than maybe 2 megapixels
94 private static final int MAX_PIXELS = 1 << 21;
96 private static final Logger log = Logger.getLogger(DecodeServlet.class.getName());
98 static final Hashtable<DecodeHintType, Object> HINTS;
99 static final Hashtable<DecodeHintType, Object> HINTS_PURE;
102 HINTS = new Hashtable<DecodeHintType, Object>(5);
103 HINTS.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
104 Collection<BarcodeFormat> possibleFormats = new Vector<BarcodeFormat>(17);
105 possibleFormats.add(BarcodeFormat.UPC_A);
106 possibleFormats.add(BarcodeFormat.UPC_E);
107 possibleFormats.add(BarcodeFormat.EAN_8);
108 possibleFormats.add(BarcodeFormat.EAN_13);
109 possibleFormats.add(BarcodeFormat.CODE_39);
110 possibleFormats.add(BarcodeFormat.CODE_93);
111 possibleFormats.add(BarcodeFormat.CODE_128);
112 //possibleFormats.add(BarcodeFormat.CODABAR);
113 possibleFormats.add(BarcodeFormat.ITF);
114 possibleFormats.add(BarcodeFormat.RSS14);
115 possibleFormats.add(BarcodeFormat.QR_CODE);
116 possibleFormats.add(BarcodeFormat.DATA_MATRIX);
117 possibleFormats.add(BarcodeFormat.PDF417);
118 HINTS.put(DecodeHintType.POSSIBLE_FORMATS, possibleFormats);
119 HINTS_PURE = new Hashtable<DecodeHintType, Object>(HINTS);
120 HINTS_PURE.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
123 private HttpParams params;
124 private SchemeRegistry registry;
125 private DiskFileItemFactory diskFileItemFactory;
128 public void init(ServletConfig servletConfig) {
130 Logger logger = Logger.getLogger("com.google.zxing");
131 logger.addHandler(new ServletContextLogHandler(servletConfig.getServletContext()));
133 params = new BasicHttpParams();
134 HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
136 registry = new SchemeRegistry();
137 registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
138 registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
140 diskFileItemFactory = new DiskFileItemFactory();
142 log.info("DecodeServlet configured");
146 protected void doGet(HttpServletRequest request, HttpServletResponse response)
147 throws ServletException, IOException {
148 String imageURIString = request.getParameter("u");
149 if (imageURIString == null || imageURIString.length() == 0) {
150 log.fine("URI was empty");
151 response.sendRedirect("badurl.jspx");
155 imageURIString = imageURIString.trim();
157 if (!(imageURIString.startsWith("http://") || imageURIString.startsWith("https://"))) {
158 imageURIString = "http://" + imageURIString;
163 imageURI = new URI(imageURIString);
164 } catch (URISyntaxException urise) {
165 log.fine("URI was not valid: " + imageURIString);
166 response.sendRedirect("badurl.jspx");
170 ClientConnectionManager connectionManager = new SingleClientConnManager(params, registry);
171 HttpClient client = new DefaultHttpClient(connectionManager, params);
173 HttpUriRequest getRequest = new HttpGet(imageURI);
174 getRequest.addHeader("Connection", "close"); // Avoids CLOSE_WAIT socket issue?
178 HttpResponse getResponse;
180 getResponse = client.execute(getRequest);
181 } catch (IllegalArgumentException iae) {
182 // Thrown if hostname is bad or null
183 log.fine(iae.toString());
185 response.sendRedirect("badurl.jspx");
187 } catch (IOException ioe) {
188 // Encompasses lots of stuff, including
189 // java.net.SocketException, java.net.UnknownHostException,
190 // javax.net.ssl.SSLPeerUnverifiedException,
191 // org.apache.http.NoHttpResponseException,
192 // org.apache.http.client.ClientProtocolException,
193 log.fine(ioe.toString());
195 response.sendRedirect("badurl.jspx");
199 if (getResponse.getStatusLine().getStatusCode() != HttpServletResponse.SC_OK) {
200 log.fine("Unsuccessful return code: " + getResponse.getStatusLine().getStatusCode());
201 response.sendRedirect("badurl.jspx");
204 if (!isSizeOK(getResponse)) {
205 log.fine("Too large");
206 response.sendRedirect("badimage.jspx");
210 log.info("Decoding " + imageURI);
211 HttpEntity entity = getResponse.getEntity();
212 InputStream is = entity.getContent();
214 processStream(is, request, response);
216 entity.consumeContent();
221 connectionManager.shutdown();
227 protected void doPost(HttpServletRequest request, HttpServletResponse response)
228 throws ServletException, IOException {
230 if (!ServletFileUpload.isMultipartContent(request)) {
231 log.fine("File upload was not multipart");
232 response.sendRedirect("badimage.jspx");
236 ServletFileUpload upload = new ServletFileUpload(diskFileItemFactory);
237 upload.setFileSizeMax(MAX_IMAGE_SIZE);
241 for (FileItem item : (List<FileItem>) upload.parseRequest(request)) {
242 if (!item.isFormField()) {
243 if (item.getSize() <= MAX_IMAGE_SIZE) {
244 log.info("Decoding uploaded file");
245 InputStream is = item.getInputStream();
247 processStream(is, request, response);
252 log.fine("Too large");
253 response.sendRedirect("badimage.jspx");
258 } catch (FileUploadException fue) {
259 log.fine(fue.toString());
260 response.sendRedirect("badimage.jspx");
265 private static void processStream(InputStream is, ServletRequest request,
266 HttpServletResponse response) throws ServletException, IOException {
270 image = ImageIO.read(is);
271 } catch (IOException ioe) {
272 log.fine(ioe.toString());
273 // Includes javax.imageio.IIOException
274 response.sendRedirect("badimage.jspx");
276 } catch (CMMException cmme) {
277 log.fine(cmme.toString());
278 // Have seen this in logs
279 response.sendRedirect("badimage.jspx");
281 } catch (IllegalArgumentException iae) {
282 log.fine(iae.toString());
283 // Have seen this in logs for some JPEGs
284 response.sendRedirect("badimage.jspx");
288 response.sendRedirect("badimage.jspx");
291 if (image.getHeight() <= 1 || image.getWidth() <= 1 ||
292 image.getHeight() * image.getWidth() > MAX_PIXELS) {
293 log.fine("Dimensions too large: " + image.getWidth() + 'x' + image.getHeight());
294 response.sendRedirect("badimage.jspx");
298 Reader reader = new MultiFormatReader();
299 LuminanceSource source = new BufferedImageLuminanceSource(image);
300 BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
301 Collection<Result> results = new ArrayList<Result>(1);
302 ReaderException savedException = null;
305 // Look for multiple barcodes
306 MultipleBarcodeReader multiReader = new GenericMultipleBarcodeReader(reader);
307 Result[] theResults = multiReader.decodeMultiple(bitmap, HINTS);
308 if (theResults != null) {
309 results.addAll(Arrays.asList(theResults));
311 } catch (ReaderException re) {
315 if (results.isEmpty()) {
317 // Look for pure barcode
318 Result theResult = reader.decode(bitmap, HINTS_PURE);
319 if (theResult != null) {
320 results.add(theResult);
322 } catch (ReaderException re) {
327 if (results.isEmpty()) {
329 // Look for normal barcode in photo
330 Result theResult = reader.decode(bitmap, HINTS);
331 if (theResult != null) {
332 results.add(theResult);
334 } catch (ReaderException re) {
339 if (results.isEmpty()) {
341 // Try again with other binarizer
342 BinaryBitmap hybridBitmap = new BinaryBitmap(new HybridBinarizer(source));
343 Result theResult = reader.decode(hybridBitmap, HINTS);
344 if (theResult != null) {
345 results.add(theResult);
347 } catch (ReaderException re) {
352 if (results.isEmpty()) {
353 handleException(savedException, response);
357 if (request.getParameter("full") == null) {
358 response.setContentType("text/plain");
359 response.setCharacterEncoding("UTF8");
360 Writer out = new OutputStreamWriter(response.getOutputStream(), "UTF8");
362 for (Result result : results) {
363 out.write(result.getText());
370 request.setAttribute("results", results);
371 request.getRequestDispatcher("decoderesult.jspx").forward(request, response);
375 private static void handleException(ReaderException re, HttpServletResponse response) throws IOException {
376 if (re instanceof NotFoundException) {
377 log.info("Not found: " + re);
378 response.sendRedirect("notfound.jspx");
379 } else if (re instanceof FormatException) {
380 log.info("Format problem: " + re);
381 response.sendRedirect("format.jspx");
382 } else if (re instanceof ChecksumException) {
383 log.info("Checksum problem: " + re);
384 response.sendRedirect("format.jspx");
386 log.info("Unknown problem: " + re);
387 response.sendRedirect("notfound.jspx");
391 private static boolean isSizeOK(HttpMessage getResponse) {
392 Header lengthHeader = getResponse.getLastHeader("Content-Length");
393 if (lengthHeader != null) {
394 long length = Long.parseLong(lengthHeader.getValue());
395 if (length > MAX_IMAGE_SIZE) {
403 public void destroy() {
404 log.config("DecodeServlet shutting down...");