Solved

About imaging with Java

Posted on 2002-05-06
6
208 Views
Last Modified: 2010-03-31
I have this small image (MyImage.gif), and I need to load that image in memory and perform some changes by accessing its pixels (for example I need to change all red pixels to blue). So far, I can get the Image like this:

    try
    {

      img = getToolkit().getImage("D:\\DownLoads\\images\\MyImage.gif");
     
    }
    catch(Exception ex)
    {
      System.out.println("Exception was thrown in action performed : "+ex.toString());
    }

But, it seems that I can do nothing with my image other than display it. I need to be able to look into its pixels analyze them and change them if I want to.

Any help with this??
Thanks a lot in advance!
0
Comment
Question by:dyma82
  • 3
  • 2
6 Comments
 
LVL 18

Accepted Solution

by:
bobbit31 earned 50 total points
ID: 6991489
BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);

// The getWidth() and getHeight() methods accept an ImageObserver. But we know that the image is fully loaded. // Therefore, we pass null as the ImageObserver, knowing that getWidth() and getHeight() will return valid values.
// Next, we obtain the Graphics2D corresponding to the new BufferedImage and render the loaded Image on it.

Graphics2D g2 = bi.createGraphics();
g2.drawImage(image, null, null);

now that you've drawn the image to the BufferedImage, you should be able to do something like:

bi.getRGB(int x, int y) to get the rgb color of pixel at x,y

and

bi.setRGB(int x, int y, int rgb) to set the rgb color of pixel at x,y to rgb

0
 
LVL 92

Expert Comment

by:objects
ID: 6992530
Have a look at PixelGrabber, and ImageFilter's.
0
 
LVL 1

Author Comment

by:dyma82
ID: 6993900
bobbit31 :

That's what I was missing, thanks. Would you clear this for me??

I knew I could greate an BufferedImage object, but I could NOT find anywhere the means to load my Image into the newly created BufferedImage. Thus, what you say is the this way is by getting the Graphics2D object from the BufferedImage, and then, drawing my Image to that object, right???

So, that's the way I load my Image into my newly created BufferedImage, right???


Thanks a lot for your time!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 18

Expert Comment

by:bobbit31
ID: 6993969
> So, that's the way I load my Image into my newly created BufferedImage, right???

yes
0
 
LVL 1

Author Comment

by:dyma82
ID: 6996400
Thanks a lot!
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6997285
no prob :)
glad to help!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

863 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

24 Experts available now in Live!

Get 1:1 Help Now