# Thread: Convert a table BufferedImage

1. Member
Join Date
Dec 2009
Posts
213

## Convert a table BufferedImage

Hello,
I would like to know how to convert a table with each cell contains the value of a pixel between 0 to 255 in a BufferedImage?

I have tried the following code, but it does not work
Code:
bufedImage buf = new bufedImage(width, height, bufedImage.TYPE_BYTE_GRAY) ;
buf.setRGB(0, 0, Width, height, pixels, 0, Width) ;
If you have a solution for this then please do post back I am the interested on here. Thanks in advance.

2. Member
Join Date
Nov 2009
Posts
356

## Re: Convert a table BufferedImage

Hello,
Have you tries with setPixel void (int x, int y, int [] t) in the case of an image of gray. t contains only 1 row and one column contention the pixel value. x and y respectively indicates the line you column of pixels. I think you should try with the following, this will definitely give you an idea about it. If you need any more help regarding this topic then do post back and we will try to help you.

3. Member
Join Date
Nov 2009
Posts
335

## Re: Convert a table BufferedImage

Hello,
Have a look at the following code, please see if this works for you
Code:
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
int c = 0;
for( int i = 0; I <width; i + +)
{
for(int j = 0J <height; j + +){
picture.getRaster().setPixel(i, j, new int[1]{c});
}
c + +;
}

4. Member
Join Date
Nov 2009
Posts
518

## Re: Convert a table BufferedImage

Hello,
Besides the proposed solution, what does not work exactly? Did you try to convert your picture to gray pixels in RGB pixel array before calling the method?
Code:
int[] colors = new int[pixels.length];
for (int i = 0 ; I <pixels.length ; I + +) {
colors[i] = ((pixels[i] & 0xFF) << 16) | ((pixels[i] & 0xFF) << 8) | ((pixels[i] & 0xFF) << 0);
}
buffer.setRGB(0, 0, Width, height, colors, 0, Width) ;
Obviously, this way, It will use more memory than the method suggested and is probably not preferable. Note, however, direct access to Raster a BufferedImage removes any potential graphics accelaration.

5. Member
Join Date
Nov 2009
Posts
446

## Re: Convert a table BufferedImage

Hello,
This code is going to help you, take a look at it
Code:
public class buffimgConverter {

// default version of crbufimg
static public buffimg crbufimg(Image imageIn,
Component comp) {
return crbufimg(imageIn, buffimg.TYPE_INT_ARGB, comp);
}

static public buffimg crbufimg(Image imageIn,
int imageType, Component comp) {
MediaTracker med = new MediaTracker(comp);
try {
med.waitForID(0);
} catch (InterruptedException ie) {
}
buffimg buffimgo = new buffimg(imageIn
.getWidth(null), imageIn.getHeight(null), imageType);
Graphics g = buffimgo.getGraphics();
g.drawImage(imageIn, 0, 0, null);

return buffimgo;
}
}

6. Member
Join Date
Dec 2009
Posts
211

## Re: Convert a table BufferedImage

Hello,
I am new to the swing components in java, but you can try the following
Code:
javax.swing.JFrame f;                  // Initialized elsewhere

// Create an image, and wait for it to load
Image img = javax.swing.ImageIcon("myimage.gif").getImage();

// Create a BufferedImage of the same size as the Image
BufferedImage buff = (BufferedImage)f.createImage(img.getWidth(f),img.getHeight(f));

Graphics2D g = buff.createGraphics();    // Get a Graphics2D object
g.drawImage(img, 0, 0, f);      // Draw the Image data into the BufferedImage

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•