Java Image to BufferedImage imageToBufferedImage(Image img)

Here you can find the source of imageToBufferedImage(Image img)

Description

image To Buffered Image

License

Open Source License

Declaration

public static BufferedImage imageToBufferedImage(Image img) 

Method Source Code

//package com.java2s;
/* autonoesis -- Automatic Movie Processor
 * Copyright (C) 2008 Shuichi Kurabayashi <Shuichi.Kurabayashi@acm.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version./*w ww.  j a  va2  s .c om*/
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

import java.awt.Component;
import java.awt.Image;
import java.awt.MediaTracker;

import java.awt.image.BufferedImage;

import java.awt.image.PixelGrabber;

public class Main {
    public static BufferedImage imageToBufferedImage(Image img) {
        try {
            MediaTracker tracker = new MediaTracker(new Component() {

                private static final long serialVersionUID = 1L;
            });
            tracker.addImage(img, 0);
            tracker.waitForAll();
        } catch (InterruptedException ie) {
            ie.printStackTrace();
            return null;
        }

        BufferedImage bimg = null;
        int w = img.getWidth(null);
        int h = img.getHeight(null);
        int[] pixels = new int[w * h];
        PixelGrabber pg = new PixelGrabber(img, 0, 0, w, h, pixels, 0, w);
        try {
            pg.grabPixels();
        } catch (InterruptedException ie) {
            ie.printStackTrace();
            return null;
        }
        bimg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        bimg.setRGB(0, 0, w, h, pixels, 0, w);
        return bimg;
    }
}

Related

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