• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getting problem in creating an image from int array

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had created an int array from an image from the following code..
public void createImage(Image serverlateImage)
{int iw=0;
int ih=0;
int i=0;
try{ iw=serverlateImage.getWidth(null);
ih=serverlateImage.getHeight(null);
pixels=new int[iw*ih];
PixelGrabber pg=new PixelGrabber(serverlateImage,0,0,iw,ih,pixels,0,iw);
pg.grabPixels();
}catch(InterruptedException ie){}
}
now if i had created perfect int array then i m creating an image from this array...and code is following..
PrintFrame(int []pixels)
{System.out.println("size of array at printframe "+pixels.length);

Dimension d=getSize();
int w=(int)(d.getWidth());
int h=(int)(d.getHeight());
int al = 0,
rd = 0,
gr = 0,
bl = 0,
avg = 0;
int grayMap[] = new int[w*h];
// im=createImage(pixels,w,h);
// if(im!=null)
{for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
al = (pixels [(i*w)+j] & 0xff000000)>>24;
rd = (pixels [(i*w)+j] & 0x00ff0000)>>16;
gr = (pixels [(i*w)+j] & 0x0000ff00)>>8;
bl = (pixels [(i*w)+j] & 0x000000ff);
avg = (rd+gr+bl)/3;
grayMap [(i*w)+j] = ((al <<24) |(avg <<16)|(avg<<8)|avg);
}
}
im=createImage (grayMap,w,h) ;
}
public static Image createImage(int [] pixels, int width, int height)
{
MemoryImageSource imageSource = new MemoryImageSource(width,height,pixels,0,width);
return Toolkit.getDefaultToolkit().createImage(imageSource);
}
now can somebody tell me whats the problem with this code...y i am not able to get image....thnx in advance
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
I understand that you are trying to do a simple image processing functionality-grayscaling of RGB images.
Here is a similar code:

In your code, you should have included the Mediatracker while loading the image.Remember image loading takes some time.So you must ensure that the execution of the program waits until the image is fully loaded. m.waitForID9..) ensures this. Otherwise you get NullPointerException for the image.
Secondly in your method printFrame(int[] pixels) you took the width and height of the image to be created from:
Dimension d=getSize();
but assuming that you are writing the code inside a class extending FRame/Applet/Panel etc, getSize() will give the size of that component and this size has nothing to do with the image.What you need is the size of the original image from which you obtained the pixels. So in the imageToIntArray(...) method put the width and height of the image into member variables, so that you can get these values in the convert(..) method.
hope this helps.
For more image manipulation algorithms,codes and concepts like image processing theory,image filters, brightness/contrast, histograms, noise reduction etc you can visit my website: http://tanveer.freeservers.com and go to the projects page.Or directly you can access: http://tanveer.freeservers.com/projects/image_processing_cover.htm
hope this helps
regards
Tanveer
 
Tanveer Rameez
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
I my reply above, I wrote 'image' variable as 'im' in the code:

Please correct it to:

 
reply
    Bookmark Topic Watch Topic
  • New Topic