Jump to content

Pixel Color!?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
12 replies to this topic

#1
DivideByZero

DivideByZero

    Learning Programmer

  • Members
  • PipPipPip
  • 35 posts
Hello CodeCall forums! I joined here today for one question only and if it goes well I might as well stay.

In Java, how can I find the color of a pixel at a given point?

something like finding pixel[20][40] = Color.(30,0,100); //pixel (20,40) is the color (30,0,100)

Please help!!! I've been frustrated with this problem for a while!! :irritated::irritated::irritated:

#2
Arkie

Arkie

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts

DivideByZero said:

Hello CodeCall forums! I joined here today for one question only and if it goes well I might as well stay.

In Java, how can I find the color of a pixel at a given point?

something like finding pixel[20][40] = Color.(30,0,100); //pixel (20,40) is the color (30,0,100)

Please help!!! I've been frustrated with this problem for a while!! :irritated::irritated::irritated:


I rewrote this one from a tutorial here: Get the color of a specific pixel - Real's Java How-to


should be what u were looking for, compile it and have fun with it.

import javax.imageio.ImageIO;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class PixelColor {
public static void main(String[] args) {
File file = new File("c:\\2.bmp");
BufferedImage image;
try {
int x = 50;
int y = 50;
image = ImageIO.read(file);
int c = image.getRGB(x,y);
int red = (c & 0x00ff0000) >> 16;
int green = (c & 0x0000ff00) >> 8;
int blue = c & 0x000000ff;
Color c1 = new Color(red,green,blue);
System.out.println("Pixel value at X:"+ x + " Y:" + y);
System.out.println("R:" +c1.getRed() + " G:" + c1.getGreen() + " B:"+ c1.getBlue());
} catch (IOException e) {
e.printStackTrace();
}
}
}

hm.. no syntax highlighting?

#3
DivideByZero

DivideByZero

    Learning Programmer

  • Members
  • PipPipPip
  • 35 posts
Thanks for the reply!

I compiled the code you gave me. I changed
File file = new File("");
to
File file = new File("stapler.jpg"); //my picture is stapler.jpg

Now it compiled perfectly but when I ran the program, I got this error:
Posted Image
http://img98.imagesh...86/errordr8.jpg


Help!!!

#4
Arkie

Arkie

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts
I didn't wrote an applet but i could if you want, are you using an IDE to run it? If not open your cli (run -> cmd in windowsXP) and typ:
javac PixelColor.java
java PixelColor

It should output something like this:
Pixel value at X:50 Y:50
R:255 G:0 B:0

#5
DivideByZero

DivideByZero

    Learning Programmer

  • Members
  • PipPipPip
  • 35 posts
Once again, thank you so much for the quick reply!
I am using TextPad as my IDE.
I did as you said and I got these results:
Posted Image
http://img98.imagesh...2/error2tx2.jpg

Does this code you posted work for you? Please try. If not can you please give me a code that works. Thank you so much. If it works I am forever grateful to you!!

#6
Arkie

Arkie

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts

DivideByZero said:

Once again, thank you so much for the quick reply!
I am using TextPad as my IDE.
I did as you said and I got these results:
Posted Image
http://img98.imagesh...2/error2tx2.jpg

Does this code you posted work for you? Please try. If not can you please give me a code that works. Thank you so much. If it works I am forever grateful to you!!


Seems to me its a classpath problem but try to open textpad and use the combination CTRL + 2
or do it in the CLI box again but now in the c:\ root with the PixelColor.java copied there.

//edit
quote-> Does this code you posted work for you?

Although i usually give out hints to people how to program so that they can solve it themselfs i don't post applications that doesn't compile.

#7
DivideByZero

DivideByZero

    Learning Programmer

  • Members
  • PipPipPip
  • 35 posts
I LOVE YOU ARKIE!!!!!
It worked! Arike you're the best! I updated to jdk1.5.0_14 and now it worked with the code you gave me!

I'll keep my promise and be a regular member of this forum.

#8
Arkie

Arkie

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts
np

now im going to bed. It's 3.30 AM here :)
Happy 2008!

#9
DivideByZero

DivideByZero

    Learning Programmer

  • Members
  • PipPipPip
  • 35 posts
yeah you too. Happy new year!

#10
DivideByZero

DivideByZero

    Learning Programmer

  • Members
  • PipPipPip
  • 35 posts
How would I find the WIDTH and HEIGHT of the image I'm using?

I tried
int width = getSize().width;
and
int width=image.getWidth(this);
and also
int width=image.getWidth(null);

but none of those work. Whats the code I should use to get the width and height?

#11
Arkie

Arkie

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts

DivideByZero said:

How would I find the WIDTH and HEIGHT of the image I'm using?

I tried
int width = getSize().width;
and
int width=image.getWidth(this);
and also
int width=image.getWidth(null);

but none of those work. Whats the code I should use to get the width and height?

I happened to check my mailbox once more before actually goign to bed and i saw your reply. Give me a few min and ill write you an example.

#12
Arkie

Arkie

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts
Use this one

import javax.imageio.ImageIO;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;

// I rewrote it from a tutorial here: Get the color of a specific pixel - Real's Java How-to
// Ark - www.edited out my site due spam.. .com , the application shows the RGB value from an X,Y coordinate of an image
// this version reads the user input for the X Y coordinates and shows the width and height of the image.

public class PixelColor {
public static void main(String[] args) {
PixelColor PC = new PixelColor();
PC.getImageInfo();
}

public void getImageInfo(){
File file = new File("c:\\tmp.bmp");
BufferedImage image;
try {
Scanner sc = new Scanner(System.in);
System.out.print("Get X,Y coordinate a from image: " );
String[] xY = sc.nextLine().split("\\,");
int x = Integer.parseInt(xY[0]);
int y = Integer.parseInt(xY[1]);

image = ImageIO.read(file);
int c = image.getRGB(x,y);
int red = (c & 0x00ff0000) >> 16;
int green = (c & 0x0000ff00) >> 8;
int blue = c & 0x000000ff;
Color c1 = new Color(red,green,blue);
System.out.println("Pixel value at X:"+ x + " Y:" + y);
System.out.println("R:" +c1.getRed() + " G:" + c1.getGreen() + " B:"+ c1.getBlue());
System.out.println("Width of Image:" + image.getWidth() + " Height of Image:" + image.getHeight());

} catch (NumberFormatException NFE) {
System.out.println("\nYour input doesn't match.. example: 15,20");
System.out.println("");
PixelColor PC = new PixelColor();
PC.getImageInfo();
} catch (IOException e) {
System.out.println("\nNo image available to read.");
PixelColor PC = new PixelColor();
PC.getImageInfo();
}
catch(ArrayIndexOutOfBoundsException AIOOBE){
System.out.println("\nx/y value is larger than the image size");
PixelColor PC = new PixelColor();
PC.getImageInfo();
}
}
}