List of usage examples for java.awt.image BufferedImage getType
public int getType()
From source file:Main.java
/** * Snapshots the specified JavaFX {@link Image} object and stores a * copy of its pixels into a {@link BufferedImage} object, creating * a new object if needed.//from w w w . j ava 2s . c om * The method will only convert a JavaFX {@code Image} that is readable * as per the conditions on the * {@link Image#getPixelReader() Image.getPixelReader()} * method. * If the {@code Image} is not readable, as determined by its * {@code getPixelReader()} method, then this method will return null. * If the {@code Image} is a writable, or other dynamic image, then * the {@code BufferedImage} will only be set to the current state of * the pixels in the image as determined by its {@link PixelReader}. * Further changes to the pixels of the {@code Image} will not be * reflected in the returned {@code BufferedImage}. * <p> * The optional {@code BufferedImage} parameter may be reused to store * the copy of the pixels. * A new {@code BufferedImage} 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 img the JavaFX {@code Image} to be converted * @param bimg an optional {@code BufferedImage} object that may be * used to store the returned pixel data * @return a {@code BufferedImage} containing a snapshot of the JavaFX * {@code Image}, or null if the {@code Image} is not readable. * @since JavaFX 2.2 */ public static BufferedImage fromFXImage(Image img, BufferedImage bimg) { PixelReader pr = img.getPixelReader(); if (pr == null) { return null; } int iw = (int) img.getWidth(); int ih = (int) img.getHeight(); int prefBimgType = getBestBufferedImageType(pr.getPixelFormat(), bimg); if (bimg != null) { int bw = bimg.getWidth(); int bh = bimg.getHeight(); if (bw < iw || bh < ih || bimg.getType() != prefBimgType) { bimg = null; } else if (iw < bw || ih < bh) { Graphics2D g2d = bimg.createGraphics(); g2d.setComposite(AlphaComposite.Clear); g2d.fillRect(0, 0, bw, bh); g2d.dispose(); } } if (bimg == null) { bimg = new BufferedImage(iw, ih, prefBimgType); } IntegerComponentRaster icr = (IntegerComponentRaster) bimg.getRaster(); int offset = icr.getDataOffset(0); int scan = icr.getScanlineStride(); int data[] = icr.getDataStorage(); WritablePixelFormat<IntBuffer> pf = getAssociatedPixelFormat(bimg); pr.getPixels(0, 0, iw, ih, pf, data, offset, scan); return bimg; }
From source file:de.darkblue.bongloader2.utils.ToolBox.java
public static BufferedImage grayScaleAlpha(BufferedImage original) { int alpha, red, green, blue; int newPixel; BufferedImage avg_gray = new BufferedImage(original.getWidth(), original.getHeight(), original.getType()); int[] avgLUT = new int[766]; for (int i = 0; i < avgLUT.length; i++) { avgLUT[i] = (int) (i / 3); }/*from w w w. j a va 2 s .c o m*/ for (int x = 0; x < original.getWidth(); x++) { for (int y = 0; y < original.getHeight(); y++) { // Get pixels by R, G, B int color = original.getRGB(x, y); alpha = color & 0xFF000000; red = (color >> 16) & 0xFF; green = (color >> 8) & 0xFF; blue = color & 0xFF; newPixel = red + green + blue; newPixel = avgLUT[newPixel]; // Return back to original format newPixel = newPixel | (newPixel << 8) | (newPixel << 16) | alpha; // Write pixels into image avg_gray.setRGB(x, y, newPixel); } } return avg_gray; }
From source file:com.alvermont.terraj.stargen.ui.UIUtils.java
/** * Scale an image to a particular X and Y size in pixels * /*from www . j ava2 s .co m*/ * @param image The image to be scaled * @param pixelsX The desired horizontal size in pixels * @param pixelsY The desired vertical size in pixels * @return A new image scaled to the requested dimensions */ public static BufferedImage scaleImage(BufferedImage image, int pixelsX, int pixelsY) { int actualWidth = image.getWidth(); int actualHeight = image.getHeight(); // work out the scale factor double sx = (float) (pixelsX) / actualWidth; double sy = (float) (pixelsY) / actualHeight; BufferedImage nbi = new BufferedImage(pixelsX, pixelsY, image.getType()); Graphics2D g2 = nbi.createGraphics(); AffineTransform at = AffineTransform.getScaleInstance(sx, sy); g2.setTransform(at); g2.drawImage(image, 0, 0, null); return nbi; }
From source file:com.ttech.cordovabuild.domain.application.source.ApplicationSourceFactoryImpl.java
public static BufferedImage scaleTo(BufferedImage image, Integer height, Integer width) throws IOException { int imageWidth = image.getWidth(); int imageHeight = image.getHeight(); double scaleX = (double) (width == null ? 1 : width / imageWidth); double scaleY = (double) (height == null ? 1 : height / imageHeight); AffineTransform scaleTransform = AffineTransform.getScaleInstance(scaleX, scaleY); AffineTransformOp bilinearScaleOp = new AffineTransformOp(scaleTransform, AffineTransformOp.TYPE_BILINEAR); return bilinearScaleOp.filter(image, new BufferedImage(width, height, image.getType())); }
From source file:com.flexive.shared.media.impl.FxMediaNativeEngine.java
/** * Flip the image horizontal/*w w w . j a va2s.c o m*/ * * @param bufferedImage original image * @return horizontally flipped image */ private static BufferedImage flipHorizontal(BufferedImage bufferedImage) { AffineTransform at = AffineTransform.getTranslateInstance(bufferedImage.getWidth(), 0); at.scale(-1.0, 1.0); BufferedImageOp biOp = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); BufferedImage imgRes = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), bufferedImage.getType()); return biOp.filter(bufferedImage, imgRes); }
From source file:com.flexive.shared.media.impl.FxMediaNativeEngine.java
/** * Flip the image horizontal/*www. j a v a 2 s .c o m*/ * * @param bufferedImage original image * @return horizontally flipped image */ private static BufferedImage flipVertical(BufferedImage bufferedImage) { AffineTransform at = AffineTransform.getTranslateInstance(0, bufferedImage.getHeight()); at.scale(1.0, -1.0); BufferedImageOp biOp = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); BufferedImage imgRes = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), bufferedImage.getType()); return biOp.filter(bufferedImage, imgRes); }
From source file:common.utils.ImageUtils.java
/** * resize input image to new dinesions(only smaller) and save it to file * @param image input image for scaling * @param scale_factor factor for scaling image * @param rez writes resulting image to a file * @throws IOException/*from w ww.ja va 2s. c o m*/ */ public static void saveScaledImageWidth(BufferedImage image, double scale_factor, OutputStream rez) throws IOException { //long time_resize = System.currentTimeMillis(); if (scale_factor == 1) { writeImage(image, 1, rez); } else { int width = (int) (scale_factor * image.getWidth()); int height = (int) (scale_factor * image.getHeight()); //BufferedImage scaled_bi = new BufferedImage( image.getWidth(), image.getHeight(), image.getType() ); int type = image.getType(); BufferedImage scaled_bi; if (type == BufferedImage.TYPE_CUSTOM) { scaled_bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); } else { scaled_bi = new BufferedImage(width, height, type); } Graphics2D g2 = scaled_bi.createGraphics(); //g2.drawImage(image, at, null); g2.drawImage(image.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null); writeImage(scaled_bi, 1, rez); g2.dispose(); } //time_resize = System.currentTimeMillis() - time_resize; //System.out.print("time_resize=" + (time_resize) + "; "); }
From source file:com.aimluck.eip.fileupload.util.FileuploadUtils.java
public static BufferedImage transformImage(BufferedImage image, AffineTransform transform, int newWidth, int newHeight) throws Exception { AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BICUBIC); BufferedImage destinationImage = new BufferedImage(newWidth, newHeight, image.getType()); Graphics2D g = destinationImage.createGraphics(); g.setColor(Color.WHITE);/*from w ww . jav a 2 s . c om*/ destinationImage = op.filter(image, destinationImage); return destinationImage; }
From source file:ImageClip.java
/** * Clips the input image to the specified shape * // w ww .j a v a 2s . c om * @param image * the input image * @param clipVerts * list of x, y pairs defining the clip shape, normalised * to image dimensions (think texture coordinates) * @return The smallest image containing those pixels that fall * inside the clip shape */ public static BufferedImage clip(BufferedImage image, float... clipVerts) { assert clipVerts.length >= 6; assert clipVerts.length % 2 == 0; int[] xp = new int[clipVerts.length / 2]; int[] yp = new int[xp.length]; int minX = image.getWidth(), minY = image.getHeight(), maxX = 0, maxY = 0; for (int j = 0; j < xp.length; j++) { xp[j] = Math.round(clipVerts[2 * j] * image.getWidth()); yp[j] = Math.round(clipVerts[2 * j + 1] * image.getHeight()); minX = Math.min(minX, xp[j]); minY = Math.min(minY, yp[j]); maxX = Math.max(maxX, xp[j]); maxY = Math.max(maxY, yp[j]); } for (int i = 0; i < xp.length; i++) { xp[i] -= minX; yp[i] -= minY; } Polygon clip = new Polygon(xp, yp, xp.length); BufferedImage out = new BufferedImage(maxX - minX, maxY - minY, image.getType()); Graphics g = out.getGraphics(); g.setClip(clip); g.drawImage(image, -minX, -minY, null); g.dispose(); return out; }
From source file:TextureByReference.java
static void printType(BufferedImage bImage) { int type = bImage.getType(); if (type == BufferedImage.TYPE_4BYTE_ABGR) { System.out.println("TYPE_4BYTE_ABGR"); } else if (type == BufferedImage.TYPE_INT_ARGB) { System.out.println("TYPE_INT_ARGB"); } else if (type == BufferedImage.TYPE_3BYTE_BGR) { System.out.println("TYPE_3BYTE_BGR"); } else if (type == BufferedImage.TYPE_CUSTOM) { System.out.println("TYPE_CUSTOM"); } else/* w ww .ja v a 2 s. com*/ System.out.println(type); }