List of usage examples for java.awt Image flush
public void flush()
From source file:SwingResourceManager.java
/** * Clear cached images in specified section * @param section the section do clear/* www . j a v a2 s . co m*/ */ public static void clearImages(String section) { for (Iterator<String> I = m_ClassImageMap.keySet().iterator(); I.hasNext();) { String key = I.next(); if (!key.startsWith(section + '|')) continue; Image image = m_ClassImageMap.get(key); image.flush(); I.remove(); } }
From source file:com.funambol.foundation.util.MediaUtils.java
/** * Creates the thumbnail./* ww w . j a v a2 s.c o m*/ * * @param imageFile the image file * @param thumbFile the empty thumbnail file * @param thumbX the width of the thumbnail * @param thumbY the height of the thumbnail * @param imageName the image file name with extension * @param tolerance the percentage of tolerance before creating a thumbnail * @return true is the thumbnail has been created, false otherwise * @throws IOException if an error occurs */ private static boolean createThumbnail(File imageFile, File thumbFile, int thumbX, int thumbY, String imageName, double tolerance) throws IOException { FileInputStream fileis = null; ImageInputStream imageis = null; Iterator readers = null; try { readers = ImageIO.getImageReadersByFormatName(imageName.substring(imageName.lastIndexOf('.') + 1)); if (readers == null || (!readers.hasNext())) { throw new IOException("File not supported"); } ImageReader reader = (ImageReader) readers.next(); fileis = new FileInputStream(imageFile); imageis = ImageIO.createImageInputStream(fileis); reader.setInput(imageis, true); // Determines thumbnail height, width and quality int thumbWidth = thumbX; int thumbHeight = thumbY; double thumbRatio = (double) thumbWidth / (double) thumbHeight; int imageWidth = reader.getWidth(0); int imageHeight = reader.getHeight(0); // // Don't create the thumbnail if the original file is smaller // than required size increased by % tolerance // if (imageWidth <= (thumbWidth * (1 + tolerance / 100)) && imageHeight <= (thumbHeight * (1 + tolerance / 100))) { return false; } double imageRatio = (double) imageWidth / (double) imageHeight; if (thumbRatio < imageRatio) { thumbHeight = (int) (thumbWidth / imageRatio); } else { thumbWidth = (int) (thumbHeight * imageRatio); } ImageReadParam param = reader.getDefaultReadParam(); param.setSourceSubsampling(3, 3, 0, 0); BufferedImage bi = reader.read(0, param); Image thumb = bi.getScaledInstance(thumbWidth, thumbHeight, Image.SCALE_SMOOTH); BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = thumbImage.createGraphics(); graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.drawImage(thumb, 0, 0, thumbWidth, thumbHeight, null); FileOutputStream fileOutputStream = new FileOutputStream(thumbFile); ImageIO.write(thumbImage, "jpg", fileOutputStream); thumb.flush(); thumbImage.flush(); fileOutputStream.flush(); fileOutputStream.close(); graphics2D.dispose(); } finally { if (fileis != null) { fileis.close(); } if (imageis != null) { imageis.close(); } } return true; }
From source file:com.jaeksoft.searchlib.ocr.OcrManager.java
public void ocerizeImage(Image image, File outputFile, LanguageEnum lang, boolean hocr) throws InterruptedException, IOException, SearchLibException { File imageFile = null;//from w ww .j av a 2 s. c o m try { RenderedImage renderedImage = ImageUtils.toBufferedImage(image); imageFile = File.createTempFile("ossocrimg", '.' + OCR_IMAGE_FORMAT); ImageIO.write(renderedImage, OCR_IMAGE_FORMAT, imageFile); image.flush(); if (imageFile.length() == 0) throw new SearchLibException("Empty image " + imageFile.getAbsolutePath()); ocerize(imageFile, outputFile, lang, hocr); } finally { Logging.debug(imageFile); if (imageFile != null) FileUtils.deleteQuietly(imageFile); } }
From source file:net.sf.firemox.tools.MToolKit.java
/** * Return the loaded picture from a local place. * /*w w w . java 2s . c o m*/ * @param localFile * the local file name. * @return the local picture. * @throws InterruptedException */ public static Image getLocalPicture(String localFile) throws InterruptedException { final Image result = Toolkit.getDefaultToolkit().getImage(getFile(localFile, true).getAbsolutePath()); if (result == null) { throw new InterruptedException("Picture " + localFile + " has not been found"); } final MediaTracker tracker = new MediaTracker(MagicUIComponents.magicForm); tracker.addImage(result, 0); tracker.waitForAll(); if (tracker.isErrorAny()) { tracker.removeImage(result, 0); tracker.waitForAll(); result.flush(); throw new InterruptedException("Malformed picture " + localFile); } return result; }
From source file:J3dSwingFrame.java
/** * Set the texture on our goemetry/* ww w . java 2s . c om*/ * <P> * Always specified as a URL so that we may fetch it from anywhere. * * @param url * The url to the image. */ public void setTexture(URL url) { Toolkit tk = Toolkit.getDefaultToolkit(); Image src_img = tk.createImage(url); BufferedImage buf_img = null; if (!(src_img instanceof BufferedImage)) { // create a component anonymous inner class to give us the image // observer we need to get the width and height of the source image. Component obs = new Component() { }; int width = src_img.getWidth(obs); int height = src_img.getHeight(obs); // construct the buffered image from the source data. buf_img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics g = buf_img.getGraphics(); g.drawImage(src_img, 0, 0, null); g.dispose(); } else buf_img = (BufferedImage) src_img; src_img.flush(); ImageComponent img_comp = new ImageComponent2D(ImageComponent.FORMAT_RGB, buf_img); texture = new Texture2D(Texture.BASE_LEVEL, Texture.RGB, img_comp.getWidth(), img_comp.getHeight()); appearance.setTexture(texture); buf_img.flush(); }
From source file:com.krawler.esp.handlers.genericFileUpload.java
public final void imgResizeCompany(String imagePath, int Width, int Height, String fName, boolean ori) throws IOException { try {/*www . jav a2 s . c o m*/ // Get a path to the image to resize. // ImageIcon is a kluge to make sure the image is fully // loaded before we proceed. Image sourceImage = new ImageIcon(Toolkit.getDefaultToolkit().getImage(imagePath)).getImage(); int imageWidth = sourceImage.getWidth(null); int imageHeight = sourceImage.getHeight(null); if (ori) { Width = imageWidth; Height = imageHeight; } else { Width = imageWidth < Width ? imageWidth : Width; Height = imageHeight < Height ? imageHeight : Height; float imageRatio = ((float) imageWidth / (float) imageHeight); float framemageratio = ((float) Width / (float) Height); if (imageRatio > framemageratio) { float value = Width / imageRatio; Height = (int) value; } else { float value = Height * imageRatio; Width = (int) value; } } BufferedImage resizedImage = this.scaleCompanyImage(sourceImage, Width, Height); ImageIO.write(resizedImage, "PNG", new File(fName + ".png")); sourceImage.flush(); } catch (Exception e) { this.ErrorMsg = "Problem occured while uploading logo"; logger.warn(e.getMessage(), e); } }
From source file:com.jcraft.weirdx.XPixmap.java
static void reqPutImage(Client c) throws IOException { int foo, n;/*from w w w . ja va 2 s. co m*/ InputOutput io = c.client; byte format; short width, height, dstx, dsty; byte depth; byte lpad; XPixmap pixmap = null; format = (byte) c.data; n = c.length; foo = io.readInt(); XDrawable d = c.lookupDrawable(foo); if (d == null) { c.errorValue = foo; c.errorReason = 9; // BadDrawable; } foo = io.readInt(); GC gc = c.lookupGC(foo); if (gc == null && c.errorReason == 0) { c.errorValue = foo; c.errorReason = 13; // GC } width = (short) io.readShort(); height = (short) io.readShort(); dstx = (short) io.readShort(); dsty = (short) io.readShort(); lpad = (byte) io.readByte(); depth = (byte) io.readByte(); io.readPad(2); c.length -= 6; n -= 6; if (c.errorReason != 0) { return; } if (dsty < 0) { // height+=dsty; dsty = 0; } if (dstx < 0) { // width+=dstx; dstx = 0; } int ddstx = dstx; int ddsty = dsty; synchronized (XPixmap.class) { if (d instanceof XPixmap) { pixmap = (XPixmap) d; if (pixmap.imageDirty) { pixmap.image2data(); } } else { if (!((XWindow) d).ddxwindow.isVisible()) { io.readPad(n * 4); return; } pixmap = null; XPixmap[] pixmaps = ((XWindow) d).screen.pixmaps; for (int i = 0; i < pixmaps.length; i++) { if (pixmaps[i].depth == d.depth) { pixmap = pixmaps[i]; break; } } if (pixmap == null) { } ((Resizable) pixmap).setSize(width, height); ((Resizable) pixmap).setColormap(d.getColormap()); pixmap.lpad = lpad; dstx = 0; dsty = 0; } byte[] data = null; data = pixmap.getData(); int ww = 0; int j = 0; int i = 0; j = dsty; if (depth == 1 && (pixmap.depth == 1 || pixmap.depth == 8)) { int www = 0; if (d instanceof XWindow) { www = ((Resizable) pixmap).getRealWidth(); } else { www = pixmap.width; } if (WeirdX.imageByteOrder == 1) { Pixmap1.putData(c, data, www, dstx, dsty, width, lpad); } else { Pixmap1.putData_LSB(c, data, www, dstx, dsty, width, lpad); } if (d.depth != 1) { byte f = (byte) gc.fgPixel; byte b = (byte) gc.bgPixel; for (i = dsty; i < height + dsty; i++) { for (j = dstx; j < width + dstx; j++) { if (data[i * www + j] == 0) { data[i * www + j] = b; } else { data[i * www + j] = f; } } } } } else if (depth == 8) { if (format == 2) { // ZPixmap int restw = width; int restww = width; int resth = height; if (d.width < dstx + width) { restw = (short) (d.width - dstx); } if (d.height < dsty + height) { resth = (short) (d.height - dsty); } int www = 0; if (d instanceof XWindow) { www = ((Resizable) pixmap).getRealWidth(); } else { www = pixmap.width; } j *= www; if (width < c.bbuffer.length) { int paddedwidthi = ((width + 3) / 4); int paddedwidth = paddedwidthi * 4; int padding = paddedwidth - restw; while (n != 0) { if (resth > 0) { io.readByte(data, j + dstx, restw); if (padding != 0) { io.readPad(padding); } } else { io.readPad(paddedwidth); } n -= paddedwidthi; j += www; resth--; } } else { while (n != 0) { restww = restw; ww = width; i = dstx; while (0 < ww) { io.readByte(c.bbuffer, 0, 4); n--; if (4 <= ww) { if (resth > 0) { if (restww >= 4) { System.arraycopy(c.bbuffer, 0, data, j + i, 4); restww -= 4; } else if (restww > 0) { System.arraycopy(c.bbuffer, 0, data, j + i, restww); restww = 0; } } i += 4; ww -= 4; } else { if (resth > 0) { if (restww >= ww) { System.arraycopy(c.bbuffer, 0, data, j + i, ww); restww -= ww; } else if (restww > 0) { System.arraycopy(c.bbuffer, 0, data, j + i, restww); restww = 0; } } i += ww; break; } } j += www; resth--; } } } else { int www = 0; if (d instanceof XWindow) { www = ((Resizable) pixmap).getRealWidth(); } else { www = pixmap.width; } n *= 4; while (n != 0) { ww = width; i = dstx; while (4 < ww) { foo = io.readInt(); n -= 4; data[j * www + i] = (byte) (foo & 0xff); i++; foo = foo >> 8; data[j * www + i] = (byte) (foo & 0xff); i++; foo = foo >> 8; data[j * www + i] = (byte) (foo & 0xff); i++; foo = foo >> 8; data[j * www + i] = (byte) (foo & 0xff); i++; ww -= 4; } if (ww != 0) { foo = io.readInt(); n -= 4; while (0 < ww) { data[j * www + i] = (byte) (foo & 0xff); i++; foo = foo >> 8; ww--; } } j++; } } } else if (pixmap.depth == 16) { ((Pixmap16) pixmap).putImage(c, gc, dstx, dsty, width, height, lpad, format, depth); } else { io.readPad(n * 4); } if (d instanceof XWindow) { Graphics g = ((XWindow) d).getGraphics(); pixmap.mis.newPixels(dstx, dsty, width, height); Image dataImg = Toolkit.getDefaultToolkit().createImage(pixmap.mis); Image ii = dataImg; java.awt.Shape tmp = g.getClip(); g.clipRect(ddstx, ddsty, width, height); g.drawImage(ii, ddstx, ddsty, Screen.screen[0].root.ddxwindow); if (tmp == null) { g.setClip(0, 0, d.width, d.height); } else { g.setClip(tmp); } ((XWindow) d).draw(ddstx, ddsty, width, height); dataImg.flush(); } else { if (pixmap.time < pixmap.colormap.icmtime) { pixmap.mkMIS(); } pixmap.mis.newPixels(dstx, dsty, width, height); Image dataImg = Toolkit.getDefaultToolkit().createImage(pixmap.mis); if (dstx != 0 || dsty != 0 || width != d.width || height != d.height) { java.awt.Shape tmp = pixmap.imgg.getClip(); pixmap.imgg.clipRect(dstx, dsty, width, height); pixmap.imgg.drawImage(dataImg, 0, 0, Screen.screen[0].root.ddxwindow); if (tmp == null) { pixmap.imgg.setClip(0, 0, d.width, d.height); } else { pixmap.imgg.setClip(tmp); } } else { pixmap.imgg.drawImage(dataImg, 0, 0, Screen.screen[0].root.ddxwindow); } dataImg.flush(); pixmap.reset(); pixmap.time = System.currentTimeMillis(); } } }
From source file:com.jcraft.weirdx.DDXWindowImp.java
public void copyArea(XWindow dst, GC gc, int srcx, int srcy, int width, int height, int destx, int desty) { Graphics g = dst.getGraphics(); if (g == null) return;//from ww w . java 2 s . c om if (window == dst) { copyArea(srcx, srcy, width, height, destx - srcx, desty - srcy); dst.draw(destx, desty, width, height); return; } Image img = window.getImage(gc, srcx, srcy, width, height); if (srcx == 0 && srcy == 0 && width == window.width && height == window.height) { dst.ddxwindow.drawImage(gc.clip_mask, img, destx, desty, width, height); } else { java.awt.Shape tmp = g.getClip(); g.clipRect(destx, desty, width, height); dst.ddxwindow.drawImage(gc.clip_mask, img, destx - srcx, desty - srcy, window.width, window.height); if (tmp == null) { g.setClip(0, 0, dst.width, dst.height); } else { g.setClip(tmp); } } dst.draw(destx, desty, width, height); if (img != window.getImage()) { img.flush(); } }
From source file:com.jcraft.weirdx.XPixmap.java
Image getImage(GC gc, int x, int y, int w, int h) { if (data != null && time < colormap.icmtime) { mkMIS();//from w ww. ja va 2s . c om Image dataImg = Toolkit.getDefaultToolkit().createImage(mis); time = System.currentTimeMillis(); imgg.drawImage(dataImg, 0, 0, Screen.screen[0].root.ddxwindow); //?? dataImg.flush(); } Image i = getImage(); if (gc != null && gc.clip_mask != null && gc.clip_mask instanceof ClipPixmap) { TransparentFilter tf = new TransparentFilter(0, 0, (XPixmap) (gc.clip_mask.getMask())); i = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(i.getSource(), tf)); } return i; }
From source file:com.jcraft.weirdx.XPixmap.java
Image getImage(GC gc, int x, int y, int w, int h) { if (data != null && time < colormap.icmtime) { mkMIS();/*from w w w . j a va 2s. c o m*/ Image dataImg = Toolkit.getDefaultToolkit().createImage(mis); time = System.currentTimeMillis(); imgg.drawImage(dataImg, 0, 0, Screen.screen[0].root.ddxwindow); //?? dataImg.flush(); } Image i = getImage(); if (gc != null && gc.clip_mask != null && gc.clip_mask instanceof ClipPixmap) { TransparentFilter tf = new TransparentFilter(0, 0, (XPixmap) (gc.clip_mask.getMask())); i = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(i.getSource(), tf)); } return i; }