Home | | Web Programming | ImageConsumer

Chapter: Java The Complete Reference : The Java Library : Images

ImageConsumer

ImageConsumer is an interface for objects that want to take pixel data from images and supply it as another kind of data.

ImageConsumer

 

ImageConsumer is an interface for objects that want to take pixel data from images and supply it as another kind of data. This, obviously, is the opposite of ImageProducer, described earlier. An object that implements the ImageConsumer interface is going to create int or byte arrays that represent pixels from an Image object. We will examine the PixelGrabber class, which is a simple implementation of the ImageConsumer interface.

PixelGrabber

 

The PixelGrabber class is defined within java.lang.image. It is the inverse of the MemoryImageSource class. Rather than constructing an image from an array of pixel values, it takes an existing image and grabs the pixel array from it. To use PixelGrabber, you first create an array of ints big enough to hold the pixel data, and then you create a PixelGrabber instance passing in the rectangle that you want to grab. Finally, you call grabPixels( ) on that instance.

 

The PixelGrabber constructor that is used in this chapter is shown here:

 

PixelGrabber(Image imgObj, int left, int top, int width, int height, int pixel [ ], int offset, int scanLineWidth)

 

Here, imgObj is the object whose pixels are being grabbed. The values of left and top specify the upper-left corner of the rectangle, and width and height specify the dimensions of the rectangle from which the pixels will be obtained. The pixels will be stored in pixel beginning at offset. The width of a scan line (which is often the same as the width of

the image) is passed in scanLineWidth. grabPixels( ) is defined like this:

 

boolean grabPixels( )

 

throws InterruptedException

 

boolean grabPixels(long milliseconds) throws InterruptedException

 

Both methods return true if successful and false otherwise. In the second form, milliseconds specifies how long the method will wait for the pixels. Both throw InterruptedException if execution is interrupted by another thread.

Here is an example that grabs the pixels from an image and then creates a histogram of pixel brightness. The histogram is simply a count of pixels that are a certain brightness for all brightness settings between 0 and 255. After the applet paints the image, it draws the histogram over the top.

/*

 

    <applet code=HistoGrab width=400 height=345>

 

    <param name=img value=Lilies.jpg>

 

    </applet> */

 

import java.applet.*; import java.awt.* ; import java.awt.image.* ;

public class HistoGrab extends Applet { Dimension d;

 

Image img; int iw, ih; int pixels[]; int w, h;

 

int hist[] = new int[256]; int max_hist = 0;

 

public void init() { d = getSize();

 

w = d.width; h = d.height;

 

try {

 

img = getImage(getDocumentBase(), getParameter("img")); MediaTracker t = new MediaTracker(this);

 

t.addImage(img, 0); t.waitForID(0);

 

iw = img.getWidth(null); ih = img.getHeight(null); pixels = new int[iw * ih];

 

PixelGrabber pg = new PixelGrabber(img, 0, 0, iw, ih, pixels, 0, iw);

 

pg.grabPixels();

 

} catch (InterruptedException e) { System.out.println("Interrupted"); return;

}

 

for (int i=0; i<iw*ih; i++) { int p = pixels[i];

 

int r = 0xff & (p >> 16); int g = 0xff & (p >> 8); int b = 0xff & (p);

 

int y = (int) (.33 * r + .56 * g + .11 * b); hist[y]++;

 

}

 

for (int i=0; i<256; i++) { if (hist[i] > max_hist) max_hist = hist[i];

 

}

 

}

 

public void update() {}

 

public void paint(Graphics g) { g.drawImage(img, 0, 0, null); int x = (w - 256) / 2;

 

int lasty = h - h * hist[0] / max_hist;

for (int i=0; i<256; i++, x++) {

 

int y = h - h * hist[i] / max_hist; g.setColor(new Color(i, i, i)); g.fillRect(x, y, 1, h); g.setColor(Color.red); g.drawLine(x-1,lasty,x,y);

 

lasty = y;

 

}

 

}

 

}

 

Figure 27-4 shows an example image and its histogram.



Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Java The Complete Reference : The Java Library : Images : ImageConsumer |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.