Solved

getPixel() Function

Posted on 2004-04-13
8
795 Views
Last Modified: 2012-05-04
I am trying to write a java program that will get the color of a pixel at a certain x and y coordinate...
i am able to load in the image using JAI but dont know how to go about getting the color of the pixel at the specified coordinates
Does anyone know if it is a built in method in JAI
if not can anyone provide som insight on how to do this, the image is loaded in as a PlanarImage...thanks
0
Comment
Question by:jduawa
  • 4
  • 4
8 Comments
 
LVL 4

Accepted Solution

by:
bkfirebird earned 125 total points
ID: 10817437
PlanarImage pi     // this is your image

// Get the number of bands on the image.  
SampleModel sm = pi.getSampleModel();  
int nbands = sm.getNumBands();  

// We assume that we can get the pixels values in a integer array.  
int[] pixel = new int[nbands];      

// Get an iterator for the image.  
RandomIter iterator = RandomIterFactory.create(pi, null);  

iterator.getPixel(w,h,pixel);   // this will get the pixel at (w,h)

HTH
kiran

0
 
LVL 1

Author Comment

by:jduawa
ID: 10817969
this is what i have...
It is not getting the right color though...i created a sample JPG file that is 3 pixels high by 8 pixels wide and in photoshop i can see what the colors of the pixels are, but the java program is telling me something different
checking pixel 1,1 assuming it is the upper left pixel the java prog tells me

nbands=3
Done!
RED=100
GREEN=93
BLUE=170

photoshop tells me
r=252
g=254
b=255

what you see below is the coldfusion cfx tag that i am using this in...the parameters are being passed to the java by coldfusion parameters...
any other help would be appreciated...

import com.allaire.cfx.*;
import java.awt.image.*;
import javax.media.jai.*;
import javax.media.jai.iterator.*;


public class CFX_GetPixel implements CustomTag{

    public void processRequest(Request request, Response response) throws Exception {
        String loadFilePath = request.getAttribute("loadFilePath");
        String saveFilePath = request.getAttribute("saveFilePath");
        String width = request.getAttribute("xCOORD");
        String height = request.getAttribute("yCOORD");
       
        int x=Integer.parseInt(width);
        int y=Integer.parseInt(height);
       
        PlanarImage pi = (PlanarImage)JAI.create("fileload", loadFilePath);
        // Get the number of bands on the image.  
        SampleModel sm = pi.getSampleModel();  
        int nbands = sm.getNumBands();  
        response.write("nbands="+String.valueOf(nbands)+"<br>");
        // We assume that we can get the pixels values in a integer array.  
        int[] pixel = new int[nbands];      

        // Get an iterator for the image.  
        RandomIter iterator = RandomIterFactory.create(pi, null);  
        //response.write("iterator="+String.valueOf(iterator)+"<br>");
        iterator.getPixel(x,y,pixel);   // this will get the pixel at (w,h)
        response.write("Done!<br>"+"RED="+String.valueOf(pixel[0])+"<BR>"+
            "GREEN="+String.valueOf(pixel[1])+"<BR>"+"BLUE="+String.valueOf(pixel[2]));
    }
}
0
 
LVL 4

Expert Comment

by:bkfirebird
ID: 10818349
i don't see why it is giving a wrong value
can you print x, y before getpixel and make sure
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 1

Author Comment

by:jduawa
ID: 10818398
yes and it is getting the correct value 1,1
does the x,y plane of the image start at 0,0 meaning 0,0 is the most upper left pixel of the image??
0
 
LVL 4

Expert Comment

by:bkfirebird
ID: 10818429
yes, it starts at 0,0
0
 
LVL 1

Author Comment

by:jduawa
ID: 10818444
doesnt solve the problem, but good to know.
I assume then that a pixel in a PlanarImage is the same as 1 pixel in a photoshop jpg file...
0
 
LVL 1

Author Comment

by:jduawa
ID: 10818559
i am now starting to think the problem lies in adobe and it is not a good tool for determining pixel color
because if i put the img i am examining on the web page calling the java and size it to 300x800 it is infact getting the color shown...i will test more tomorrow and award points after testing...
0
 
LVL 4

Expert Comment

by:bkfirebird
ID: 10818585
they should be the same ideally ... i havent really used photoshop much
but you can always tell by manual inspection ...

RED=100
GREEN=93
BLUE=170
would be kinda bluish


photoshop tells me
r=252
g=254
b=255
this is almost pure white
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
windows explorer path to command prompt 5 45
oracle 11g 23 84
Java string replace 11 48
Is there a simpler dropbox system? 10 22
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question