GeeCON Prague 2014*
The moose likes Java in General and the fly likes how to  create an image through bufferedimage,image and other helper class using byte array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "how to  create an image through bufferedimage,image and other helper class using byte array" Watch "how to  create an image through bufferedimage,image and other helper class using byte array" New topic
Author

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

addy sharma
Greenhorn

Joined: Apr 17, 2011
Posts: 16

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.


java is best because i can even drink it.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Check out ImageIO.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
addy sharma
Greenhorn

Joined: Apr 17, 2011
Posts: 16

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

Joined: Oct 27, 2005
Posts: 19697
    
  20

Then either the image isn't a proper BMP file, or Java doesn't support this type of BMP file.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

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.


SCJP
Visit my download page
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Just don't mix tabs and spaces in your indentation.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

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

Joined: Oct 27, 2005
Posts: 19697
    
  20

I setup my text editor to convert new tabs to spaces.
addy sharma
Greenhorn

Joined: Apr 17, 2011
Posts: 16

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

Joined: Apr 17, 2011
Posts: 16

i have got the code...here it is!!!

 
GeeCON Prague 2014
 
subject: how to create an image through bufferedimage,image and other helper class using byte array