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:costumetrade.common.verify.ImageVerification.java

public Pair<String, BufferedImage> create() {
    int x = 0, fontHeight = 0, codeY = 0;
    int red = 0, green = 0, blue = 0;

    x = width / (codeCount + 2);//?
    fontHeight = height - 2;//
    codeY = height - 4;/*from w  w  w .jav  a2  s  . c om*/

    // ?buffer
    BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = buffImg.createGraphics();
    // ?
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, width, height);
    // 
    g.setFont(new Font("Arial", Font.PLAIN, fontHeight));

    for (int i = 0; i < lineCount; i++) {

        int xs = RandomUtils.nextInt(0, width);
        int ys = RandomUtils.nextInt(0, height);
        int xe = xs + RandomUtils.nextInt(0, width / 8);
        int ye = ys + RandomUtils.nextInt(0, height / 8);
        red = RandomUtils.nextInt(0, 255);
        green = RandomUtils.nextInt(0, 255);
        blue = RandomUtils.nextInt(0, 255);
        g.setColor(new Color(red, green, blue));
        g.drawLine(xs, ys, xe, ye);
    }

    // randomCode???
    StringBuilder randomCode = new StringBuilder();
    // ?codeCount??
    for (int i = 0; i < codeCount; i++) {
        String strRand = String.valueOf(codeSequence[RandomUtils.nextInt(0, codeSequence.length)]);
        // ????
        red = RandomUtils.nextInt(0, 255);
        green = RandomUtils.nextInt(0, 255);
        blue = RandomUtils.nextInt(0, 255);
        g.setColor(new Color(red, green, blue));
        g.drawString(strRand, (i + 1) * x, codeY);
        // ??
        randomCode.append(strRand);
    }
    // ????Session
    return new Pair<String, BufferedImage>(randomCode.toString(), buffImg);
}

From source file:com.alibaba.webx.tutorial.app1.module.screen.simple.SayHiImage.java

private void writeImage(OutputStream out) throws IOException {
    BufferedImage img = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = img.createGraphics();

    g2d.setPaint(Color.red);/* www  .j  av a 2  s .co m*/
    g2d.setFont(new Font("Serif", Font.BOLD, 36));
    g2d.drawString("Hi there, how are you doing today?", 5, g2d.getFontMetrics().getHeight());
    g2d.dispose();

    ImageIO.write(img, "jpg", out);
}

From source file:com.xebia.visualreview.PixelComparator.java

private static BufferedImage generateMask(java.util.Map maskInfo, int width, int height) {
    BufferedImage maskImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Rect fullRect = new Rect(0, 0, width, height);
    if (maskInfo != null) {
        java.util.List<java.util.Map> excludeszones = (java.util.List<java.util.Map>) maskInfo
                .get(Keyword.find("excludeZones"));
        if (excludeszones != null) {
            for (int i = 0; i < excludeszones.size(); i++) {
                java.util.Map excludeZone = excludeszones.get(i);
                Rect rect = new Rect(excludeZone, width, height);
                rect.applyToImage(maskImage, DIFFCOLOUR);
            }// w  w w. j  av a 2  s  . co  m
        }
    }
    return maskImage;
}

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 . ja v  a  2 s  .  com
 *
 * @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:cpcc.ros.sim.osm.Camera.java

/**
 * Initialize the tile map./*from   w  w w  .  j  a  v  a  2s  .  c  om*/
 */
private void initMap() {
    map = new BufferedImage(mapWidth * cfg.getTileWidth(), mapHeight * cfg.getTileHeight(),
            BufferedImage.TYPE_INT_RGB);

    Graphics2D g2d = map.createGraphics();
    tiles = new Graphics[mapWidth][mapHeight];

    for (int w = 0; w < mapWidth; ++w) {
        for (int h = 0; h < mapHeight; ++h) {
            tiles[w][h] = g2d.create(w * cfg.getTileWidth(), h * cfg.getTileHeight(), cfg.getTileWidth(),
                    cfg.getTileHeight());
        }
    }
}

From source file:Main.java

@Override
public void paint(Graphics g, JComponent c) {
    JLayer jlayer = (JLayer) c;
    JProgressBar progress = (JProgressBar) jlayer.getView();
    int w = progress.getSize().width;
    int h = progress.getSize().height;

    if (bi == null || w != prevw || h != prevh) {
        bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    }/* ww  w . j  ava 2s.  c o  m*/
    prevw = w;
    prevh = h;

    Graphics2D g2 = bi.createGraphics();
    super.paint(g2, c);
    g2.dispose();

    Image image = c.createImage(new FilteredImageSource(bi.getSource(), new RedGreenChannelSwapFilter()));
    g.drawImage(image, 0, 0, c);
}

From source file:RasterImageTest.java

/**
 * Makes the Mandelbrot image./*from   ww w  .j  av a  2s .  c o m*/
 * @param width the width
 * @parah height the height
 * @return the image
 */
public BufferedImage makeMandelbrot(int width, int height) {
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    WritableRaster raster = image.getRaster();
    ColorModel model = image.getColorModel();

    Color fractalColor = Color.red;
    int argb = fractalColor.getRGB();
    Object colorData = model.getDataElements(argb, null);

    for (int i = 0; i < width; i++)
        for (int j = 0; j < height; j++) {
            double a = XMIN + i * (XMAX - XMIN) / width;
            double b = YMIN + j * (YMAX - YMIN) / height;
            if (!escapesToInfinity(a, b))
                raster.setDataElements(i, j, colorData);
        }
    return image;
}

From source file:fr.pasteque.pos.sales.restaurant.Floor.java

public Floor(JSONObject o) {
    this.m_sID = o.getString("id");
    this.m_sName = o.getString("label");
    // TODO restore background image
    BufferedImage img = new BufferedImage(640, 480, BufferedImage.TYPE_INT_ARGB);
    ThumbNailBuilder tnbcat = new ThumbNailBuilder(32, 32, defimg);
    m_container = new JPanelDrawing(img);
    m_icon = new ImageIcon(tnbcat.getThumbNail(img));
}

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./*  w ww  .  ja  va 2  s .com*/
 * 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:net.roboconf.doc.generator.internal.GraphUtils.java

/**
 * Computes the width of a shape for a given component or facet.
 * @param type a type/*from w  w  w  . j a  va 2  s  .  c  o  m*/
 * @return the width it should take once displayed as a graph vertex
 */
public static int computeShapeWidth(AbstractType type) {

    Font font = GraphUtils.getDefaultFont();
    BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
    FontMetrics fm = img.getGraphics().getFontMetrics(font);

    int width = fm.stringWidth(type.getName());
    width = Math.max(width, 80) + 20;
    return width;
}