List of usage examples for java.awt.image BufferedImage getWidth
public int getWidth()
From source file:Main.java
/** * This method cleans input image by replacing all pixels with RGB values * from -3092272 (light gray) to -1 (white) with white pixels and * from -3092272 (light gray) to -16777216 (black) with black pixels * @param image - input image that will be cleaned * @return - cleaned input image as BufferedImage *//*w w w .j a va 2s . c om*/ public static BufferedImage blackAndLightGrayCleaning(BufferedImage image) { for (int j = 0; j < image.getHeight(); j++) { for (int i = 0; i < image.getWidth(); i++) { if (image.getRGB(i, j) > -3092272) { image.setRGB(i, j, -1); } else { image.setRGB(i, j, -16777216); } } } return image; }
From source file:Main.java
/** * Shrinks an image to fit into memory more * Effectively.//from www . j a v a 2 s.c om * @param src The source image. * @return */ public static BufferedImage imgUtilMinimizeNoAlpha(BufferedImage src) { if (src == null) return null; BufferedImage b = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_3BYTE_BGR); Graphics2D g = (Graphics2D) b.getGraphics(); g.drawImage(src, 0, 0, null); g.dispose(); return b; }
From source file:Main.java
/** * Draws the given {@link BufferedImage} to the canvas, centered and cropped to fill the * bounds defined by the destination rectangle, and with preserved aspect ratio. * * @param g The destination canvas. * @param source The source image.//from w w w . jav a 2 s . co m * @param dstRect The destination rectangle in the destination canvas into which to draw the * image. */ public static void drawCenterCrop(Graphics2D g, BufferedImage source, Rectangle dstRect) { final int srcWidth = source.getWidth(); final int srcHeight = source.getHeight(); if (srcWidth * 1.0 / srcHeight > dstRect.width * 1.0 / dstRect.height) { final int scaledWidth = dstRect.height * srcWidth / srcHeight; final int scaledHeight = dstRect.height; Image scaledImage = scaledImage(source, scaledWidth, scaledHeight); g.drawImage(scaledImage, dstRect.x, dstRect.y, dstRect.x + dstRect.width, dstRect.y + dstRect.height, 0 + (scaledWidth - dstRect.width) / 2, 0, 0 + (scaledWidth - dstRect.width) / 2 + dstRect.width, 0 + dstRect.height, null); } else { final int scaledWidth = dstRect.width; final int scaledHeight = dstRect.width * srcHeight / srcWidth; Image scaledImage = scaledImage(source, scaledWidth, scaledHeight); g.drawImage(scaledImage, dstRect.x, dstRect.y, dstRect.x + dstRect.width, dstRect.y + dstRect.height, 0, 0 + (scaledHeight - dstRect.height) / 2, 0 + dstRect.width, 0 + (scaledHeight - dstRect.height) / 2 + dstRect.height, null); } }
From source file:Main.java
/** * Draws the given {@link BufferedImage} to the canvas, centered, wholly contained within the * bounds defined by the destination rectangle, and with preserved aspect ratio. * * @param g The destination canvas. * @param source The source image./*from ww w.ja va 2s . c o m*/ * @param dstRect The destination rectangle in the destination canvas into which to draw the * image. */ public static void drawCenterInside(Graphics2D g, BufferedImage source, Rectangle dstRect) { final int srcWidth = source.getWidth(); final int srcHeight = source.getHeight(); if (srcWidth * 1.0 / srcHeight > dstRect.width * 1.0 / dstRect.height) { final int scaledWidth = Math.max(1, dstRect.width); final int scaledHeight = Math.max(1, dstRect.width * srcHeight / srcWidth); Image scaledImage = scaledImage(source, scaledWidth, scaledHeight); g.drawImage(scaledImage, dstRect.x, dstRect.y + (dstRect.height - scaledHeight) / 2, dstRect.x + dstRect.width, dstRect.y + (dstRect.height - scaledHeight) / 2 + scaledHeight, 0, 0, 0 + scaledWidth, 0 + scaledHeight, null); } else { final int scaledWidth = Math.max(1, dstRect.height * srcWidth / srcHeight); final int scaledHeight = Math.max(1, dstRect.height); Image scaledImage = scaledImage(source, scaledWidth, scaledHeight); g.drawImage(scaledImage, dstRect.x + (dstRect.width - scaledWidth) / 2, dstRect.y, dstRect.x + (dstRect.width - scaledWidth) / 2 + scaledWidth, dstRect.y + dstRect.height, 0, 0, 0 + scaledWidth, 0 + scaledHeight, null); } }
From source file:Main.java
public static BufferedImage generateOutline(BufferedImage source, Color color1, Color color2, boolean alpha) { int w = source.getWidth(); int h = source.getHeight(); BufferedImage result = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR); for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { if (!testNeighbour(source, x, y, alpha)) { if (testNeighbours(source, x, y, alpha)) { boolean a = ((x + y) % 10) <= 5; result.setRGB(x, y, a ? color1.getRGB() : color2.getRGB()); }//from w ww . j a v a 2 s . c om } } } return result; }
From source file:GraphicsUtil.java
public static void drawCentered(Graphics g, BufferedImage img, Point2D location) { drawCentered(g, img, location, img.getWidth(), img.getHeight(), null); }
From source file:Main.java
/** * Trims the transparent pixels from the given {@link BufferedImage} (returns a sub-image). * * @param source The source image./*from w ww . j a v a 2 s .co m*/ * @return A new, trimmed image, or the source image if no trim is performed. */ public static BufferedImage trimmedImage(BufferedImage source) { final int minAlpha = 1; final int srcWidth = source.getWidth(); final int srcHeight = source.getHeight(); Raster raster = source.getRaster(); int l = srcWidth, t = srcHeight, r = 0, b = 0; int alpha, x, y; int[] pixel = new int[4]; for (y = 0; y < srcHeight; y++) { for (x = 0; x < srcWidth; x++) { raster.getPixel(x, y, pixel); alpha = pixel[3]; if (alpha >= minAlpha) { l = Math.min(x, l); t = Math.min(y, t); r = Math.max(x, r); b = Math.max(y, b); } } } if (l > r || t > b) { // No pixels, couldn't trim return source; } return source.getSubimage(l, t, r - l + 1, b - t + 1); }
From source file:Main.java
public static BufferedImage create(BufferedImage image, double angle, GraphicsConfiguration gc) { double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle)); int w = image.getWidth(), h = image.getHeight(); int neww = (int) Math.floor(w * cos + h * sin), newh = (int) Math.floor(h * cos + w * sin); int transparency = image.getColorModel().getTransparency(); BufferedImage result = gc.createCompatibleImage(neww, newh, transparency); Graphics2D g = result.createGraphics(); g.translate((neww - w) / 2, (newh - h) / 2); g.rotate(angle, w / 2, h / 2);/*from w w w.ja v a2 s . c o m*/ g.drawRenderedImage(image, null); return result; }
From source file:Main.java
/** * This method cleans input image by replacing all pixels with RGB values * from RGBcolor input (the input color) to -1 (white) with white pixels and * from RGBcolor input (the input color) to -16777216 (black) with black pixels * @param image - input image that will be cleaned * @param RGBcolor - input RGB value of wanted color as reference for celaning * @return - cleaned input image as BufferedImage *///from ww w .j a v a2 s . c o m public static BufferedImage colorCleaning(BufferedImage image, int RGBcolor) { for (int j = 0; j < image.getHeight(); j++) { for (int i = 0; i < image.getWidth(); i++) { if (image.getRGB(i, j) == RGBcolor) { image.setRGB(i, j, -16777216); } else { image.setRGB(i, j, -1); } } } return image; }
From source file:Main.java
/** * Creates an outline of an image,//from www .j av a2 s .c o m * with the default clipping rectangle. * @param src The source image. * @param c The color to outline the image * in. * @return */ public static BufferedImage imgUtilOutline(BufferedImage src, Color c) { if (src == null) return null; BufferedImage b = new BufferedImage(src.getWidth(), src.getHeight(), src.getType()); Graphics2D g = (Graphics2D) b.getGraphics(); g.setColor(c); g.drawRect(1, 1, src.getWidth() - 1, src.getHeight() - 1); g.drawImage(src, 0, 0, null); g.dispose(); return b; }