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 void main(String[] args) throws IOException {
    InputStream stream = Main.class.getResourceAsStream("test.png");
    BufferedImage image = ImageIO.read(stream);
    int iw = image.getWidth();
    int ih = image.getHeight();
    stream.close();//from  w w  w  .  ja  v a2  s. c o  m

    for (int y = 0; y < ih; y++) {
        for (int x = 0; x < iw; x++) {
            int pixel = image.getRGB(x, y);
            int alpha = (pixel >> 24) & 0xFF;
            int red = (pixel >> 16) & 0xFF;
            int green = (pixel >> 8) & 0xFF;
            int blue = pixel & 0xFF;
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.java2s.com/style/download.png");
    final BufferedImage bi = ImageIO.read(url);
    final String size = bi.getWidth() + "x" + bi.getHeight();
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JLabel l = new JLabel(size, new ImageIcon(bi), SwingConstants.RIGHT);
            JOptionPane.showMessageDialog(null, l);
        }/* w  w w .  j  av  a2 s.c om*/
    });
}

From source file:Main.java

public static void main(final String args[]) throws Exception {
    URL url = new URL("http://www.java2s.com/style/download.png");
    BufferedImage image = ImageIO.read(url);

    for (int x = 0; x < image.getWidth(); x++) {
        for (int y = 0; y < image.getHeight(); y++) {
            int clr = image.getRGB(x, y);
            int red = (clr & 0x00ff0000) >> 16;
            int green = (clr & 0x0000ff00) >> 8;
            int blue = clr & 0x000000ff;

            System.out.println("Red Color value = " + red);
            System.out.println("Green Color value = " + green);
            System.out.println("Blue Color value = " + blue);
        }//from  w ww  .  j  ava 2 s  .c  o m
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.java2s.com/style/download.png");
    BufferedImage image = ImageIO.read(url);

    int w = image.getWidth();
    int h = image.getHeight();
    Ellipse2D.Double ellipse1 = new Ellipse2D.Double(10, 10, 20, 30);
    Ellipse2D.Double ellipse2 = new Ellipse2D.Double(15, 15, 20, 30);
    Area circle = new Area(ellipse1);
    circle.subtract(new Area(ellipse2));

    BufferedImage result = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = result.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
    g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g.setClip(circle);//from w  ww  . ja va  2  s .  c  o m
    g.drawImage(image, 0, 0, null);
    g.dispose();

    ImageIO.write(result, "png", new File("result.png"));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    BufferedImage inputFile = ImageIO.read(new URL("http://www.java2s.com/style/download.png"));

    for (int x = 0; x < inputFile.getWidth(); x++) {
        for (int y = 0; y < inputFile.getHeight(); y++) {
            int rgba = inputFile.getRGB(x, y);
            Color col = new Color(rgba, true);
            col = new Color(255 - col.getRed(), 255 - col.getGreen(), 255 - col.getBlue());
            inputFile.setRGB(x, y, col.getRGB());
        }//ww w.  j a  v  a 2s  .c o  m
    }

    File outputFile = new File("invert.png");
    ImageIO.write(inputFile, "png", outputFile);

}

From source file:Main.java

public static void main(String args[]) throws IOException {
    File file = new File("your file path.jpg");
    BufferedImage image = ImageIO.read(file);

    for (int i = 0; i < image.getHeight(); i++) {
        for (int j = 0; j < image.getWidth(); j++) {
            int color = image.getRGB(j, i);
            // convert the color to a readable format
            String clr = Integer.toHexString(color).substring(2);
            System.out.println(clr);
        }/* ww w.j  a  v a  2s . c om*/
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.java2s.com/style/download.png");

    final BufferedImage originalImage = ImageIO.read(url);
    int width = originalImage.getWidth();
    int height = originalImage.getHeight();
    final BufferedImage textImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = textImage.createGraphics();

    FontRenderContext frc = g.getFontRenderContext();
    Font font = new Font("Arial", Font.BOLD, 50);
    GlyphVector gv = font.createGlyphVector(frc, "java2s.com");

    int xOff = 0;
    int yOff = 50;

    Shape shape = gv.getOutline(xOff, yOff);
    g.setClip(shape);/*from   w w w  .  ja  v a  2 s  .c  om*/
    g.drawImage(originalImage, 0, 0, null);

    g.setStroke(new BasicStroke(2f));
    g.setColor(Color.BLACK);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.draw(shape);
    g.dispose();

    ImageIO.write(textImage, "png", new File("cat-text.png"));

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(textImage)));
        }
    });
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.java2s.com/style/download.png");
    final BufferedImage image = ImageIO.read(url);
    int x = 50;//w ww. j a v  a2  s .  c  o m
    final Image crop = image.getSubimage(x, 0, image.getWidth() - x, image.getHeight());
    Runnable r = new Runnable() {
        @Override
        public void run() {
            JPanel gui = new JPanel();
            gui.add(new JLabel(new ImageIcon(image)), BorderLayout.LINE_START);
            gui.add(new JLabel("java2s.com"));
            gui.add(new JLabel(new ImageIcon(crop)), BorderLayout.LINE_END);
            JOptionPane.showMessageDialog(null, gui);
        }
    };
    SwingUtilities.invokeLater(r);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    BufferedImage img = ImageIO.read(new File("c:/Java_Dev/a.jpg"));
    int height = img.getHeight();
    int width = img.getWidth();

    System.out.println(height + "  " + width + " " + img.getRGB(30, 30));

    for (int h = 1; h < height; h++) {
        for (int w = 1; w < width; w++) {
            int rgb = img.getRGB(w, h);
            int red = (rgb >> 16) & 0x000000FF;
            int green = (rgb >> 8) & 0x000000FF;
            int blue = (rgb) & 0x000000FF;

            if (red == 0 && green == 0 && blue == 0) {
                System.out.println("Black");
            }//w w  w.java2  s. c o m
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    BufferedImage master = ImageIO.read(new URL("http://www.java2s.com/style/download.png"));
    BufferedImage gray = new BufferedImage(master.getWidth(), master.getHeight(), BufferedImage.TYPE_INT_ARGB);

    ColorConvertOp op = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
    op.filter(master, gray);//from   w  w  w .j  av a2s  .  co m

    // new JLabel(new ImageIcon(master));
    // new JLabel(new ImageIcon(gray));
    ImageIO.write(master, "png", new File("c:/Java_Dev/master.png"));
    ImageIO.write(gray, "png", new File("c:/Java_Dev/gray.png"));
}