Results 1 to 4 of 4
  1. #1
    Piri is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default Read BMP File...

    Hi all...I am a student and have to do a work in Java, and i'm kind off stuck :confused:

    Hi need to read a binary file (in this case a BMP file) and later tell some things about the file and let the user flip and re-size the image! But first I need help to read the file :S

    I now that a BMP File follows this structure:

    Class Bitmap{
    BitmapFileHeader bitmapFileHeader;
    BitmapInfoHeader bitmapInfoHeader;
    byte[] rgbQuad; // color pallete opcional (ver abaixo)
    byte[] data; // pixel data
    }

    Class BitmapFileHeader {
    short type; // must be 'BM' to declare a bmp-file
    int size; // specifies the size of the file in bytes
    short reserved1; // must always be set to zero
    short reserved2; // must always be set to zero
    int offBits; // specifies the offset from the
    // beginning of the file to the bitmap data
    }

    Class BitmapInfoHeader{
    int size; // the size of this header (40 bytes)
    int width; // the bitmap width in pixels
    int height; // the bitmap height in pixels
    short planes; // the number of color planes being used. Must be set to 1
    short bitCount; // the number of bits per pixel (color depth) - 1, 4, 8, 16, 24, 32
    int compression; // the compression method being used
    int sizeImage; // the image size. This is the size of the raw bitmap data
    int xPelsPerMeter; // the horizontal resolution of the image (pixel per meter)
    int yPelsPerMeter; // the vertical resolution of the image (pixel per meter)
    int clrUsed; // the number of colors in the color palette,
    // or 0 to default to 2n
    int clrImportant; // the number of important colors used,
    // or 0 when every color is important
    }
    The teacher told that it was just read the file because after we now the structure the rest is easy -.-

    I did the classes and started to read from the file...But when I get to read the byte[] data; something wrong appear...The compiler tells me that OffBits() is null -.-

    I even now that I have to:
    short bitCount = Short.reverseBytes(in.readShort());
    int size = Integer.reverseBytes(in.readInt());
    because:

    (Least Significant Byte first).
    Vrs
    (Most Significant Byte first).

    Here it goes part of my code:
    public boolean addPicture(String local) throws IOException{
    try{
    File fileOrigin = new File(local);
    FileInputStream i = new FileInputStream(fileOrigin);
    DataInputStream in = new DataInputStream(i);


    BitmapFileHeader parte1=new BitmapFileHeader();
    parte1.setType(Short.reverseBytes(in.readShort())) ;
    parte1.setSize(Integer.reverseBytes(in.readInt())) ;
    parte1.setReserved1(Short.reverseBytes(in.readShor t()));
    parte1.setReserved2(Short.reverseBytes(in.readShor t()));
    parte1.setOffBits(Short.reverseBytes(in.readShort( )));


    setBitmapFileHeader(parte1);

    BitmapInfoHeader parte2=new BitmapInfoHeader();
    parte2.setSize(Integer.reverseBytes(in.readInt())) ;
    parte2.setWidth(Integer.reverseBytes(in.readInt()) );
    parte2.setHeight(Integer.reverseBytes(in.readInt() ));
    parte2.setPlanes(Short.reverseBytes(in.readShort() ));
    parte2.setBitCount(Short.reverseBytes(in.readShort ()));
    parte2.setCompression(Integer.reverseBytes(in.read Int()));
    parte2.setSizeImage(Integer.reverseBytes(in.readIn t()));
    parte2.setxPelsPerMeter(Integer.reverseBytes(in.re adInt()));
    parte2.setyPelsPerMeter(Integer.reverseBytes(in.re adInt()));
    parte2.setClrUsed(Integer.reverseBytes(in.readInt( )));

    parte2.setClrImportant(Integer.reverseBytes(in.rea dInt()));

    setBitmapInfoHeader(parte2);
    //int x;
    //while((x = in.read()) != -1){
    //System.out.println("byte = "+x);
    //}
    //try{
    System.out.println(parte1.getOffBits());
    System.out.println(parte1.getType());
    in.read(data,Integer.reverseBytes(parte1.getOffBit s()),1);



    // }catch(java.lang.NullPointerException e){
    // System.out.println("NILLLLLLL");
    // }
    System.out.println(getData());
    in.close();
    }catch(Exception e){
    System.out.println("Error = "+e);
    }

    return true;
    }

    Hope that someone helps me :(

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

    Default

    Why not leave the actual reading of the .bmp file to the ImageIO class? Read the API documentation.

    kind regards,

    Jos

  3. #3
    Piri is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    Because its suppose to read the file this way -.-

    Aim is to develop an application that allows to decode and process the
    content of an image file in BMP format (uncompressed). The application
    must meet the following requirements:
    a) Receive the location of a BMP file, open it and print all information
    on that file, ie number of rows, columns, number of colors, etc..
    b) allowing the extraction of pixel data "and save to a file." raw ". Try to visualize
    this file with appropriate image viewer (Irfanview, Photoshop, etc.)
    c) allow the reduction of the size of the image to . For example, an image with
    with 512x512 pixels would then be 256x256. The end result should be saved
    a new BMP file.
    d) Allow the image to reverse (flip) vertically or horizontally. The end result should
    be stored in a new BMP file.

  4. #4
    Piri is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Read a file and converting this file into a string
    By kostinio in forum New To Java
    Replies: 7
    Last Post: 12-26-2009, 03:54 PM
  2. Replies: 2
    Last Post: 05-11-2009, 10:07 AM
  3. Replies: 0
    Last Post: 02-11-2009, 09:53 AM
  4. Replies: 5
    Last Post: 02-05-2009, 10:28 AM
  5. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:13 AM

Posting Permissions

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