java left logo
java middle logo
java right logo
 

Home
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4096
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to use PixelGrabber class to acquire pixel data from an Image object E-mail
User Rating: / 105
PoorBest 

PixelGrabber class available in java.awt.image packate can be used to access pixels of an Image object. PixelGrabber class can acquire pixel data synchronously or asynchronously. It can store pixel values in a user-specified array or create a suitable array itself.

The following example shows how to use PixelGrabber class to get pixel data from an Image object.

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.PixelGrabber;

public class PixelGrabberTest {
    
    public PixelGrabberTest() {
    }
    
    public static void processImage(String inFile, String outFile) {
        
        Image image = Toolkit.getDefaultToolkit().getImage(inFile);
        
        try {
            
            PixelGrabber grabber = 
                    new PixelGrabber(image, 00, -1, -1false);
            
            if (grabber.grabPixels()) {
                int width = grabber.getWidth();
                int height = grabber.getHeight();
                
                if (isGreyscaleImage(grabber)) {
                    byte[] data = (byte[]) grabber.getPixels();
                    
                    // Process greyscale image ...
                    
                }
                else {
                    int[] data = (int[]) grabber.getPixels();
                    
                    // Process Color image
                    
                }
            }
        }
        catch (InterruptedException e1) {
            e1.printStackTrace();
        }
    }
    
    public static final boolean isGreyscaleImage(PixelGrabber pg) {
        return pg.getPixels() instanceof byte[];
    }
    
    public static void main(String args[]) {
        
        if (args.length > 1) {
            processImage(args[0], args[1]);
            System.exit(0);
        else {
            System.err.println(
                    "usage: java PixelGrabberTest <infile> <outfile>");
            
            System.exit(2);
        }
          
    }
}

 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.