List of usage examples for java.awt.image BufferedImage createGraphics
public Graphics2D createGraphics()
From source file:com.fun.util.TesseractUtil.java
/** * /*from ww w .j a v a 2 s .com*/ * * @param imageFile * @param times * @param targetFile * @throws IOException */ private static void scaled(File imageFile, int times, File targetFile) throws IOException { BufferedImage image = ImageIO.read(imageFile); int targetWidth = image.getWidth() * times; int targetHeight = image.getHeight() * times; int type = (image.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB; BufferedImage tmp = new BufferedImage(targetWidth, targetHeight, type); Graphics2D g2 = tmp.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2.drawImage(image, 0, 0, targetWidth, targetHeight, null); g2.dispose(); ImageIO.write(tmp, "png", targetFile); }
From source file:Main.java
/** * Paints a set of {@link Rectangle} object out of a rendered {@link BufferedImage} * such that the resulting image is transparent except for a minimum bounding * rectangle of the selected elements./*from w w w .j a va 2 s . co m*/ * * @param image the source image * @param rectangles the set of rectangles to copy * @param boundingBox the bounding rectangle of the set of rectangles to copy, can be * computed by {@link ImageUtils#getBoundingRectangle} * @param scale a scale factor to apply to the result, e.g. 0.5 to shrink the * destination down 50%, 1.0 to leave it alone and 2.0 to zoom in by * doubling the image size * @return a rendered image, or null */ public static BufferedImage drawRectangles(BufferedImage image, java.util.List<Rectangle> rectangles, Rectangle boundingBox, double scale) { // This code is not a test. When I implemented image cropping, I first implemented // it for BufferedImages (since it's easier; easy image painting, easy scaling, // easy transparency handling, etc). However, this meant that we would need to // convert the SWT images from the ImageOverlay to BufferedImages, crop and convert // back; not ideal, so I rewrote it in SWT (see SwtUtils). However, I // don't want to throw away the code in case we start keeping BufferedImages rather // than SWT images or need it for other purposes, but rather than place it in the // production codebase I'm leaving this utility here in the associated ImageUtils // test class. It was used like this: // @formatter:off // // BufferedImage wholeImage = SwtUtils.convertToAwt(image); // BufferedImage result = ImageUtils.cropSelection(wholeImage, // rectangles, boundingBox, scale); // e.image = SwtUtils.convertToSwt(image.getDevice(), result, true, // DRAG_TRANSPARENCY); // // @formatter:on if (boundingBox == null) { return null; } int destWidth = (int) (scale * boundingBox.width); int destHeight = (int) (scale * boundingBox.height); BufferedImage dest = new BufferedImage(destWidth, destHeight, image.getType()); Graphics2D g = dest.createGraphics(); for (Rectangle bounds : rectangles) { int dx1 = bounds.x - boundingBox.x; int dy1 = bounds.y - boundingBox.y; int dx2 = dx1 + bounds.width; int dy2 = dy1 + bounds.height; dx1 *= scale; dy1 *= scale; dx2 *= scale; dy2 *= scale; int sx1 = bounds.x; int sy1 = bounds.y; int sx2 = sx1 + bounds.width; int sy2 = sy1 + bounds.height; g.drawImage(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null); } g.dispose(); return dest; }
From source file:Main.java
public static Image getImage(Component c) { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gd = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gd.getDefaultConfiguration(); // Create an image that supports transparent pixels BufferedImage bImage = gc.createCompatibleImage(c.getWidth(), c.getHeight(), Transparency.BITMASK); /*/*from ww w . j a v a 2 s.c om*/ * And now this is how we get an image of the component */ Graphics2D g = bImage.createGraphics(); // Then use the current component we're in and call paint on this // graphics object c.paint(g); return bImage; }
From source file:Transparency.java
public static BufferedImage makeImageTranslucent(BufferedImage source, double alpha) { BufferedImage target = new BufferedImage(source.getWidth(), source.getHeight(), java.awt.Transparency.TRANSLUCENT); // Get the images graphics Graphics2D g = target.createGraphics(); // Set the Graphics composite to Alpha g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) alpha)); // Draw the image into the prepared reciver image g.drawImage(source, null, 0, 0);/*from w ww .j ava 2 s. co m*/ // let go of all system resources in this Graphics g.dispose(); // Return the image return target; }
From source file:net.imglib2.script.analysis.ChartUtils.java
public static final Img<ARGBType> asImage(final JFreeChart chart, int width, int height) { final ChartPanel panel = new ChartPanel(chart); final Dimension d = panel.getPreferredSize(); if (-1 == width && -1 == height) { width = d.width;//from w ww. j a va2 s . co m height = d.height; panel.setSize(d); } else { panel.setSize(width, height); } layoutComponent(panel); final BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); final Graphics2D g = bi.createGraphics(); if (!panel.isOpaque()) { g.setColor(panel.getBackground()); g.fillRect(0, 0, width, height); } panel.paint(g); final int[] pixels = new int[width * height]; final PixelGrabber pg = new PixelGrabber(bi, 0, 0, width, height, pixels, 0, width); try { pg.grabPixels(); } catch (final InterruptedException e) { } g.dispose(); final ArrayImg<ARGBType, IntArray> a = new ArrayImg<ARGBType, IntArray>(new IntArray(pixels), new long[] { width, height }, 1); // create a Type that is linked to the container final ARGBType linkedType = new ARGBType(a); // pass it to the DirectAccessContainer a.setLinkedType(linkedType); return a; }
From source file:org.jfree.data.general.HeatMapUtilities.java
/** * Creates an image that displays the values from the specified dataset. * * @param dataset the dataset (<code>null</code> not permitted). * @param paintScale the paint scale for the z-values (<code>null</code> * not permitted)./*from ww w . ja va 2 s.c o m*/ * * @return A buffered image. */ public static BufferedImage createHeatMapImage(HeatMapDataset dataset, PaintScale paintScale) { ParamChecks.nullNotPermitted(dataset, "dataset"); ParamChecks.nullNotPermitted(paintScale, "paintScale"); int xCount = dataset.getXSampleCount(); int yCount = dataset.getYSampleCount(); BufferedImage image = new BufferedImage(xCount, yCount, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = image.createGraphics(); for (int xIndex = 0; xIndex < xCount; xIndex++) { for (int yIndex = 0; yIndex < yCount; yIndex++) { double z = dataset.getZValue(xIndex, yIndex); Paint p = paintScale.getPaint(z); g2.setPaint(p); g2.fillRect(xIndex, yCount - yIndex - 1, 1, 1); } } return image; }
From source file:org.jfree.data.general.HeatMapUtils.java
/** * Creates an image that displays the values from the specified dataset. * * @param dataset the dataset ({@code null} not permitted). * @param paintScale the paint scale for the z-values ({@code null} * not permitted)./*from ww w .j a va 2 s .c o m*/ * * @return A buffered image. */ public static BufferedImage createHeatMapImage(HeatMapDataset dataset, PaintScale paintScale) { Args.nullNotPermitted(dataset, "dataset"); Args.nullNotPermitted(paintScale, "paintScale"); int xCount = dataset.getXSampleCount(); int yCount = dataset.getYSampleCount(); BufferedImage image = new BufferedImage(xCount, yCount, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = image.createGraphics(); for (int xIndex = 0; xIndex < xCount; xIndex++) { for (int yIndex = 0; yIndex < yCount; yIndex++) { double z = dataset.getZValue(xIndex, yIndex); Paint p = paintScale.getPaint(z); g2.setPaint(p); g2.fillRect(xIndex, yCount - yIndex - 1, 1, 1); } } return image; }
From source file:com.uksf.mf.core.utility.loaders.ImageLoad.java
/** * Changes colour of all non-transparent pixels for given image to given colour * @param image - image to change colours in * @param newColour colour to change to// w ww . jav a 2 s. c o m * @return image with changed colours */ private static ImageIcon changeImageColour(ImageIcon image, int newColour) { LogHandler.logSeverity(INFO, "Converting image: " + image + " colour to: " + newColour); BufferedImage bufferedImage = new BufferedImage(image.getIconWidth(), image.getIconHeight(), BufferedImage.TYPE_INT_ARGB); Graphics graphics = bufferedImage.createGraphics(); image.paintIcon(null, graphics, 0, 0); graphics.dispose(); for (int x = 0; x < bufferedImage.getWidth(); x++) { for (int y = 0; y < bufferedImage.getHeight(); y++) { int colour = bufferedImage.getRGB(x, y); colour = (((colour >> 24) & 0xff) << 24) | (((colour & 0x00ff0000) >> 16) << 16) | (((colour & 0x0000ff00) >> 8) << 8) | (colour & 0x000000ff); if (colour != COLOUR_TRANSPARENT.getRGB()) { newColour = (((colour >> 24) & 0xff) << 24) | (((newColour & 0x00ff0000) >> 16) << 16) | (((newColour & 0x0000ff00) >> 8) << 8) | (newColour & 0x000000ff); bufferedImage.setRGB(x, y, newColour); } } } return new ImageIcon(bufferedImage); }
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./* ww w.ja v a2 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:game.com.HandleUploadGameThumbServlet.java
public static BufferedImage resizeImage(final Image image, int width, int height) { final BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); final Graphics2D graphics2D = bufferedImage.createGraphics(); graphics2D.setComposite(AlphaComposite.Src); //below three lines are for RenderingHints for better image quality at cost of higher processing time graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); graphics2D.drawImage(image, 0, 0, width, height, null); graphics2D.dispose();//from w ww . j a v a 2 s. c o m return bufferedImage; }