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) {} }
private Image copy() { Image imgTarget = new Image(); imgTarget.setImageName(imageName); imgTarget.setFileURL(fileURL); imgTarget.setBimg(bimg); imgTarget.setGroup(group); imgTarget.setIsAbsolute(imageIsAbsolute); imgTarget.setIsText(imageIsText); imgTarget.setIsBundled(imageIsBundled); imgTarget.setLastSeen(getLastSeen(), getLastSeenScore()); imgTarget.setHasIOException(hasIOException()); if (isPattern()) { imgTarget.setSimilarity(similarity); imgTarget.setOffset(offset); imgTarget.setWaitAfter(waitAfter); imgTarget.setIsPattern(true); } return imgTarget; }
/** * create a new Image with Pattern aspects from an existing Pattern * @param p a Pattern * @return the new Image */ public static Image create(Pattern p) { Image img = p.getImage().copy(); img.setIsPattern(true); img.setSimilarity(p.getSimilar()); img.setOffset(p.getTargetOffset()); img.setWaitAfter(p.getTimeAfter()); return img; }
if (fName.startsWith("\t") && fName.endsWith("\t")) { fName = fName.substring(1, fName.length() - 1); img = new Image(); img.setIsText(true); } else { fName = FileManager.slashify(fName, false); String fileName = Settings.getValidImageFilename(fName); if (fileName.isEmpty()) { log(-1, "not a valid image type: " + fName); fileName = fName; img = new Image(fileName, fURL, silent); img.setIsAbsolute(imgFile.isAbsolute()); } else { if (img.bimg != null) { log(3, "reused: %s (%s)", img.imageName, img.fileURL); } else { if (Settings.getImageCache() > 0) { img.load();
private static Image createImageValidate(Image img, boolean verbose) { if (img == null) { log(-1, "Image not valid, creating empty Image"); return new Image("", null); } if (!img.isValid()) { if (Settings.OcrTextSearch) { img.setIsText(true); if (Settings.isValidImageFilename(img.getName())) { img.setIsText(false); } } else { if (verbose) { log(-1, "Image not valid, but TextSearch is switched off!"); } } } return img; }
/** * 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; } }
public Image getRotatedCopy(Image i, float angleDeg) { Image rotated = i.copy(); rotated.setRotation(angleDeg); rotated.rotate(); return rotated; }
/** * Finder constructor (finding within an image within the given region). * <br>internally used with a screen snapshot * * @param imageFilename a string (name, path, url) * @param region search Region within image - topleft = (0,0) * @throws java.io.IOException if imagefile not found */ public Finder(String imageFilename, Region region) throws IOException { Image img = Image.create(imageFilename); if (img.isValid()) { _findInput.setSource(Image.convertBufferedImageToMat(img.get())); _region = region; screenFinder = false; } else { log(-1, "imagefile not found:\n%s"); valid = false; } }
/** * 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; } }
/** * to get old style OpenCV Mat for FindInput * * @return SWIG interfaced OpenCV Mat * @deprecated */ @Deprecated protected org.sikuli.natives.Mat getMatNative() { return convertBufferedImageToMat(get()); }
/** * create a new image from the given url <br> * file ending .png is added if missing <br> * filename: ...url-path.../name[.png] is loaded from the url and and cached * <br> * already loaded image with same url is reused (reference) and taken from * cache * * @param url image file URL * @return the image */ public static Image create(URL url) { Image img = get(url); if (img == null) { img = new Image(url); } return createImageValidate(img, true); }
/** * create a sub image from this image * * @param x pixel column * @param y pixel row * @param w width * @param h height * @return the new image */ public Image getSub(int x, int y, int w, int h) { BufferedImage bi = createBufferedImage(w, h); Graphics2D g = bi.createGraphics(); g.drawImage(get().getSubimage(x, y, w, h), 0, 0, null); g.dispose(); return new Image(bi); }
@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; } }
/** * get the filename of the image used for searching * @return filename */ public String getImageFilename() { return image.getFilename(); }
/** * A Pattern from a BufferedImage * * @param bimg BufferedImage */ public Pattern(BufferedImage bimg) { image = new Image(bimg); }
/** * Constructor for special use from a BufferedImage * * @param bimg BufferedImage */ public Finder(BufferedImage bimg) { _findInput.setSource(Image.convertBufferedImageToMat(bimg)); }