List of usage examples for java.awt Image getWidth
public abstract int getWidth(ImageObserver observer);
From source file:com.piaoyou.util.ImageUtil.java
/** * This method returns a fit-sized image for a source image, * this method retains the ratio of the source image *//*from w w w . jav a 2 s . c o m*/ public static Image getFitSizeImage(Image srcImage, int fitWidth, int fitHeight) throws IOException { if ((fitWidth < 100) || (fitHeight < 100)) throw new IllegalArgumentException("Cannot accept values < 100"); int srcWidth = srcImage.getWidth(null);//xem lai cho nay vi neu dung BufferedImage thi khong can null int srcHeight = srcImage.getHeight(null); //log.trace("src w = " + srcWidth + " h = " + srcHeight); // don't need any transforms if ((srcWidth == fitWidth) && (srcHeight == fitHeight)) return srcImage; int newWidth = srcWidth; int newHeight = srcHeight; double fitRatio = (double) fitWidth / fitHeight; double srcRatio = (double) srcWidth / srcHeight; if (srcRatio > fitRatio) {// must cut the width of the source image newWidth = (int) (srcHeight * fitRatio); } else {// must cut the height of the source image newHeight = (int) (srcWidth / fitRatio); } //log.trace("new w = " + newWidth + " h = " + newHeight); ImageFilter cropFilter = new CropImageFilter((srcWidth - newWidth) / 2, (srcHeight - newHeight) / 2, newWidth, newHeight); ImageProducer cropProd = new FilteredImageSource(srcImage.getSource(), cropFilter); Image cropImage = Toolkit.getDefaultToolkit().createImage(cropProd); Image retImage = new ImageIcon(getScaledInstance(cropImage, fitWidth, fitHeight)).getImage(); return retImage; }
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// w ww .j a v a 2 s.co 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; }
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; }/*ww w. j a va 2s . 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:edu.ku.brc.specify.plugins.ipadexporter.InstitutionConfigDlg.java
public static BufferedImage toBufferedImage(final Image img) { if (img instanceof BufferedImage) { return (BufferedImage) img; }// w w w .j a v a 2s . c o m // Create a buffered image with transparency BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB); // Draw the image on to the buffered image Graphics2D bGr = bimage.createGraphics(); bGr.drawImage(img, 0, 0, null); bGr.dispose(); // Return the buffered image return bimage; }
From source file:net.rptools.lib.image.ImageUtil.java
/** * Converts a byte array into an {@link Image} instance. * /* ww w . j av a 2s . c om*/ * @param imageBytes * bytes to convert * @return * @throws IOException */ public static Image bytesToImage(byte[] imageBytes) throws IOException { if (imageBytes == null) { throw new IOException("Could not load image - no data provided"); } boolean interrupted = false; Throwable exception = null; Image image = null; image = Toolkit.getDefaultToolkit().createImage(imageBytes); MediaTracker tracker = new MediaTracker(observer); tracker.addImage(image, 0); do { try { interrupted = false; tracker.waitForID(0); // This is the only method that throws an exception } catch (InterruptedException t) { interrupted = true; continue; } catch (Throwable t) { exception = t; } } while (interrupted); 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:ImageTest.java
public ImagePanel(Image img) { this.img = img; Dimension size = new Dimension(img.getWidth(null), img.getHeight(null)); setPreferredSize(size);/* w w w . j a v a 2s . c o m*/ setMinimumSize(size); setMaximumSize(size); setSize(size); setLayout(null); }
From source file:MyCanvas.java
public void paint(Graphics g) { Image img1 = Toolkit.getDefaultToolkit().getImage("yourFile.gif"); int width = img1.getWidth(this); int height = img1.getHeight(this); int scale = 2; int w = scale * width; int h = scale * height; // explicitly specify width (w) and height (h) g.drawImage(img1, 10, 10, (int) w, (int) h, this); }
From source file:com.piaoyou.util.ImageUtil.java
/** * This method write the image to a stream. * It auto detect the image is Image or BufferedImage. * This method close the output stream before it return. * * @param image Image/*from w w w. jav a 2s . c o m*/ * @param outputStream OutputStream * @throws IOException */ public static void writeJpegImage_Sun(Image image, OutputStream outputStream) throws IOException { if (outputStream == null) { return; } try { BufferedImage bufferedImage = null; if (image instanceof BufferedImage) { bufferedImage = (BufferedImage) image; } else { // 30% cpu resource bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB); // 7.5 cpu Graphics2D g = bufferedImage.createGraphics(); // 50% cpu g.drawImage(image, 0, 0, null); g.dispose(); // free resource } // write it to disk // 12% cpu //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outputStream); //encoder.encode(bufferedImage); ImageIO.write(bufferedImage, "jpeg", outputStream); } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException ex) { ex.printStackTrace(); } } } }
From source file:ImageUtil.java
/** * Posted by alpha02 at http://www.dreamincode.net/code/snippet1076.htm *///from www . j av a 2 s .co m public static BufferedImage toBufferedImage(Image image) { if (image instanceof BufferedImage) return (BufferedImage) image; // This code ensures that all the pixels in the image are loaded image = new ImageIcon(image).getImage(); // Determine if the image has transparent pixels boolean hasAlpha = hasAlpha(image); // Create a buffered image with a format that's compatible with the screen BufferedImage bimage = null; GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); try { // Determine the type of transparency of the new buffered image int transparency = Transparency.OPAQUE; if (hasAlpha == true) transparency = Transparency.BITMASK; // Create the buffered image GraphicsDevice gs = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gs.getDefaultConfiguration(); bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency); } catch (HeadlessException e) { } //No screen if (bimage == null) { // Create a buffered image using the default color model int type = BufferedImage.TYPE_INT_RGB; if (hasAlpha == true) { type = BufferedImage.TYPE_INT_ARGB; } bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type); } // Copy image to buffered image Graphics g = bimage.createGraphics(); // Paint the image onto the buffered image g.drawImage(image, 0, 0, null); g.dispose(); return bimage; }
From source file:ImageFlip.java
public void paint(Graphics g) { Image myImage = new ImageIcon("yourImage.jpg").getImage(); BufferedImage bufferedImage = new BufferedImage(myImage.getWidth(null), myImage.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics2D g2d = (Graphics2D) g; Graphics gb = bufferedImage.getGraphics(); gb.drawImage(myImage, 0, 0, null);/*w w w .jav a2s. co m*/ gb.dispose(); AffineTransform tx = AffineTransform.getScaleInstance(-1, 1); tx.translate(-myImage.getWidth(null), 0); AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); bufferedImage = op.filter(bufferedImage, null); g2d.drawImage(myImage, 10, 10, null); g2d.drawImage(bufferedImage, null, 300, 10); }