Results 1 to 3 of 3
  1. #1
    Moorkh is offline Member
    Join Date
    Sep 2010
    Posts
    1
    Rep Power
    0

    Default 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]);
    }
    }



    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    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

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    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

  1. Need help converting int to a 4 byte array
    By kook04 in forum Advanced Java
    Replies: 5
    Last Post: 02-26-2010, 08:59 PM
  2. Converting Image to byte array[] ?
    By afflictedd2 in forum CLDC and MIDP
    Replies: 0
    Last Post: 04-11-2009, 11:33 PM
  3. Reading/Writing a File using byte array
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-16-2008, 10:41 AM
  4. Converting a byte[] into Sound
    By savage82 in forum Advanced Java
    Replies: 1
    Last Post: 11-21-2007, 11:57 PM

Posting Permissions

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