Long imageId = Long.valueOf(request.getPathInfo().substring(1)); // 123 Image image = imageDAO.find(imageId); // Get Image from DB. // Image class is just a Javabean with the following properties: // private String filename; // private Long length; // private InputStream content; response.setHeader("Content-Type", getServletContext().getMimeType(image.getFilename())); response.setHeader("Content-Length", String.valueOf(image.getLength())); response.setHeader("Content-Disposition", "inline; filename=\"" + image.getFilename() + "\""); BufferedInputStream input = null; BufferedOutputStream output = null; try { input = new BufferedInputStream(image.getContent()); output = new BufferedOutputStream(response.getOutputStream()); byte[] buffer = new byte[8192]; for (int length = 0; (length = input.read(buffer)) > 0) { output.write(buffer, 0, length); } } finally { if (output != null) try { output.close(); } catch (IOException logOrIgnore) {} if (input != null) try { input.close(); } catch (IOException logOrIgnore) {} }
/** * get the filename of the image used for searching * @return filename */ public String getImageFilename() { return image.getFilename(); }
/** * the current image's absolute filepath * <br>will return null, if image is in jar or in web * <br>use getFileURL in this case * * @return might be null */ public String getFilename() { return image.getFilename(); }
@WebServlet("/image/*") public class ImageServlet extends HttpServlet { @EJB private SomeImageService someImageService; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String path = request.getPathInfo().substring(1); Image image = someImageService.findSomehowBy(path); String filename = image.getFilename(); byte[] content = image.getContent(); response.setHeader("Content-Type", getServletContext().getMimetype(filename)); response.setHeader("Content-Length", String.valueOf(content.length)); response.setHeader("Content-Disposition", "inline; filename=\"" + filename + "\""); response.getOutputStream().write(content); } }
private String setTargetSmartly(FindInput fin, String target) { if (isImageFile(target)) { //assume it's a file first String filename = Image.create(target).getFilename(); if (filename != null) { fin.setTarget(TARGET_TYPE.IMAGE, filename); return filename; } else { if (!repeating) { Debug.error(target + " looks like a file, but not on disk. Assume it's text."); } } } if (!Settings.OcrTextSearch) { Debug.error("Region.find(text): text search is currently switched off"); return target + "???"; } else { fin.setTarget(TARGET_TYPE.TEXT, target); if (TextRecognizer.getInstance() == null) { Debug.error("Region.find(text): text search is now switched off"); return target + "???"; } return target; } }
/** * do a find op with the given pattern in the Finder's image * (hasNext() and next() will reveal possible match results) * @param img Image * @return null. if find setup not possible */ public String find(Image img) { if (!valid) { log(-1, "not valid"); return null; } if (img.isValid()) { _findInput.setTarget(img.getMatNative()); _findInput.setSimilarity(Settings.MinSimilarity); _results = Vision.find(_findInput); _cur_result_i = 0; return img.getFilename(); } else if (img.isUseable()) { return find(new Pattern(img)); } else { return null; } }
/** * do a findAll op with the given image in the Finder's image * (hasNext() and next() will reveal possible match results) * @param img Image * @return null. if find setup not possible */ public String findAll(Image img) { if (!valid) { log(-1, "not valid"); return null; } if (img.isValid()) { _findInput.setTarget(img.getMatNative()); _findInput.setSimilarity(Settings.MinSimilarity); _findInput.setFindAll(true); Debug timing = Debug.startTimer("Finder.findAll"); _results = Vision.find(_findInput); _cur_result_i = 0; timing.end(); return img.getFilename(); } else { return null; } }