List of usage examples for java.awt Image getHeight
public abstract int getHeight(ImageObserver observer);
From source file:org.sbs.util.ImageCompress.java
/** Checks the given image for valid width and height. */ private static void checkImage(Image image) { waitForImage(image);/*from ww w. java 2 s .com*/ int imageWidth = image.getWidth(null); if (imageWidth < 1) throw new IllegalArgumentException("image width " + imageWidth + " is out of range"); int imageHeight = image.getHeight(null); if (imageHeight < 1) throw new IllegalArgumentException("image height " + imageHeight + " is out of range"); }
From source file:org.sbs.util.ImageCompress.java
/** Encodes the given image at the given quality to the output stream. */ private static void encode(OutputStream outputStream, Image outputImage, String format) throws java.io.IOException { int outputWidth = outputImage.getWidth(null); if (outputWidth < 1) throw new IllegalArgumentException("output image width " + outputWidth + " is out of range"); int outputHeight = outputImage.getHeight(null); if (outputHeight < 1) throw new IllegalArgumentException("output image height " + outputHeight + " is out of range"); // Get a buffered image from the image. BufferedImage bi = new BufferedImage(outputWidth, outputHeight, BufferedImage.TYPE_INT_RGB); Graphics2D biContext = bi.createGraphics(); biContext.drawImage(outputImage, 0, 0, null); ImageIO.write(bi, format, outputStream); outputStream.flush();/*from w w w . jav a 2 s . co m*/ }
From source file:net.rptools.lib.image.ImageUtil.java
/** * Look at the image and determine which Transparency is most appropriate. If it finds any translucent pixels it * returns Transparency.TRANSLUCENT, if it finds at least one purely transparent pixel and no translucent pixels it * will return Transparency.BITMASK, in all other cases it returns Transparency.OPAQUE, including errors * //from w w w .j a v a2s. com * @param image * @return one of Transparency constants */ public static int pickBestTransparency(Image image) { // Take a shortcut if possible if (image instanceof BufferedImage) { return pickBestTransparency((BufferedImage) image); } // Legacy method // NOTE: This is a horrible memory hog int width = image.getWidth(null); int height = image.getHeight(null); int[] pixelArray = new int[width * height]; PixelGrabber pg = new PixelGrabber(image, 0, 0, width, height, pixelArray, 0, width); try { pg.grabPixels(); } catch (InterruptedException e) { System.err.println("interrupted waiting for pixels!"); return Transparency.OPAQUE; } if ((pg.getStatus() & ImageObserver.ABORT) != 0) { System.err.println("image fetch aborted or errored"); return Transparency.OPAQUE; } // Look for specific pixels boolean foundTransparent = false; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { // Get the next pixel int pixel = pixelArray[y * width + x]; int alpha = (pixel >> 24) & 0xff; // Is there translucency or just pure transparency ? if (alpha > 0 && alpha < 255) { return Transparency.TRANSLUCENT; } if (alpha == 0 && !foundTransparent) { foundTransparent = true; } } } return foundTransparent ? Transparency.BITMASK : Transparency.OPAQUE; }
From source file:com.t3.image.ImageUtil.java
/** * Look at the image and determine which Transparency is most appropriate. * If it finds any translucent pixels it returns Transparency.TRANSLUCENT, if * it finds at least one purely transparent pixel and no translucent pixels * it will return Transparency.BITMASK, in all other cases it returns * Transparency.OPAQUE, including errors * /*from w w w . j ava 2s . c om*/ * @param image * @return one of Transparency constants */ public static int pickBestTransparency(Image image) { // Take a shortcut if possible if (image instanceof BufferedImage) { return pickBestTransparency((BufferedImage) image); } // Legacy method // NOTE: This is a horrible memory hog int width = image.getWidth(null); int height = image.getHeight(null); int[] pixelArray = new int[width * height]; PixelGrabber pg = new PixelGrabber(image, 0, 0, width, height, pixelArray, 0, width); try { pg.grabPixels(); } catch (InterruptedException e) { System.err.println("interrupted waiting for pixels!"); return Transparency.OPAQUE; } if ((pg.getStatus() & ImageObserver.ABORT) != 0) { System.err.println("image fetch aborted or errored"); return Transparency.OPAQUE; } // Look for specific pixels boolean foundTransparent = false; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { // Get the next pixel int pixel = pixelArray[y * width + x]; int alpha = (pixel >> 24) & 0xff; // Is there translucency or just pure transparency ? if (alpha > 0 && alpha < 255) { return Transparency.TRANSLUCENT; } if (alpha == 0 && !foundTransparent) { foundTransparent = true; } } } return foundTransparent ? Transparency.BITMASK : Transparency.OPAQUE; }
From source file:org.tolven.security.bean.DocProtectionBean.java
/** * Place the resulting scaled image into the output buffer * @param image The scaled image//from w ww .java2 s .co m * @param windowWidth The desired window width to return * @param windowHeight The desired window hight to return * @return A Buffered image, ready for output */ static public BufferedImage toBufferedImage(Image image, int windowWidth, int windowHeight) { image = new ImageIcon(image).getImage(); BufferedImage bufferedImage = new BufferedImage(windowWidth, windowHeight, BufferedImage.TYPE_INT_RGB); Graphics g = bufferedImage.createGraphics(); g.setColor(Color.WHITE); g.fillRect(0, 0, windowWidth, windowHeight); // Center image in window int hOffset = (windowWidth - image.getWidth(null)) / 2; int vOffset = (windowHeight - image.getHeight(null)) / 2; g.drawImage(image, hOffset, vOffset, null); g.dispose(); return bufferedImage; }
From source file:com.t3.image.ImageUtil.java
public static Image bytesToImage(byte[] imageBytes) throws IOException { if (imageBytes == null) { System.out.println("WEhaah??"); }//from www . j a v a 2 s . co m Throwable exception = null; Image image = null; try { image = Toolkit.getDefaultToolkit().createImage(imageBytes); MediaTracker tracker = new MediaTracker(observer); tracker.addImage(image, 0); tracker.waitForID(0); } catch (Throwable t) { exception = t; } if (image == null || exception != null || image.getWidth(null) <= 0 || image.getHeight(null) <= 0) { // Try the newer way (although it pretty much sucks rocks) image = ImageIO.read(new ByteArrayInputStream(imageBytes)); } if (image == null) { throw new IOException("Could not load image: " + exception); } return image; }
From source file:com.aimluck.eip.fileupload.util.FileuploadUtils.java
/** * ?????????//from w w w. ja v a2 s. c o m * * @param imgfile * @param dim * @return */ public static BufferedImage shrinkImage(BufferedImage imgfile, int width, int height) { int iwidth = imgfile.getWidth(); int iheight = imgfile.getHeight(); double ratio = Math.min((double) width / (double) iwidth, (double) height / (double) iheight); int shrinkedWidth = (int) (iwidth * ratio); int shrinkedHeight = (int) (iheight * ratio); // ?? Image targetImage = imgfile.getScaledInstance(shrinkedWidth, shrinkedHeight, Image.SCALE_AREA_AVERAGING); BufferedImage tmpImage = new BufferedImage(targetImage.getWidth(null), targetImage.getHeight(null), BufferedImage.TYPE_3BYTE_BGR); Graphics2D g = tmpImage.createGraphics(); g.setColor(Color.WHITE); g.fillRect(0, 0, shrinkedWidth, shrinkedHeight); g.drawImage(targetImage, 0, 0, null); return tmpImage; }
From source file:com.aimluck.eip.fileupload.util.FileuploadUtils.java
/** * ???// www . j av a2s . co m * * @param imgfile * @param width * @param height * @return */ public static BufferedImage shrinkAndTrimImage(BufferedImage imgfile, int width, int height) { int iwidth = imgfile.getWidth(); int iheight = imgfile.getHeight(); double ratio = Math.max((double) width / (double) iwidth, (double) height / (double) iheight); int shrinkedWidth; int shrinkedHeight; if ((iwidth <= width) || (iheight < height)) { shrinkedWidth = iwidth; shrinkedHeight = iheight; } else { shrinkedWidth = (int) (iwidth * ratio); shrinkedHeight = (int) (iheight * ratio); } // ?? Image targetImage = imgfile.getScaledInstance(shrinkedWidth, shrinkedHeight, Image.SCALE_AREA_AVERAGING); int w_size = targetImage.getWidth(null); int h_size = targetImage.getHeight(null); if (targetImage.getWidth(null) < width) { w_size = width; } if (targetImage.getHeight(null) < height) { h_size = height; } BufferedImage tmpImage = new BufferedImage(w_size, h_size, BufferedImage.TYPE_INT_RGB); Graphics2D g = tmpImage.createGraphics(); g.setBackground(Color.WHITE); g.setColor(Color.WHITE); // ?????????????? g.fillRect(0, 0, w_size, h_size); int diff_w = 0; int diff_h = 0; if (width > shrinkedWidth) { diff_w = (width - shrinkedWidth) / 2; } if (height > shrinkedHeight) { diff_h = (height - shrinkedHeight) / 2; } g.drawImage(targetImage, diff_w, diff_h, null); int _iwidth = tmpImage.getWidth(); int _iheight = tmpImage.getHeight(); BufferedImage _tmpImage; if (_iwidth > _iheight) { int diff = _iwidth - width; _tmpImage = tmpImage.getSubimage(diff / 2, 0, width, height); } else { int diff = _iheight - height; _tmpImage = tmpImage.getSubimage(0, diff / 2, width, height); } return _tmpImage; }
From source file:com.tomtom.speedtools.json.ImageSerializer.java
@Nonnull private static BufferedImage convertToBufferedImage(@Nonnull final Image image) throws IOException { assert image != null; if (image instanceof BufferedImage) { return (BufferedImage) image; }//from w ww .j a v a 2 s.c o m /** * Load the image in the background and wait * until is is downloaded. */ final MediaTracker tracker = new MediaTracker(new Component() { // Empty. }); tracker.addImage(image, 0); try { tracker.waitForAll(); } catch (final InterruptedException e) { throw new IOException(e.getMessage(), e); } /** * Create a buffered image with the right dimensions. */ final BufferedImage bufImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB); /** * Draw the image in the buffer and return it as base64 data. */ final Graphics g = bufImage.createGraphics(); g.drawImage(image, 0, 0, null); return bufImage; }
From source file:smanilov.mandelbrot.compute.Computer.java
/** * Creates a thread that draws points from the toDoList. * @param width The maximum x coordinate. * @param height The maximum y coordinate. * @return//from w ww . j a v a 2s . c o m */ private static Thread createShaderThread(final Image drawing, final Color foregroundColor, final Color backgroundColor, final ReentrantLock drawingLock, final int scale, final Point2D center) { Thread shaderThread = new Thread() { @Override public void run() { System.out.println("Shader: [START]"); int id = currentDrawingId; int currentIterations = iterations; super.run(); int width = drawing.getWidth(null); int height = drawing.getHeight(null); Point pixelCenter = new Point(width / 2, height / 2); while (active) { // TODO: remove busy-wait while (true) { queueLock.lock(); if (toDoList.size() == 0) { queueLock.unlock(); break; } Point p = toDoList.poll(); int i = p.x; int j = p.y; queueLock.unlock(); double k = 0; double aliasInterval = 1.0 / antiAliasing; for (int aliasx = 0; aliasx < antiAliasing; ++aliasx) { for (int aliasy = 0; aliasy < antiAliasing; ++aliasy) { double x = i - 0.5 + aliasInterval / 2 + aliasInterval * aliasx; double y = j - 0.5 + aliasInterval / 2 + aliasInterval * aliasy; Complex c = toComplex(x, y, pixelCenter, scale, center); Complex z = new Complex(c.getReal(), c.getImaginary()); k += 1.0; for (int aliask = 1; aliask < currentIterations; ++aliask, k += 1.0) { if (id != currentDrawingId) return; z = z.multiply(z).add(c); if (z.abs() > 2) break; } } } k /= antiAliasing * antiAliasing; if (Math.ceil(k) == currentIterations) { drawingLock.lock(); Graphics g = drawing.getGraphics(); Color color = mixColors(foregroundColor, backgroundColor, k + 1 - currentIterations); g.setColor(color); g.fillRect(i, j, 1, 1); drawingLock.unlock(); } else { drawingLock.lock(); Graphics g = drawing.getGraphics(); Color color = mixColors(backgroundColor, foregroundColor, (double) k / currentIterations); g.setColor(color); g.fillRect(i, j, 1, 1); drawingLock.unlock(); } nDrawnLock.lock(); ++nDrawn; nDrawnLock.unlock(); } } long interval = System.currentTimeMillis() - startTime; System.out.println("Shader: [END after " + interval + " ms]"); saveImage(drawing, drawingLock); } }; return shaderThread; }