Solved

About imaging with Java

Posted on 2002-05-06
6
224 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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 learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

856 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