List of usage examples for java.awt.image BufferedImage getHeight
public int getHeight()
From source file:ImageUtil.java
public static ByteArrayOutputStream fit(ByteArrayInputStream bais, int width, int height) throws IOException { BufferedImage src = ImageIO.read(bais); int newWidth; int newHeight; Float scale;/*from w w w . j a va2 s. c o m*/ if (src.getWidth() > src.getHeight()) { scale = Float.valueOf(width) / Float.valueOf(src.getWidth()); } else { scale = Float.valueOf(height) / Float.valueOf(src.getHeight()); } newWidth = Float.valueOf(src.getWidth() * scale).intValue(); newHeight = Float.valueOf(src.getHeight() * scale).intValue(); // 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, newWidth, newHeight); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(temp, "JPG", baos); return baos; }
From source file:ImageUtils.java
/** * Scales down an image into a box of maxSideLenght x maxSideLength. * @param image the image to scale down. It remains untouched. * @param maxSideLength the maximum side length of the scaled down instance. Has to be > 0. * @return the scaled image, the//from w w w.jav a2 s . c o m */ public static BufferedImage scaleImage(BufferedImage image, int maxSideLength) { assert (maxSideLength > 0); double originalWidth = image.getWidth(); double originalHeight = image.getHeight(); double scaleFactor = 0.0; if (originalWidth > originalHeight) { scaleFactor = ((double) maxSideLength / originalWidth); } else { scaleFactor = ((double) maxSideLength / originalHeight); } // create smaller image BufferedImage img = new BufferedImage((int) (originalWidth * scaleFactor), (int) (originalHeight * scaleFactor), BufferedImage.TYPE_INT_RGB); // fast scale (Java 1.4 & 1.5) Graphics g = img.getGraphics(); g.drawImage(image, 0, 0, img.getWidth(), img.getHeight(), null); return img; }
From source file:ImageUtil.java
public static BufferedImage crop(BufferedImage src, int width, int height) throws IOException { int x = src.getWidth() / 2 - width / 2; int y = src.getHeight() / 2 - height / 2; // System.out.println("---" + src.getWidth() + " - " + src.getHeight() + " - " + x + " - " + y); BufferedImage clipping = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//src.getType()); Graphics2D area = (Graphics2D) clipping.getGraphics().create(); area.drawImage(src, 0, 0, clipping.getWidth(), clipping.getHeight(), x, y, x + clipping.getWidth(), y + clipping.getHeight(), null); area.dispose();// ww w . j a v a 2 s . co m return clipping; }
From source file:imageprocessingproject.ImageHistogram.java
public static BufferedImage normalizeImage(BufferedImage image) { int height = image.getHeight(); int width = image.getWidth(); int r, g, b, minr = 255, ming = 255, minb = 255, maxr = 0, maxg = 0, maxb = 0; Color c;//from ww w.j ava2 s. com BufferedImage tempImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { c = new Color(image.getRGB(i, j)); if (minr > c.getRed()) { minr = c.getRed(); } if (ming > c.getGreen()) { ming = c.getGreen(); } if (minb > c.getBlue()) { minb = c.getBlue(); } if (maxr < c.getRed()) { maxr = c.getRed(); } if (maxg < c.getGreen()) { maxg = c.getGreen(); } if (maxb < c.getBlue()) { maxb = c.getBlue(); } } } for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { c = new Color(image.getRGB(i, j)); r = (int) ((c.getRed() - minr) * 255 / (maxr - minr)); g = (int) ((c.getGreen() - ming) * 255 / (maxg - ming)); b = (int) ((c.getBlue() - minb) * 255 / (maxb - minb)); tempImage.setRGB(i, j, new Color(r, g, b, c.getAlpha()).getRGB()); } } return tempImage; }
From source file:ConvertUtil.java
/** * Converts the source image to 8-bit colour using the default 256-colour * palette. No transparency./* w w w. java 2 s .com*/ * * @param src * the source image to convert * @return a copy of the source image with an 8-bit colour depth */ public static BufferedImage convert8(BufferedImage src) { BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_INDEXED); ColorConvertOp cco = new ColorConvertOp(src.getColorModel().getColorSpace(), dest.getColorModel().getColorSpace(), null); cco.filter(src, dest); return dest; }
From source file:ConvertUtil.java
/** * Converts the source image to 24-bit colour (RGB). No transparency. * //from w ww . j a v a2 s.c o m * @param src * the source image to convert * @return a copy of the source image with a 24-bit colour depth */ public static BufferedImage convert24(BufferedImage src) { BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_RGB); ColorConvertOp cco = new ColorConvertOp(src.getColorModel().getColorSpace(), dest.getColorModel().getColorSpace(), null); cco.filter(src, dest); return dest; }
From source file:ConvertUtil.java
/** * Converts the source image to 32-bit colour with transparency (ARGB). * //from ww w . ja v a 2s. c o m * @param src * the source image to convert * @return a copy of the source image with a 32-bit colour depth. */ public static BufferedImage convert32(BufferedImage src) { BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_ARGB); ColorConvertOp cco = new ColorConvertOp(src.getColorModel().getColorSpace(), dest.getColorModel().getColorSpace(), null); cco.filter(src, dest); return dest; }
From source file:Main.java
/** * Snapshots the specified {@link BufferedImage} and stores a copy of * its pixels into a JavaFX {@link Image} object, creating a new * object if needed./*from w w w .ja va 2 s .c o m*/ * The returned {@code Image} will be a static snapshot of the state * of the pixels in the {@code BufferedImage} at the time the method * completes. Further changes to the {@code BufferedImage} will not * be reflected in the {@code Image}. * <p> * The optional JavaFX {@link WritableImage} parameter may be reused * to store the copy of the pixels. * A new {@code Image} will be created if the supplied object is null, * is too small or of a type which the image pixels cannot be easily * converted into. * * @param bimg the {@code BufferedImage} object to be converted * @param wimg an optional {@code WritableImage} object that can be * used to store the returned pixel data * @return an {@code Image} object representing a snapshot of the * current pixels in the {@code BufferedImage}. * @since JavaFX 2.2 */ public static WritableImage toFXImage(BufferedImage bimg, WritableImage wimg) { int bw = bimg.getWidth(); int bh = bimg.getHeight(); switch (bimg.getType()) { case BufferedImage.TYPE_INT_ARGB: case BufferedImage.TYPE_INT_ARGB_PRE: break; default: BufferedImage converted = new BufferedImage(bw, bh, BufferedImage.TYPE_INT_ARGB_PRE); Graphics2D g2d = converted.createGraphics(); g2d.drawImage(bimg, 0, 0, null); g2d.dispose(); bimg = converted; break; } // assert(bimg.getType == TYPE_INT_ARGB[_PRE]); if (wimg != null) { int iw = (int) wimg.getWidth(); int ih = (int) wimg.getHeight(); if (iw < bw || ih < bh) { wimg = null; } else if (bw < iw || bh < ih) { int empty[] = new int[iw]; PixelWriter pw = wimg.getPixelWriter(); PixelFormat<IntBuffer> pf = PixelFormat.getIntArgbPreInstance(); if (bw < iw) { pw.setPixels(bw, 0, iw - bw, bh, pf, empty, 0, 0); } if (bh < ih) { pw.setPixels(0, bh, iw, ih - bh, pf, empty, 0, 0); } } } if (wimg == null) { wimg = new WritableImage(bw, bh); } PixelWriter pw = wimg.getPixelWriter(); IntegerComponentRaster icr = (IntegerComponentRaster) bimg.getRaster(); int data[] = icr.getDataStorage(); int offset = icr.getDataOffset(0); int scan = icr.getScanlineStride(); PixelFormat<IntBuffer> pf = (bimg.isAlphaPremultiplied() ? PixelFormat.getIntArgbPreInstance() : PixelFormat.getIntArgbInstance()); pw.setPixels(0, 0, bw, bh, pf, data, offset, scan); return wimg; }
From source file:Main.java
/** * Quickly crops an image from its source. * @param src The source image.// ww w.ja v a 2 s. com * @param x The x coordinate to start at. * @param y The y coordinate to start at. * @param width The width to clip. * @param height The height to clip. * @return */ public static BufferedImage imgUtilFastCrop(BufferedImage src, int x, int y, int width, int height) { if (src == null) return null; if (x == 0 && y == 0 && width == src.getWidth() && height == src.getHeight()) return imgUtilFastCopy(src); else { BufferedImage b = new BufferedImage(width, height, src.getType()); Graphics g = b.getGraphics(); g.drawImage(src, -x, -y, null); g.dispose(); return b; } }
From source file:imageprocessingproject.ImageHistogram.java
private static void createHistogram(BufferedImage image) { histogram = new double[256]; int height = image.getHeight(); int width = image.getWidth(); int sum = 0;/* w w w . j a v a 2 s .com*/ for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { int grayVal = getGrayValue(image.getRGB(i, j)); histogram[grayVal]++; sum++; } } for (int i = 0; i < 256; i++) { histogram[i] = histogram[i] / sum; } }