Solved

About imaging with Java

Posted on 2002-05-06
6
197 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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…

744 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

14 Experts available now in Live!

Get 1:1 Help Now