Example usage for java.awt.image BufferedImage BufferedImage

List of usage examples for java.awt.image BufferedImage BufferedImage

Introduction

In this page you can find the example usage for java.awt.image BufferedImage BufferedImage.

Prototype

public BufferedImage(int width, int height, int imageType) 

Source Link

Document

Constructs a BufferedImage of one of the predefined image types.

Usage

From source file:GraphicsUtil.java

public static BufferedImage createImage(int width, int height) {
    return new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
}

From source file:com.igormaznitsa.mindmap.swing.panel.utils.MiscIcons.java

@Nullable
private static Image loadImage(@Nonnull final String name) {
    if ("empty".equals(name)) {
        final BufferedImage result = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
        return result;
    }/*from   w w  w . j a va  2s .  c om*/
    final InputStream in = MiscIcons.class
            .getResourceAsStream("/com/igormaznitsa/mindmap/swing/miscicons/" + name + ".png");
    if (in == null) {
        return null;
    }
    try {
        return ImageIO.read(in);
    } catch (IOException ex) {
        LOGGER.error("IO exception for icon '" + name + '\'');
        return null;
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:TexturedText.java

private BufferedImage getTextureImage() {
    // Create the test image.
    int size = 8;
    BufferedImage bi = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bi.createGraphics();
    g2.setPaint(Color.red);/* w ww . j a  v a  2s. co m*/
    g2.fillRect(0, 0, size / 2, size / 2);
    g2.setPaint(Color.yellow);
    g2.fillRect(size / 2, 0, size, size / 2);
    g2.setPaint(Color.green);
    g2.fillRect(0, size / 2, size / 2, size);
    g2.setPaint(Color.blue);
    g2.fillRect(size / 2, size / 2, size, size);
    return bi;
}

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  . j ava 2s  .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:Utils.java

public static BufferedImage createGradientMask(int width, int height, int orientation) {
    // algorithm derived from Romain Guy's blog
    BufferedImage gradient = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = gradient.createGraphics();
    GradientPaint paint = new GradientPaint(0.0f, 0.0f, new Color(1.0f, 1.0f, 1.0f, 1.0f),
            orientation == SwingConstants.HORIZONTAL ? width : 0.0f,
            orientation == SwingConstants.VERTICAL ? height : 0.0f, new Color(1.0f, 1.0f, 1.0f, 0.0f));
    g.setPaint(paint);//w  w  w .j  av  a 2s  .  c o m
    g.fill(new Rectangle2D.Double(0, 0, width, height));

    g.dispose();
    gradient.flush();

    return gradient;
}

From source file:probe.com.model.util.SwingToImageGenerator.java

public String generateHeatMap(JComponent component) {

    BufferedImage heatMapImg = new BufferedImage((component.getWidth()), (component.getHeight()),
            BufferedImage.TYPE_INT_ARGB);
    Graphics g = heatMapImg.getGraphics();
    component.paint(g);//from   ww w  .ja  va  2s .  co m
    return generateEncodedImg(heatMapImg);
}

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// w w  w.  jav a  2  s . c om
 */
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:com.chinarewards.gwt.license.util.ImageDisposeUtil.java

/**
 * @param imgsrc   /* w  w w . j av  a  2 s . c om*/
 * @param imgdist ? 
 * @param widthdist  
 * @param heightdist 
 * @param int benchmark :0,12
 * 
 */
public static void reduceImg(String imgsrc, String imgdist, int widthdist, int heightdist, int benchmark) {

    try {
        // System.out.println("*******widthdist********:"+widthdist);
        // System.out.println("*******heightdist********:"+heightdist);
        // System.out.println("*******benchmark********:"+benchmark);
        File srcfile = new File(imgsrc);
        if (!srcfile.exists()) {
            return;
        }
        Image src = javax.imageio.ImageIO.read(srcfile);
        int width = src.getWidth(null);
        int height = src.getHeight(null);
        if (width <= widthdist && height <= heightdist) {
            // SysUtil.cpoyFile(imgsrc, imgdist);
            FileUtils.copyFile(new File(imgsrc), new File(imgdist));
            return;
        }
        // 
        float wh = (float) width / (float) height;
        if (benchmark == 0) {
            if (wh > 1) {
                float tmp_heigth = (float) widthdist / wh;
                BufferedImage tag = new BufferedImage(widthdist, (int) tmp_heigth, BufferedImage.TYPE_INT_RGB);
                tag.getGraphics().drawImage(src, 0, 0, widthdist, (int) tmp_heigth, null);
                FileOutputStream out = new FileOutputStream(imgdist);
                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                encoder.encode(tag);
                out.close();
            } else {
                float tmp_width = (float) heightdist * wh;
                BufferedImage tag = new BufferedImage((int) tmp_width, heightdist, BufferedImage.TYPE_INT_RGB);
                tag.getGraphics().drawImage(src, 0, 0, (int) tmp_width, heightdist, null);
                FileOutputStream out = new FileOutputStream(imgdist);
                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                encoder.encode(tag);
                out.close();
            }
        }
        if (benchmark == 1) {
            float tmp_heigth = (float) widthdist / wh;
            BufferedImage tag = new BufferedImage(widthdist, (int) tmp_heigth, BufferedImage.TYPE_INT_RGB);
            tag.getGraphics().drawImage(src, 0, 0, widthdist, (int) tmp_heigth, null);
            FileOutputStream out = new FileOutputStream(imgdist);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            encoder.encode(tag);
            out.close();
        }
        if (benchmark == 2) {
            float tmp_width = (float) heightdist * wh;
            BufferedImage tag = new BufferedImage((int) tmp_width, heightdist, BufferedImage.TYPE_INT_RGB);
            tag.getGraphics().drawImage(src, 0, 0, (int) tmp_width, heightdist, null);
            FileOutputStream out = new FileOutputStream(imgdist);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            encoder.encode(tag);
            out.close();
        }
    } catch (IOException ex) {
        logger.error(ex);
    }
}

From source file:com.l1j5.web.common.utils.ImageUtils.java

public static void getImageThumbnail(BufferedInputStream stream_file, String save, String type, int w, int h) {

    try {//from   w  w  w . ja  v a 2s  .c  o m
        File file = new File(save);
        BufferedImage bi = ImageIO.read(stream_file);

        int width = bi.getWidth();
        int height = bi.getHeight();
        if (w < width) {
            width = w;
        }
        if (h < height) {
            height = h;
        }

        BufferedImage bufferIm = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Image atemp = bi.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING);

        Graphics2D g2 = bufferIm.createGraphics();
        g2.drawImage(atemp, 0, 0, width, height, null);
        ImageIO.write(bufferIm, type, file);
    } catch (Exception e) {
        log.error(e);
    }

}

From source file:com.openkm.util.ImageUtils.java

/**
 * cloneImage/*w  w  w  . j a  va  2s.c  om*/
 */
public static BufferedImage clone(BufferedImage source) {
    BufferedImage img = new BufferedImage(source.getWidth(), source.getHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics g = img.getGraphics();
    g.drawImage(source, 0, 0, null);
    g.dispose();
    return img;
}