Results 1 to 13 of 13
- 06-16-2012, 07:51 PM #1
Member
- Join Date
- Jun 2012
- Posts
- 8
- Rep Power
- 0
How to get individual pixel colors ?
I want to know alph, red, blue, and green values of pixels in buffered image. Following displays the picture correctly, But all red blue and green values are always zero. Can anybody guide me how to get actual values :
Java Code:import java.awt.*; import java.awt.image.*; import javax.swing.*; public class BuffIt { public static void main (String args[]) { // Get Image ImageIcon icon = new ImageIcon(args[0]); Image image = icon.getImage(); int totval = 0; int picval; int rd, bl, gr; picval = 0; int i,j,w,h, clr; // Create empty BufferedImage, sized to Image BufferedImage buffImage = new BufferedImage( image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB); w = image.getWidth(null); h = image.getHeight(null); System.out.println("width : " + Integer.toString(w)); System.out.println("height : " + Integer.toString(h)); for (j=0;j<h;j++) { for (i=0;i<w;i++) { clr = buffImage.getRGB(i,j); rd = (clr & 0x00ff0000) >> 16; gr = (clr & 0x0000ff00) >> 8; bl = clr & 0x000000ff; picval = ( picval + rd + gr + bl); if ( rd > 0 || gr > 0 || bl > 0 ) { System.out.println("red : " + Integer.toString(rd)); System.out.println("green : " + Integer.toString(gr)); System.out.println("blue : " + Integer.toString(bl)); System.exit(0); } } } System.out.println("picval : " + Integer.toString(picval)); totval = (totval + picval ) & 0xffffffff; System.out.println("totval: " + Integer.toString(totval)); // Draw Image into BufferedImage Graphics g = buffImage.getGraphics(); g.drawImage(image, 0, 0, null); // Show success JFrame frame = new JFrame(); JLabel label = new JLabel(new ImageIcon(buffImage)); frame.getContentPane().add(label); frame.pack(); frame.show(); } }
Please helpLast edited by Fubarable; 06-16-2012 at 07:59 PM. Reason: code tags added
-
Re: How to get individual pixel colors ?
Look at the order that you do things:
- When do you create the BufferedImage?
- When do you extract the numeric data from the BufferedImage?
- When do you draw to the BufferedImage?
- 06-17-2012, 06:42 PM #3
Re: How to get individual pixel colors ?
Also posted at Can not get individual pixel values using getRGB
If you don't understand my response, don't ignore it, ask a question.
-
Re: How to get individual pixel colors ?
Posted today and he never replied to my post. I must thank him for allowing me to waste my time on him.
- 06-17-2012, 09:25 PM #5
Re: How to get individual pixel colors ?
It's all over the net:
https://forums.oracle.com/forums/thr...readID=2403002
Can not get individual pixel values (Swing / AWT / SWT / JFace forum at JavaRanch)
And the code is adapted from a 12 year old sample:
http://www.jguru.com/faq/view.jsp?EID=53328
dbLast edited by DarrylBurke; 06-17-2012 at 09:28 PM.
Why do they call it rush hour when nothing moves? - Robin Williams
- 06-18-2012, 06:11 AM #6
Member
- Join Date
- Jun 2012
- Posts
- 8
- Rep Power
- 0
Re: How to get individual pixel colors ?
Sorry Fuberble, I dd not reply to your querry
First ImageIcon icon = new ImageIcon(args[0]);
Then I create buff image BufferedImage buffImage =
new BufferedImage(
image.getWidth(null),
image.getHeight(null),
BufferedImage.TYPE_INT_ARGB);
w = image.getWidth(null);
After that I rey to read pixel data.
Then I draw image.
Actually, drawing of image has nothing to do wuth getRGB.
In my other app i capture image from webcam without showing it ( no gui) and save it in jpg format.
When buff image is created using Image all data is lready there
I shall see other sources mentioned above
- 06-18-2012, 06:22 AM #7
Member
- Join Date
- Jun 2012
- Posts
- 8
- Rep Power
- 0
Re: How to get individual pixel colors ?
Dear DarryBurke,
I am sorry to post the same question on diff. forums. But the reason is that I am till not getting the answer. The old code you have mentioned is just for display. For getting pixels values I have adopted code from RoseIndia. Despite my best efforts, I am unable to get the values and I need them to write a geiger counter program.
Please show me the way how to do it.
- 06-18-2012, 11:24 AM #8
- 06-18-2012, 03:08 PM #9
Re: How to get individual pixel colors ?
With changes as per Fubarable post#2 I get:
width : 55
height : 68
clr=ffb8b8b0, j=1, i=40
red : 184, green : 184, blue : 176
clr=ff706868, j=1, i=41
red : 112, green : 104, blue : 104
clr=ff202428, j=2, i=40
red : 32, green : 36, blue : 40
clr=ff201018, j=2, i=41
red : 32, green : 16, blue : 24
clr=ff383030, j=2, i=42
red : 56, green : 48, blue : 48
clr=ff505850, j=3, i=39
red : 80, green : 88, blue : 80
clr=ff181010, j=3, i=40
red : 24, green : 16, blue : 16
clr=ff201818, j=3, i=41
red : 32, green : 24, blue : 24
clr=ff282028, j=3, i=42
red : 40, green : 32, blue : 40
clr=ff181010, j=4, i=39
...If you don't understand my response, don't ignore it, ask a question.
- 06-23-2012, 06:44 PM #10
Member
- Join Date
- Jun 2012
- Posts
- 8
- Rep Power
- 0
Re: How to get individual pixel colors ?
When instead of
BufferedImage buffImage =
new BufferedImage(
image.getWidth(null),
image.getHeight(null),
BufferedImage.TYPE_INT_ARGB);
I used
File file= new File(args[0]);
buffImage = ImageIO.read(file);
It worked and gave me correct values of red, green blue !
- 06-23-2012, 06:53 PM #11
Re: How to get individual pixel colors ?
The important thing was to have some image with colors in the buffImage variable. Your earlier code didn't.
If you don't understand my response, don't ignore it, ask a question.
- 06-23-2012, 09:28 PM #12
Member
- Join Date
- Jun 2012
- Posts
- 8
- Rep Power
- 0
Re: How to get individual pixel colors ?
Dear Norm.
Thanks you.
I did not understand how the program works for you.
First two lines of my program create image by accepting file name on command line.
ImageIcon icon = new ImageIcon(args[0]);
Image image = icon.getImage();
That is why the image is displayed, but with BufferImage it still gives me all zeros ifI do not use
File file= new File(args[0]);
buffImage = ImageIO.read(file);
I have tried by first displaying values and then using getRGB but it did not help.
Will you please show me the entire code which shows you correct pixel values ?
Thanking you once again.
-Mahesh Chavan
- 06-23-2012, 09:36 PM #13
Similar Threads
-
Scanning Image Pixel by Pixel
By the_transltr in forum Advanced JavaReplies: 5Last Post: 08-28-2012, 04:01 PM -
Controlling individual threads
By youngstorm in forum Threads and SynchronizationReplies: 3Last Post: 12-03-2010, 10:37 PM -
* vs. individual package name?
By XmisterIS in forum New To JavaReplies: 3Last Post: 09-01-2010, 12:19 PM -
compare two images pixel by pixel
By java_bond in forum Advanced JavaReplies: 6Last Post: 03-02-2010, 11:27 AM -
Calculating Individual Numbers
By TheKnight in forum New To JavaReplies: 2Last Post: 01-30-2009, 12:51 AM


LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks