Solved

About imaging with Java

Posted on 2002-05-06
6
227 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

696 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