• 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

how to create an image through bufferedimage,image and other helper class using byte array

 
Greenhorn
Posts: 19
Oracle Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have an array of type byte.that array consist of the bytes read from an bmp image file.now how to recreate a bmp image file through that byte array.i have heared that it can be created by using bufferedimage and some other class.provide me the code of recreating the image through an array of byte using bufferedimage class.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out ImageIO.
 
addy sharma
Greenhorn
Posts: 19
Oracle Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
byte rasterdata[]=new byte[br.available()]; //this is the array which has the byte of a bmp image.
br.read(rasterdata,0,(br.available()-1));
ByteArrayInputStream bis= new ByteArrayInputStream(rasterdata);
Iterator<?> readers = ImageIO.getImageReadersByFormatName("bmp");
ImageReader reader = (ImageReader) readers.next();
Object source = bis;
ImageInputStream iis = ImageIO.createImageInputStream(source);
reader.setInput(iis, true);
ImageReadParam param = reader.getDefaultReadParam();
Image image = reader.read(0,param); //and this the place where i am getting the following error



Exception in thread "main" java.lang.IllegalArgumentException: Invalid magic value for BMP file.
at com.sun.imageio.plugins.bmp.BMPImageReader.readHeader(Unknown Source)
at com.sun.imageio.plugins.bmp.BMPImageReader.read(Unknown Source)
at rd.main(rd.java:138)
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then either the image isn't a proper BMP file, or Java doesn't support this type of BMP file.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
all i can tell you for sure is ImageIO can read the .bmp i just created in microsoft Paint. your code seems kind of complicated to me. mine seems much simpler.

where image is a BufferedImage.
don't know if that helps
i give up. i copy code and use code tags. it looks fine when i paste it, but when it posts it is screwed up. even now as i edit, the code is indented properly. but once i post, it will be screwed up.
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just don't mix tabs and spaces in your indentation.
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
maybe i should get a different text editor like i saw someone mention somewhere. i would like tab to be 4 spaces not 8.
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I setup my text editor to convert new tabs to spaces.
 
addy sharma
Greenhorn
Posts: 19
Oracle Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
any image can be read through FileInputSteram and the data can be stored in an array of byte.every image contains the header,other information and the image data of its specific type.suppose if we have read the header and after a specific offset in the byte stream ,the image data starts.that image data can be used in drawing images through JFrame.

There is a way to draw that image from that image data without those headers information.it can be done by taking the image data in some DataBufferInt and some of the methods i remember are getRaster().getDataBuffer().with the help of these the image can be generated in a JFrame.
so can you provide me the code to do this task.

@Randall Twede:i want the code to read the data of an image in byte form and then recreate that image with the help of that byte data excluding the header information.
 
addy sharma
Greenhorn
Posts: 19
Oracle Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have got the code...here it is!!!

 
reply
    Bookmark Topic Watch Topic
  • New Topic