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.DecodeHintType;
22 import com.google.zxing.LuminanceSource;
23 import com.google.zxing.MultiFormatReader;
24 import com.google.zxing.Reader;
25 import com.google.zxing.ReaderException;
26 import com.google.zxing.Result;
27 import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
28 import com.google.zxing.client.result.ParsedResult;
29 import com.google.zxing.client.result.ResultParser;
30 import com.google.zxing.common.GlobalHistogramBinarizer;
32 import org.apache.commons.fileupload.FileItem;
33 import org.apache.commons.fileupload.FileUploadException;
34 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
35 import org.apache.commons.fileupload.servlet.ServletFileUpload;
36 import org.apache.commons.lang.StringEscapeUtils;
37 import org.apache.http.Header;
38 import org.apache.http.HttpMessage;
39 import org.apache.http.HttpResponse;
40 import org.apache.http.HttpVersion;
41 import org.apache.http.client.HttpClient;
42 import org.apache.http.client.methods.HttpGet;
43 import org.apache.http.client.params.HttpClientParams;
44 import org.apache.http.conn.scheme.PlainSocketFactory;
45 import org.apache.http.conn.scheme.Scheme;
46 import org.apache.http.conn.scheme.SchemeRegistry;
47 import org.apache.http.conn.ssl.SSLSocketFactory;
48 import org.apache.http.impl.client.DefaultHttpClient;
49 import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
50 import org.apache.http.params.BasicHttpParams;
51 import org.apache.http.params.HttpParams;
52 import org.apache.http.params.HttpProtocolParams;
54 import java.awt.image.BufferedImage;
55 import java.io.IOException;
56 import java.io.InputStream;
57 import java.io.OutputStreamWriter;
58 import java.io.Writer;
59 import java.net.SocketException;
61 import java.net.URISyntaxException;
62 import java.net.UnknownHostException;
63 import java.util.Hashtable;
64 import java.util.List;
65 import java.util.Vector;
66 import java.util.logging.Logger;
68 import javax.imageio.ImageIO;
69 import javax.servlet.ServletConfig;
70 import javax.servlet.ServletException;
71 import javax.servlet.http.HttpServlet;
72 import javax.servlet.http.HttpServletRequest;
73 import javax.servlet.http.HttpServletResponse;
76 * {@link HttpServlet} which decodes images containing barcodes. Given a URL, it will
77 * retrieve the image and decode it. It can also process image files uploaded via POST.
81 public final class DecodeServlet extends HttpServlet {
83 private static final long MAX_IMAGE_SIZE = 500000L;
85 private static final Logger log = Logger.getLogger(DecodeServlet.class.getName());
87 static final Hashtable<DecodeHintType, Object> HINTS;
90 HINTS = new Hashtable<DecodeHintType, Object>(5);
91 HINTS.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
92 Vector<BarcodeFormat> possibleFormats = new Vector<BarcodeFormat>();
93 possibleFormats.add(BarcodeFormat.UPC_A);
94 possibleFormats.add(BarcodeFormat.UPC_E);
95 possibleFormats.add(BarcodeFormat.EAN_8);
96 possibleFormats.add(BarcodeFormat.EAN_13);
97 possibleFormats.add(BarcodeFormat.CODE_39);
98 possibleFormats.add(BarcodeFormat.CODE_128);
99 possibleFormats.add(BarcodeFormat.ITF);
100 possibleFormats.add(BarcodeFormat.QR_CODE);
101 possibleFormats.add(BarcodeFormat.DATAMATRIX);
102 HINTS.put(DecodeHintType.POSSIBLE_FORMATS, possibleFormats);
105 private HttpClient client;
106 private DiskFileItemFactory diskFileItemFactory;
109 public void init(ServletConfig servletConfig) {
111 Logger logger = Logger.getLogger("com.google.zxing");
112 logger.addHandler(new ServletContextLogHandler(servletConfig.getServletContext()));
114 HttpParams params = new BasicHttpParams();
115 HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
117 SchemeRegistry registry = new SchemeRegistry();
118 registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
119 registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
121 client = new DefaultHttpClient(new ThreadSafeClientConnManager(params, registry), params);
123 diskFileItemFactory = new DiskFileItemFactory();
125 log.info("DecodeServlet configured");
129 protected void doGet(HttpServletRequest request, HttpServletResponse response)
130 throws ServletException, IOException {
131 String imageURIString = request.getParameter("u");
132 if (imageURIString == null || imageURIString.length() == 0) {
133 response.sendRedirect("badurl.jspx");
137 if (!(imageURIString.startsWith("http://") || imageURIString.startsWith("https://"))) {
138 imageURIString = "http://" + imageURIString;
143 imageURI = new URI(imageURIString);
144 } catch (URISyntaxException urise) {
145 response.sendRedirect("badurl.jspx");
149 HttpGet getRequest = new HttpGet(imageURI);
150 getRequest.addHeader("Connection", "close"); // Avoids CLOSE_WAIT socket issue?
153 HttpResponse getResponse = client.execute(getRequest);
154 if (getResponse.getStatusLine().getStatusCode() != HttpServletResponse.SC_OK) {
155 response.sendRedirect("badurl.jspx");
158 if (!isSizeOK(getResponse)) {
159 response.sendRedirect("badimage.jspx");
162 log.info("Decoding " + imageURI);
163 InputStream is = getResponse.getEntity().getContent();
165 processStream(is, request, response);
169 } catch (IllegalArgumentException iae) {
170 // Thrown if hostname is bad or null
172 response.sendRedirect("badurl.jspx");
173 } catch (SocketException se) {
174 // Thrown if hostname is bad or null
176 response.sendRedirect("badurl.jspx");
177 } catch (UnknownHostException uhe) {
179 response.sendRedirect("badurl.jspx");
185 protected void doPost(HttpServletRequest request, HttpServletResponse response)
186 throws ServletException, IOException {
188 if (!ServletFileUpload.isMultipartContent(request)) {
189 response.sendRedirect("badimage.jspx");
193 ServletFileUpload upload = new ServletFileUpload(diskFileItemFactory);
194 upload.setFileSizeMax(MAX_IMAGE_SIZE);
198 for (FileItem item : (List<FileItem>) upload.parseRequest(request)) {
199 if (!item.isFormField()) {
200 if (item.getSize() <= MAX_IMAGE_SIZE) {
201 log.info("Decoding uploaded file");
202 InputStream is = item.getInputStream();
204 processStream(is, request, response);
209 response.sendRedirect("badimage.jspx");
214 } catch (FileUploadException fue) {
215 response.sendRedirect("badimage.jspx");
220 private static void processStream(InputStream is, HttpServletRequest request,
221 HttpServletResponse response) throws ServletException, IOException {
222 BufferedImage image = ImageIO.read(is);
224 response.sendRedirect("badimage.jspx");
228 Reader reader = new MultiFormatReader();
231 LuminanceSource source = new BufferedImageLuminanceSource(image);
232 BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
233 result = reader.decode(bitmap, HINTS);
234 } catch (ReaderException re) {
235 log.info("DECODE FAILED: " + re.toString());
236 response.sendRedirect("notfound.jspx");
240 if (request.getParameter("full") == null) {
241 response.setContentType("text/plain");
242 response.setCharacterEncoding("UTF8");
243 Writer out = new OutputStreamWriter(response.getOutputStream(), "UTF8");
245 out.write(result.getText());
250 request.setAttribute("result", result);
251 byte[] rawBytes = result.getRawBytes();
252 if (rawBytes != null) {
253 request.setAttribute("rawBytesString", arrayToString(rawBytes));
255 request.setAttribute("rawBytesString", "(Not applicable)");
257 String text = result.getText();
259 request.setAttribute("text", StringEscapeUtils.escapeXml(text));
261 request.setAttribute("text", "(Not applicable)");
263 ParsedResult parsedResult = ResultParser.parseResult(result);
264 request.setAttribute("parsedResult", parsedResult);
265 String displayResult = parsedResult.getDisplayResult();
266 if (displayResult != null) {
267 request.setAttribute("displayResult", StringEscapeUtils.escapeXml(displayResult));
269 request.setAttribute("displayResult", "(Not applicable)");
271 request.getRequestDispatcher("decoderesult.jspx").forward(request, response);
275 private static boolean isSizeOK(HttpMessage getResponse) {
276 Header lengthHeader = getResponse.getLastHeader("Content-Length");
277 if (lengthHeader != null) {
278 long length = Long.parseLong(lengthHeader.getValue());
279 if (length > MAX_IMAGE_SIZE) {
286 private static String arrayToString(byte[] bytes) {
287 int length = bytes.length;
288 StringBuilder result = new StringBuilder(length << 2);
291 int max = Math.min(i + 8, length);
292 for (int j = i; j < max; j++) {
293 int value = bytes[j] & 0xFF;
294 result.append(Integer.toHexString(value / 16));
295 result.append(Integer.toHexString(value % 16));
301 for (int j = i - 8; j < length; j++) {
302 result.append(Integer.toHexString(bytes[j] & 0xFF));
305 return result.toString();
309 public void destroy() {
310 log.config("DecodeServlet shutting down...");