Java Code Examples for java.awt.image.PixelGrabber#getWidth()

The following examples show how to use java.awt.image.PixelGrabber#getWidth() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImageUtils.java    From openbd-core with GNU General Public License v3.0 6 votes vote down vote up
/**
    * Cretae a BufferedImage from an ImageProducer.
    * @param producer the ImageProducer
    * @return a new TYPE_INT_ARGB BufferedImage
    */
   public static BufferedImage createImage(ImageProducer producer) {
	PixelGrabber pg = new PixelGrabber(producer, 0, 0, -1, -1, null, 0, 0);
	try {
		pg.grabPixels();
	} catch (InterruptedException e) {
		throw new RuntimeException("Image fetch interrupted");
	}
	if ((pg.status() & ImageObserver.ABORT) != 0)
		throw new RuntimeException("Image fetch aborted");
	if ((pg.status() & ImageObserver.ERROR) != 0)
		throw new RuntimeException("Image fetch error");
	BufferedImage p = new BufferedImage(pg.getWidth(), pg.getHeight(), BufferedImage.TYPE_INT_ARGB);
	p.setRGB(0, 0, pg.getWidth(), pg.getHeight(), (int[])pg.getPixels(), 0, pg.getWidth());
	return p;
}
 
Example 2
Source File: GifEncoder.java    From jrobin with GNU Lesser General Public License v2.1 6 votes vote down vote up
DirectGif89Frame(Image img) throws IOException {
	PixelGrabber pg = new PixelGrabber(img, 0, 0, -1, -1, true);
	String errmsg = null;
	try {
		if (!pg.grabPixels()) {
			errmsg = "can't grab pixels from image";
		}
	}
	catch (InterruptedException e) {
		errmsg = "interrupted grabbing pixels from image";
	}
	if (errmsg != null) {
		throw new IOException(errmsg + " (" + getClass().getName() + ")");
	}
	theWidth = pg.getWidth();
	theHeight = pg.getHeight();
	argbPixels = (int[]) pg.getPixels();
	ciPixels = new byte[argbPixels.length];
}
 
Example 3
Source File: GraphicsUtils.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
/**
 * 分割指定图像为image[]
 * 
 * @param image
 * @param row
 * @param col
 * @return
 */
public static Image[] getSplitImages(Image image, int row, int col, boolean isFiltrate) {
	int index = 0;
	int wlength = image.getWidth(null) / row;
	int hlength = image.getHeight(null) / col;
	int l = wlength * hlength;
	Image[] abufferedimage = new Image[l];
	for (int y = 0; y < hlength; y++) {
		for (int x = 0; x < wlength; x++) {
			abufferedimage[index] = GraphicsUtils.createImage(row, col, true);
			Graphics g = abufferedimage[index].getGraphics();
			g.drawImage(image, 0, 0, row, col, (x * row), (y * col), row + (x * row), col + (y * col), null);
			g.dispose();
			g = null;
			PixelGrabber pgr = new PixelGrabber(abufferedimage[index], 0, 0, -1, -1, true);
			try {
				pgr.grabPixels();
			} catch (InterruptedException ex) {
			}
			int pixels[] = (int[]) pgr.getPixels();
			if (isFiltrate) {
				for (int i = 0; i < pixels.length; i++) {
					int[] rgbs = LColor.getRGBs(pixels[i]);
					if ((rgbs[0] == 247 && rgbs[1] == 0 && rgbs[2] == 255)
							|| (rgbs[0] == 255 && rgbs[1] == 255 && rgbs[2] == 255)) {
						pixels[i] = 0;
					}
				}
			}
			ImageProducer ip = new MemoryImageSource(pgr.getWidth(), pgr.getHeight(), pixels, 0, pgr.getWidth());
			abufferedimage[index] = toolKit.createImage(ip);
			index++;
		}
	}
	return abufferedimage;
}
 
Example 4
Source File: GraphicsUtils.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
/**
 * 分割指定图像为image[]
 * 
 * @param image
 * @param row
 * @param col
 * @return
 */
public static Image[][] getSplit2Images(Image image, int row, int col, boolean isFiltrate) {
	int wlength = image.getWidth(null) / row;
	int hlength = image.getHeight(null) / col;
	Image[][] abufferedimage = new Image[wlength][hlength];
	for (int y = 0; y < hlength; y++) {
		for (int x = 0; x < wlength; x++) {
			abufferedimage[x][y] = GraphicsUtils.createImage(row, col, true);
			Graphics g = abufferedimage[x][y].getGraphics();
			g.drawImage(image, 0, 0, row, col, (x * row), (y * col), row + (x * row), col + (y * col), null);
			g.dispose();
			g = null;
			PixelGrabber pgr = new PixelGrabber(abufferedimage[x][y], 0, 0, -1, -1, true);
			try {
				pgr.grabPixels();
			} catch (InterruptedException ex) {
				ex.getStackTrace();
			}
			int pixels[] = (int[]) pgr.getPixels();
			if (isFiltrate) {
				for (int i = 0; i < pixels.length; i++) {
					int[] rgbs = LColor.getRGBs(pixels[i]);
					if ((rgbs[0] == 247 && rgbs[1] == 0 && rgbs[2] == 255)
							|| (rgbs[0] == 255 && rgbs[1] == 0 && rgbs[2] == 255)
							|| (rgbs[0] == 0 && rgbs[1] == 0 && rgbs[2] == 0)) {
						pixels[i] = 0;
					}
				}
			}
			ImageProducer ip = new MemoryImageSource(pgr.getWidth(), pgr.getHeight(), pixels, 0, pgr.getWidth());
			abufferedimage[x][y] = toolKit.createImage(ip);
		}
	}
	return abufferedimage;
}
 
Example 5
Source File: BmpWriter.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * The constructor.
 * 
 * @param img
 */
public BmpWriter( Image img )
{
	if ( img == null )
	{
		return;
	}

	PixelGrabber pg = new PixelGrabber( img, 0, 0, -1, -1, true );

	try
	{
		pg.grabPixels( );
	}
	catch ( InterruptedException e )
	{
		return;
	}

	if ( ( pg.status( ) & ImageObserver.ABORT ) != 0 )
	{
		return;
	}

	this.pix = (int[]) pg.getPixels( );
	this.width = pg.getWidth( );
	this.height = pg.getHeight( );
}
 
Example 6
Source File: AnimationHelper.java    From RipplePower with Apache License 2.0 4 votes vote down vote up
public static AnimationHelper makeObject(String fileName, int tileWidth, int tileHeight, Color col) {
	String key = fileName.trim().toLowerCase();
	AnimationHelper animation = (AnimationHelper) animations.get(key);
	if (animation == null) {
		Image image = GraphicsUtils.loadNotCacheImage(fileName);
		int c = col.getRGB();
		int wlength = image.getWidth(null) / tileWidth;
		int hlength = image.getHeight(null) / tileHeight;
		Image[][] images = new Image[wlength][hlength];
		for (int y = 0; y < hlength; y++) {
			for (int x = 0; x < wlength; x++) {
				images[x][y] = GraphicsUtils.createImage(tileWidth, tileHeight, true);
				Graphics g = images[x][y].getGraphics();
				g.drawImage(image, 0, 0, tileWidth, tileHeight, (x * tileWidth), (y * tileHeight),
						tileWidth + (x * tileWidth), tileHeight + (y * tileHeight), null);
				g.dispose();
				g = null;
				PixelGrabber pgr = new PixelGrabber(images[x][y], 0, 0, -1, -1, true);
				try {
					pgr.grabPixels();
				} catch (InterruptedException ex) {
					ex.getStackTrace();
				}
				int pixels[] = (int[]) pgr.getPixels();
				for (int i = 0; i < pixels.length; i++) {
					if (pixels[i] == c) {
						pixels[i] = 0;
					}
				}
				ImageProducer ip = new MemoryImageSource(pgr.getWidth(), pgr.getHeight(), pixels, 0,
						pgr.getWidth());
				images[x][y] = GraphicsUtils.toolKit.createImage(ip);
			}
		}
		Image[][] result = new Image[hlength][wlength];
		for (int y = 0; y < wlength; y++) {
			for (int x = 0; x < hlength; x++) {
				result[x][y] = images[y][x];
			}
		}
		images = null;
		animations.put(key, animation = makeObject(result[0], result[1], result[3], result[2]));
	}
	return animation;

}