?
Solved

getPixel() Function

Posted on 2004-04-13
8
Medium Priority
?
832 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 4

Accepted Solution

by:
bkfirebird earned 500 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month15 days, 7 hours left to enroll

741 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