?
Solved

About imaging with Java

Posted on 2002-05-06
6
Medium Priority
?
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 18

Accepted Solution

by:
bobbit31 earned 200 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.
Suggested Courses

719 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