Java Image to BufferedImage getBufferedImage(java.awt.Image image)

Here you can find the source of getBufferedImage(java.awt.Image image)

Description

get Buffered Image

License

Open Source License

Return

the with same content as given .

Declaration

private static BufferedImage getBufferedImage(java.awt.Image image) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011 Google, Inc./*from www. j ava  2s .  c  o  m*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Google, Inc. - initial API and implementation
 *******************************************************************************/

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import java.awt.image.ImageObserver;

public class Main {
    /**
     * @return the {@link BufferedImage} with same content as given {@link java.awt.Image}.
     */
    private static BufferedImage getBufferedImage(java.awt.Image image) {
        BufferedImage bufferedImage;
        if (image instanceof BufferedImage) {
            bufferedImage = (BufferedImage) image;
        } else {
            waitForImage(image);
            // prepare dimensions
            int w = image.getWidth(null);
            int h = image.getHeight(null);
            // draw into BufferedImage
            bufferedImage = new BufferedImage(w, h,
                    BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2 = bufferedImage.createGraphics();
            g2.drawImage(image, 0, 0, null);
            // done
            g2.dispose();
        }
        return bufferedImage;
    }

    /**
     * Waits until image is fully loaded, so ready for drawing.
     */
    private static void waitForImage(java.awt.Image image) {
        BufferedImage bufferedImage = new BufferedImage(1, 1,
                BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = bufferedImage.createGraphics();
        // prepare observer
        final Object done = new Object();
        ImageObserver imageObserver = new ImageObserver() {
            public boolean imageUpdate(java.awt.Image img, int flags,
                    int x, int y, int width, int height) {
                if (flags < ALLBITS) {
                    return true;
                } else {
                    synchronized (done) {
                        done.notify();
                    }
                    return false;
                }
            }
        };
        // draw Image with wait
        synchronized (done) {
            boolean completelyLoaded = g2.drawImage(image, 0, 0,
                    imageObserver);
            if (!completelyLoaded) {
                while (true) {
                    try {
                        done.wait(0);
                        break;
                    } catch (InterruptedException e) {
                    }
                }
            }
        }
        // clean up
        g2.dispose();
    }
}

Related

  1. getBufferedImage(Image image)
  2. getBufferedImage(Image image)
  3. getBufferedImage(Image image)
  4. getBufferedImage(Image image)
  5. getBufferedImage(Image img)
  6. getScaledBufferedImage(Image icon, double scale)
  7. imageToBufferedImage(Image image)
  8. ImageToBufferedImage(Image image, int width, int height)
  9. imageToBufferedImage(Image img)