Tuesday 3 December 2013

BufferedImage pixel value issue RGB Value for each PIXEL

package com..jpt.dream.miscl;

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.imageio.ImageIO;

import java.net.URL;

class ImageTest {

public static int getRGB(int x, int y, BufferedImage image) {
return image.getRGB(x, y);
}

public static Color getColor(int x, int y, BufferedImage image) {
int rgb = image.getRGB(x, y);
Color c = new Color(rgb);
return c;
}

public static void main(String[] args) throws Exception {
BufferedImage bi = ImageIO
.read(new URL("http://i.imgur.com/WMfeU.png"));
int w = bi.getWidth();
int h = bi.getHeight();

final BufferedImage bi2 = new BufferedImage(w, h,
BufferedImage.TYPE_INT_RGB);

for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
int rgb = getRGB(x, y, bi);
if (x % 20 == 0 && y % 20 == 0) {
System.out.println(getColor(x, y, bi));
}
bi2.setRGB(x, y, rgb);
}
}

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

}
}

No comments: