Solved

getPixel() Function

Posted on 2004-04-13
8
806 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Groovy problem when using SOAPUI : DispatchException occurred 7 58
hibernate insert example 13 34
jmss example java 2 23
java mysql insert application 14 25
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…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

791 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