Solved

getPixel() Function

Posted on 2004-04-13
8
783 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now