Solved

getPixel() Function

Posted on 2004-04-13
8
813 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 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
Technology Partners: 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
xampp tool 12 59
use lov values 2 72
jboss wildfly 10.1 10 427
ejb example issues 3 53
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

733 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