List of usage examples for java.awt.image BufferedImage getHeight
public int getHeight()
From source file:Main.java
/** * Creates an outline of an image,/*from ww w .j a va 2 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; }
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 . j ava2s . c o m g.drawRenderedImage(image, null); return result; }
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 w w.j av a 2 s . c om } } } return result; }
From source file:GraphicsUtil.java
public static void drawCentered(Graphics g, BufferedImage img, Point2D location, ImageObserver observer) { drawCentered(g, img, location, img.getWidth(), img.getHeight(), observer); }
From source file:app.utils.ImageUtilities.java
public static Image readImageFromFile(File imgFile) throws IOException { String imgName = imgFile.getName(); BufferedImage img = ImageIO.read(imgFile); Pixel[][] pixels = new Pixel[img.getWidth()][img.getHeight()]; for (int x = 0; x < img.getWidth(); x++) { for (int y = 0; y < img.getHeight(); y++) { int redValue = new Color(img.getRGB(x, y)).getRed(); int greenValue = new Color(img.getRGB(x, y)).getGreen(); int blueValue = new Color(img.getRGB(x, y)).getBlue(); pixels[x][y] = new Pixel(redValue, greenValue, blueValue); }//from w ww . j a va2 s .c o m } return new Image(imgName, pixels, img.getRaster().getNumDataElements()); }
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./* w w w. java2 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 w w w .j a va 2 s. com * @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
/** * Trims the transparent pixels from the given {@link BufferedImage} (returns a sub-image). * * @param source The source image.//from ww w . j a va 2 s .c om * @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:ImageUtil.java
public static ByteArrayOutputStream smartCrop(ByteArrayInputStream bais, int width, int height) throws IOException { BufferedImage src = ImageIO.read(bais); Float scale;//w w w.j a v a 2 s . c om if (src.getWidth() > src.getHeight()) { scale = Float.valueOf(height) / Float.valueOf(src.getHeight()); if (src.getWidth() * scale < width) { scale = Float.valueOf(width) / Float.valueOf(src.getWidth()); } } else { scale = Float.valueOf(width) / Float.valueOf(src.getWidth()); if (src.getHeight() * scale < height) { scale = Float.valueOf(height) / Float.valueOf(src.getHeight()); } } // // System.out.println("--- " + src.getWidth() + " - " + width); // System.out.println("--- " + src.getHeight() + " - " + height); // System.out.println("--- " + scale + " -- " + Float.valueOf(src.getWidth() * scale).intValue() + " -- " + Float.valueOf(src.getHeight() * scale).intValue()); // BufferedImage temp = scale(src, Float.valueOf(src.getWidth() * scale).intValue(), Float.valueOf(src.getHeight() * scale).intValue()); temp = crop(temp, width, height); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(temp, "JPG", baos); return baos; }
From source file:GraphicsUtil.java
public static void drawImage(Graphics g, BufferedImage img, ImageObserver observer) { Graphics2D g2 = (Graphics2D) g; g2.drawImage(img, 0, 0, img.getWidth(), img.getHeight(), observer); }