Support reading multiple barcodes in one image.
[zxing.git] / zxingorg / src / com / google / zxing / web / DecodeServlet.java
1 /*
2  * Copyright 2008 ZXing authors
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.google.zxing.web;
18
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;
33
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;
58
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;
65 import java.net.URI;
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;
75
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;
83
84 /**
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.
87  *
88  * @author Sean Owen
89  */
90 public final class DecodeServlet extends HttpServlet {
91
92   private static final long MAX_IMAGE_SIZE = 2000000L;
93
94   private static final Logger log = Logger.getLogger(DecodeServlet.class.getName());
95
96   static final Hashtable<DecodeHintType, Object> HINTS;
97   static final Hashtable<DecodeHintType, Object> HINTS_PURE;
98
99   static {
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);
117   }
118
119   private HttpParams params;
120   private SchemeRegistry registry;
121   private DiskFileItemFactory diskFileItemFactory;
122
123   @Override
124   public void init(ServletConfig servletConfig) {
125
126     Logger logger = Logger.getLogger("com.google.zxing");
127     logger.addHandler(new ServletContextLogHandler(servletConfig.getServletContext()));
128
129     params = new BasicHttpParams();
130     HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
131
132     registry = new SchemeRegistry();
133     registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
134     registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
135
136     diskFileItemFactory = new DiskFileItemFactory();
137
138     log.info("DecodeServlet configured");
139   }
140
141   @Override
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");
147       return;
148     }
149
150     if (!(imageURIString.startsWith("http://") || imageURIString.startsWith("https://"))) {
151       imageURIString = "http://" + imageURIString;
152     }
153
154     URI imageURI;
155     try {
156       imageURI = new URI(imageURIString);
157     } catch (URISyntaxException urise) {
158       response.sendRedirect("badurl.jspx");
159       return;
160     }
161
162     ClientConnectionManager connectionManager = new SingleClientConnManager(params, registry);
163     HttpClient client = new DefaultHttpClient(connectionManager, params);
164
165     HttpUriRequest getRequest = new HttpGet(imageURI);
166     getRequest.addHeader("Connection", "close"); // Avoids CLOSE_WAIT socket issue?
167
168     try {
169
170       HttpResponse getResponse;
171       try {
172         getResponse = client.execute(getRequest);
173       } catch (IllegalArgumentException iae) {
174         // Thrown if hostname is bad or null
175         getRequest.abort();
176         response.sendRedirect("badurl.jspx");
177         return;
178       } catch (SocketException se) {
179         // Thrown if hostname is bad or null
180         getRequest.abort();
181         response.sendRedirect("badurl.jspx");
182         return;
183       } catch (UnknownHostException uhe) {
184         getRequest.abort();
185         response.sendRedirect("badurl.jspx");
186         return;
187       }
188
189       if (getResponse.getStatusLine().getStatusCode() != HttpServletResponse.SC_OK) {
190         response.sendRedirect("badurl.jspx");
191         return;
192       }
193       if (!isSizeOK(getResponse)) {
194         response.sendRedirect("badimage.jspx");
195         return;
196       }
197
198       log.info("Decoding " + imageURI);
199       HttpEntity entity = getResponse.getEntity();
200       InputStream is = entity.getContent();
201       try {
202         processStream(is, request, response);
203       } finally {
204         entity.consumeContent();
205         is.close();
206       }
207
208     } finally {
209       connectionManager.shutdown();
210     }
211
212   }
213
214   @Override
215   protected void doPost(HttpServletRequest request, HttpServletResponse response)
216           throws ServletException, IOException {
217
218     if (!ServletFileUpload.isMultipartContent(request)) {
219       response.sendRedirect("badimage.jspx");
220       return;
221     }
222
223     ServletFileUpload upload = new ServletFileUpload(diskFileItemFactory);
224     upload.setFileSizeMax(MAX_IMAGE_SIZE);
225
226     // Parse the request
227     try {
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();
233             try {
234               processStream(is, request, response);
235             } finally {
236               is.close();
237             }
238           } else {
239             response.sendRedirect("badimage.jspx");
240           }
241           break;
242         }
243       }
244     } catch (FileUploadException fue) {
245       response.sendRedirect("badimage.jspx");
246     }
247
248   }
249
250   private static void processStream(InputStream is, ServletRequest request,
251       HttpServletResponse response) throws ServletException, IOException {
252
253     BufferedImage image = ImageIO.read(is);
254     if (image == null) {
255       response.sendRedirect("badimage.jspx");
256       return;
257     }
258
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;
264
265     try {
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));
271       }
272     } catch (ReaderException re) {
273       savedException = re;
274     }
275
276     if (results.isEmpty()) {
277       try {
278         // Look for pure barcode
279         Result theResult = reader.decode(bitmap, HINTS_PURE);
280         if (theResult != null) {
281           results.add(theResult);
282         }
283       } catch (ReaderException re) {
284         savedException = re;
285       }
286     }
287
288     if (results.isEmpty()) {
289       try {
290         // Look for normal barcode in photo
291         Result theResult = reader.decode(bitmap, HINTS);
292         if (theResult != null) {
293           results.add(theResult);
294         }
295       } catch (ReaderException re) {
296         savedException = re;
297       }
298     }
299
300     if (results.isEmpty()) {
301       try {
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);
307         }
308       } catch (ReaderException re) {
309         savedException = re;
310       }
311     }
312
313     if (results.isEmpty()) {
314       handleException(savedException, response);
315       return;
316     }
317
318     if (request.getParameter("full") == null) {
319       response.setContentType("text/plain");
320       response.setCharacterEncoding("UTF8");
321       Writer out = new OutputStreamWriter(response.getOutputStream(), "UTF8");
322       try {
323         for (Result result : results) {
324           out.write(result.getText());
325           out.write('\n');
326         }
327       } finally {
328         out.close();
329       }
330     } else {
331       request.setAttribute("results", results);
332       request.getRequestDispatcher("decoderesult.jspx").forward(request, response);
333     }
334   }
335
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");
346     } else {
347       log.info("Unknown problem: " + re);
348       response.sendRedirect("notfound.jspx");
349     }
350   }
351
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) {
357         return false;
358       }
359     }
360     return true;
361   }
362
363   @Override
364   public void destroy() {
365     log.config("DecodeServlet shutting down...");
366   }
367
368 }