Example usage for java.awt.image BufferedImage getWidth

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

Introduction

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

Prototype

public int getWidth() 

Source Link

Document

Returns the width of the BufferedImage .

Usage

From source file:Main.java

public static BufferedImage joinBufferedImage(BufferedImage img1, BufferedImage img2) {
    int offset = 2;
    int width = img1.getWidth() + img2.getWidth() + offset;
    int height = Math.max(img1.getHeight(), img2.getHeight()) + offset;
    BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = newImage.createGraphics();
    Color oldColor = g2.getColor();
    g2.setPaint(Color.BLACK);/*from   w w w .ja v  a  2  s .  c  om*/
    g2.fillRect(0, 0, width, height);
    g2.setColor(oldColor);
    g2.drawImage(img1, null, 0, 0);
    g2.drawImage(img2, null, img1.getWidth() + offset, 0);
    g2.dispose();
    return newImage;
}

From source file:Main.java

public static void swapColors(BufferedImage img, Color... mapping) {
    int[] pixels = img.getRGB(0, 0, img.getWidth(), img.getHeight(), null, 0, img.getWidth());
    HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
    for (int i = 0; i < mapping.length / 2; i++) {
        map.put(mapping[2 * i].getRGB(), mapping[2 * i + 1].getRGB());
    }//from w w w  .  j a v  a  2 s  .c  om
    for (int i = 0; i < pixels.length; i++) {
        if (map.containsKey(pixels[i]))
            pixels[i] = map.get(pixels[i]);
    }

    img.setRGB(0, 0, img.getWidth(), img.getHeight(), pixels, 0, img.getWidth());
}

From source file:Main.java

public static BufferedImage getFlippedImage(BufferedImage bi) {
    BufferedImage flipped = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
    AffineTransform tran = AffineTransform.getTranslateInstance(0, bi.getHeight());
    AffineTransform flip = AffineTransform.getScaleInstance(1d, -1d);
    tran.concatenate(flip);//w w  w .  j a va  2 s  .  c o m

    Graphics2D g = flipped.createGraphics();
    g.setTransform(tran);
    g.drawImage(bi, 0, 0, null);
    g.dispose();

    return flipped;
}

From source file:Main.java

public static BufferedImage getTransparentImage(BufferedImage image, Color transparent) {
    BufferedImage img = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = img.createGraphics();
    for (int x = 0; x < img.getWidth(); x++) {
        for (int y = 0; y < img.getHeight(); y++) {
            if (image.getRGB(x, y) != transparent.getRGB()) {
                img.setRGB(x, y, image.getRGB(x, y));
            }/*from w w  w.ja v a2 s.co  m*/
        }
    }
    g.dispose();
    return img;
}

From source file:Main.java

public static int getWidthFromImage(String filename) {
    BufferedImage image;
    try {/*from   w  ww  . j ava 2s  .  c  o m*/
        image = ImageIO.read(new File(filename));
        return image.getWidth();
    } catch (IOException e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
    return -1;
}

From source file:BufferedImages.java

/**
 * Returns a {@link BufferedImage} with the specified image type, where the
 * graphical content is a copy of the specified image.
 * /*from  ww w  .ja  v a 2s  . com*/
 * @param img      The image to copy.
 * @param imageType   The image type for the image to return.
 * @return         A copy of the specified image.
 */
public static BufferedImage copy(BufferedImage img, int imageType) {
    int width = img.getWidth();
    int height = img.getHeight();

    BufferedImage newImage = new BufferedImage(width, height, imageType);
    Graphics g = newImage.createGraphics();

    g.drawImage(img, 0, 0, null);

    g.dispose();

    return newImage;
}

From source file:Main.java

public static BufferedImage convert2grey(BufferedImage image) {
    BufferedImage grey = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
    Graphics g = grey.getGraphics();
    g.drawImage(image, 0, 0, null);/*from w ww .  ja v  a  2 s.  c om*/
    g.dispose();
    BufferedImage rgb = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
    g = rgb.getGraphics();
    g.drawImage(grey, 0, 0, null);
    g.dispose();
    return rgb;
}

From source file:Main.java

public static int[] columnChecksums(BufferedImage img) {

    int[] sums = new int[img.getWidth()];
    Arrays.fill(sums, 0);// w  ww  . j a  v  a2  s . c o  m

    for (int y = 0; y < img.getHeight(); y++) {
        for (int x = 0; x < img.getWidth(); x++) {
            final int colour = img.getRGB(x, y);
            sums[x] += (int) ((colour & 0x00ff0000) >> 16);
            sums[x] += (int) ((colour & 0x0000ff00) >> 8);
            sums[x] += (int) (colour & 0x000000ff);
        }
    }

    for (int x = 0; x < img.getWidth(); x++) {
        sums[x] %= 256;
    }

    return sums;
}

From source file:Main.java

private static BufferedImage prepareImage(BufferedImage image, int shadowSize) {
    BufferedImage subject = new BufferedImage(image.getWidth() + shadowSize * 2,
            image.getHeight() + shadowSize * 2, BufferedImage.TYPE_INT_ARGB);

    Graphics2D g2 = subject.createGraphics();
    g2.drawImage(image, null, shadowSize, shadowSize);
    g2.dispose();/*from   w  w w . j  ava 2  s  .  co  m*/

    return subject;
}

From source file:Utils.java

/**
 * Produces a copy of the supplied image
 *
 * @param image The original image/*from   www . jav a 2  s . c om*/
 * @return The new BufferedImage
 */
public static BufferedImage copyImage(BufferedImage image) {
    return scaledImage(image, image.getWidth(), image.getHeight());
}