The Artima Developer Community
Sponsored Link

Java Answers Forum
light a pixel

1 reply on 1 page. Most recent reply: Mar 27, 2002 8:35 PM by Yousuf

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
fara

Posts: 1
Nickname: fara
Registered: Mar, 2002

light a pixel Posted: Mar 26, 2002 10:16 PM
Reply to this message Reply
Advertisement
hi
i want to light a pixel with java .iknow there is
pixelgrabber class for it .but i don't know how to use
it.
please write an example code .

thanks.


Yousuf

Posts: 19
Nickname: saeed
Registered: Mar, 2002

Re: light a pixel Posted: Mar 27, 2002 8:35 PM
Reply to this message Reply
Hello fara..
I hope the following code of mine will you help that explore the functionality of pixel grabber class.
This class is used to adjust your image in terms of pixel according to your requirement.


import java.awt.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.color.*;
import java.util.*;
import javax.swing.*;

public class RGBImageFactory implements ImageObserver{

public static void main(String[] args){
RGBImageFactory ifact = new RGBImageFactory();
/* if(args.length <1) {
System.out.println("Enter a valid image file name");
System.exit(0);
}*/
ifact.createRGBBufferedImage("Bup.jpg");//any image should be in same folder
}

public Image readImage(String imageName){
Image image = Toolkit.getDefaultToolkit().getImage(imageName);
MediaTracker imageTracker = new MediaTracker(new JPanel());
imageTracker.addImage(image, 0);
try{
imageTracker.waitForID(0);
}catch(InterruptedException e){ return null;}
return image;
}

public boolean imageUpdate(Image img,
int infoflags,
int x,
int y,
int width,
int height){

if((infoflags & ImageObserver.ERROR) != 0){
System.out.println("ERROR in image loading or drawing");
return false;
}

if((infoflags & (ImageObserver.FRAMEBITS | ImageObserver.ALLBITS))!= 0) {
return false;
}
return true;
}


public int[] fetchPixels(Image image, int width, int height){
int pixMap[] = new int[width*height];
PixelGrabber pg = new PixelGrabber(image, 0,0,width,height, pixMap, 0, width);//thats you want...Use OF PixelGrabber
try {
pg.grabPixels();
} catch (InterruptedException e){return null;}
if((pg.status() & ImageObserver.ABORT)!=0){
return null;
}
return pixMap;
}

public byte[] extractData(int[] pixmap, int numbands) {
byte data[] = new byte[pixmap.length*numbands];

for(int i=0;i<pixmap.length;i++){
int pixel = pixmap;
byte a = (byte)((pixel >> 24) & 0xff);
byte r = (byte)((pixel >> 16) & 0xff);
byte g = (byte)((pixel >> 8) & 0xff);
byte b = (byte)((pixel ) & 0xff);

if(numbands == 4){
data[i*numbands+0] = r;
data[i*numbands+1] = g;
data[i*numbands+2]= b;
data[i*numbands+3] = a;
} else {
data[i*numbands+0] = r;
data[i*numbands+1] = g;
data[i*numbands+2]= b;
}
}
return data;
}
public static void displayImage(BufferedImage img){
JFrame fr = new JFrame();
ImagePanel pan = new ImagePanel(img);
pan.setSize(150,150);
fr.getContentPane().add(pan);
fr.pack();
fr.setSize(150,150);
fr.show();
}

public void createRGBBufferedImage(String filename) {
Image awtImage = readImage(filename);
int imageWidth = awtImage.getWidth(this);
int imageHeight = awtImage.getHeight(this);
int[] pix = fetchPixels(awtImage, imageWidth, imageHeight);
byte[] data = extractData(pix, 4);
BufferedImage image = createInterleavedRGBImage(imageWidth, imageHeight, 8, data,true);
displayImage(image);
}


public static BufferedImage createInterleavedRGBImage(int imageWidth,
int imageHeight,
int imageDepth,
byte data[],
boolean hasAlpha){
int pixelStride,transparency ;
if(hasAlpha) {
pixelStride = 4;
transparency = Transparency.BITMASK;
}
else {
pixelStride = 3;
transparency = Transparency.OPAQUE;
}
int[] numBits = new int[pixelStride];
int[] bandoffsets = new int[pixelStride];

for(int i=0;i<pixelStride;i++){
numBits = imageDepth;
bandoffsets =i;
}

ComponentColorModel ccm = new ComponentColorModel(
ColorSpace.getInstance(ColorSpace.CS_sRGB),
numBits,
hasAlpha,
false, //Alpha pre-multiplied
transparency,
DataBuffer.TYPE_BYTE);
PixelInterleavedSampleModel csm = new PixelInterleavedSampleModel(
DataBuffer.TYPE_BYTE,
imageWidth, imageHeight,
pixelStride, //Pixel stride
imageWidth*pixelStride, // Scanline stride
bandoffsets);

DataBuffer dataBuf = new DataBufferByte(data, imageWidth*imageHeight*pixelStride);
WritableRaster wr = Raster.createWritableRaster(csm, dataBuf, new Point(0,0));
Hashtable ht = new Hashtable();
ht.put("owner", "Lawrence Rodrigues");
return new BufferedImage(ccm, wr, false, ht);
}

static class ImagePanel extends JComponent {
protected BufferedImage image;
public ImagePanel(){}
public ImagePanel(BufferedImage img){ image = img;}

public void setImage(BufferedImage img){ image = img; }

public void paintComponent(Graphics g){
Rectangle rect = this.getBounds();
if(image != null) {
g.drawImage(image, 0,0,rect.width, rect.height, this);
}
}
}
}

Yousuf

Flat View: This topic has 1 reply on 1 page
Topic: Handling files in java Previous Topic   Next Topic Topic: How to position cursor in JFileChooser

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use