Turn: Java extracts pixels from a picture

Source: Internet
Author: User

This article transferred from: http://www.infosys.tuwien.ac.at/teaching/courses/WebEngineering/References/java/docs/api/java/awt/image/ Pixelgrabber.html

The PixelGrabber class implementation can attach a imageconsumer that obtains a subset of the image pixels on an image or ImageProducer object. Here is an example:

 public void Handlesinglepixel (int x, int y, int pixel) {int alpha = (pixel >>) & 0xFF;        int red = (pixel >>) & 0xFF;        int green = (pixel >> 8) & 0xFF;        int blue = (pixel) & 0xff; Deal with the pixel as necessary ...}        public void Handlepixels (Image img, int x, int y, int w, int h) {int[] pixels = new int[w * h];        PixelGrabber pg = new PixelGrabber (img, x, y, W, h, pixels, 0, W);        try {pg.grabpixels ();            } catch (Interruptedexception e) {System.err.println ("interrupted waiting for pixels!");        Return } if ((Pg.getstatus () & imageobserver.abort)! = 0) {System.err.println ("image fetch aborted or Erro            Red ");        Return } for (int j = 0; J < H; j + +) {for (int i = 0; i < W; i++) {Handlesinglepixel (x+i            , Y+j, Pixels[j * w + i]); }        } }

  

PixelGrabber
PixelGrabber (Image img,                    int x,                    int y,                    int w,                    int h,                    int[] pix,                    int off,                    int scansize)
Creates a PixelGrabber object that fetches the pixel rectangle (x, Y, W, h) from the specified image into the given array. Stores the pixels in the array in the default RGB colormodel form. The RGB data in pixels (i, j) ((I, j) in the rectangle (x, Y, W, h) is stored in the array pix[(j-y) * scansize + (i-x) + off] position.

Parameters:
img -image from which to retrieve pixels
-The x-coordinate of the upper
x -left corner of the pixel rectangle retrieved from the image, relative to the default (non-scaled) image size
y -the upper-left corner of the pixel rectangle retrieved from the image y-coordinate
-the width of the
w pixel rectangle to retrieve
-the height of the
h pixel rectangle to retrieve
pix -an array of integers to hold the RGB pixels retrieved from the image
off -The offset of the first pixel
in the array
scansize -the distance between a row of pixels in the array and the next line of pixels
See also:
ColorModel.getRGBdefault()

PixelGrabber
 public  PixelGrabber  (Imageproducer ip, Int x, Int&nbs P;y, Int w, Int h, Int[] pix, I Nt off, Int scansize) 
The
creates a PixelGrabber object that crawls the pixel rectangle (x, Y, W, h) into the given array from the image generated by the specified imageproducer. Stores the pixels in the array in the default RGB colormodel form. The RGB data for pixels (i, j) ((I, j) in the rectangle (x, Y, W, h) is stored in the array   pix[(j-y) * scansize + (i-x) + off]   position.

 

Parameters:
IP  -generate images for   imageproducer, retrieving pixels from the image
x  -The x-coordinate of the upper-left corner of the pixel rectangle retrieved from the image, relative to the default (non-scaled) image size
y  -the upper-left corner of the pixel rectangle retrieved from the image
w  -The width of the pixel rectangle to retrieve
h  -the height of the pixel rectangle to retrieve
pix  -An integer array to hold the RGB pixels retrieved from the image
o FF  -The offset of the first pixel in the array
scansize The distance between a row of pixels in the  -array and the next line of pixels
See also:
colormodel.getrgbdefault ()

PixelGrabber
PixelGrabber (Image img,                    int x,                    int y,                    int w,                    int h,                    boolean Forcergb)
Creates a PixelGrabber object that captures the rectangular portion of the pixel (x, Y, W, h) from the specified image. If each call to SetPixels uses the same colormodel, it is stored as if it were the original ColorModel, otherwise the pixels are stored in the default RGB colormodel format. If the Forcergb parameter is true, the pixels will always be stored in the default RGB colormodel form. In either case, the PixelGrabber allocates a buffer to hold the pixels. if (W < 0) or (H < 0), they default to the width and height of the source data saved when the information is passed.

Parameters:
img -image to retrieve image data
from
x -The x-coordinate of the upper-left corner of the pixel rectangle retrieved from the image, relative to the default (not scaled) image size
y -The y-coordinate of the upper-left corner of the pixel rectangle retrieved from the image
w -width of the pixel rectangle to retrieve
h -the height of the pixel rectangle to retrieve
forceRGB -True if the pixel should always be converted to the default RGB ColorModel
Grabpixels
Grabpixels ()                   throws Interruptedexception
Requests that Image or imageproducer begin passing pixels, and waits for all pixels in the relevant rectangle to be passed.

Return:
Returns true if the pixel was successfully fetched, false if aborted, error, or timed out
Thrown:
InterruptedException -Another thread has interrupted this thread.

Turn: Java extracts pixels from a picture

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.