Results 1 to 3 of 3
- 09-07-2010, 11:51 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 1
- Rep Power
- 0
converting byte array to bmp file
i am trying to generate texture models.
i have so far created a 2x2 integer array of the texture with the values representing height or darkness.
i want to convert this array into a bmp file so that its easier to appreciate.
i have converted the array to a byte[][] array and m trying to write it to a bmp file
i used The .bmp file format to guide me about the details of the format. and tried to write all the headers etc by myself. however the file i manage to create is unreadable by windows. is there an easier way to achieve it?
public static void convImg(int[][] arr,int n) throws IOException{
//String filePath = "D://fract//cloud1.bmp";
String filePath = "cloud1.bmp";
System.out.println("going in to create file");
//File f;
//f= new File(filePath);
/*if(f.exists()){
System.out.println("filecreated");
}*/
System.out.println("file created");
FileOutputStream fos = new FileOutputStream(filePath);
System.out.println("output stream created");
//file header
fos.write("BM".getBytes()); //to specify that the file is a bitmap file
fos.write(1352); // specifies the size of the file
fos.write(0); // should be zero by default in bmp format
fos.write(1078); // mentions the offset of the start of data from file beginning
//file info header
fos.write(40); // size of the info header
fos.write(n); //width of image in pixels
fos.write(n); //height of image in pixels
/*
byte tt; //writing the number of planes
tt=(byte)0; //of target device
fos.write(tt); //should be
fos.write(tt); // set to zero
*/
fos.write((short)0); // number of planes in target device generally set to zero
fos.write((short)8); // number of bits used per pixel
fos.write(0); // type of compression, zero means no compression
fos.write(0); //-|
fos.write(0); // |
fos.write(0); // |--> some other values that need to be set to zero
fos.write(0); // |
fos.write(0); //-|
byte[][] bb = new byte[n][n];// converting int array to byte array
bb= toByteArr(arr,n);
int i,j;
for(i=0;i<n;i++){ // writing bitmap data to file
for(j=0;j<n;j++){
fos.write(bb[i][j]);
}
}
}
- 09-07-2010, 12:18 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
Create a BufferedImage of the right size and type; get a Graphics object from it; draw on the BufferedImage using this Graphics object and finally save the BufferedImage as a .bmp file using the ImageIO class.
kind regards,
Jos
- 09-07-2010, 02:58 PM #3
You should copy the file format into your code so anyone can compare what your code does with the format. Jumping back and forth from one page to another to verify that your output is correctly formatted will require someone with more patience than I have.
Make it easy for someone to help and you'll get more help.
Also use code tags vs quote tags around your code
Similar Threads
-
Need help converting int to a 4 byte array
By kook04 in forum Advanced JavaReplies: 5Last Post: 02-26-2010, 08:59 PM -
Converting Image to byte array[] ?
By afflictedd2 in forum CLDC and MIDPReplies: 0Last Post: 04-11-2009, 11:33 PM -
Reading/Writing a File using byte array
By Java Tip in forum Java TipReplies: 0Last Post: 01-16-2008, 10:41 AM -
Converting a byte[] into Sound
By savage82 in forum Advanced JavaReplies: 1Last Post: 11-21-2007, 11:57 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks