Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Creating a bmp in java

markm208markm208 Posts: 1Member
I am trying to write a class that will create a simple bitmap using an array of pixel values. I found the format of bmp's here http://www.fortunecity.com/skyscraper/windows/364/bmpffrmt.html

It appears that I am creating the file correctly but my bmp's don't show up. Any advice would be much appreciated. Here is the code:

import java.util.*;
import java.io.*;

public class BitmapCreator
{
private List < String > bitMapFileHeader = new ArrayList< String >();
private List < String > bitMapInfoHeader = new ArrayList< String >();
private List < String > rgbQuadArray = new ArrayList < String >();
private List < String > pixels = new ArrayList < String >();

private final static int BMP_WIDTH = 4;
private final static int BMP_HEIGHT = 4;
private final static int BMP_NUM_BITS_FOR_COLORS = 24;
private final static int BMP_PALETTE_SIZE = (int)Math.pow(2.0, (double)BitmapCreator.BMP_NUM_BITS_FOR_COLORS);
private final static int BMP_IMAGE_SIZE = BMP_HEIGHT * BMP_WIDTH;
private final static int BMP_FILE_SIZE = BMP_IMAGE_SIZE + 40 + 14;

public BitmapCreator()
{
setUpFileHeader();
setUpInfoHeader();
//setUpRgbQuadArray();
addPixels(null);
}

public void setUpInfoHeader()
{
//for bi size
bitMapInfoHeader.add(convertIntToBytes(40, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));
//bitMapInfoHeader.add(convertIntToBytes(40, 8));

//for bi width
bitMapInfoHeader.add(convertIntToBytes(BitmapCreator.BMP_WIDTH, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));
//bitMapInfoHeader.add(convertIntToBytes(BitmapCreator.BMP_WIDTH, 8));

//for bi height
bitMapInfoHeader.add(convertIntToBytes(BitmapCreator.BMP_HEIGHT, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));
//bitMapInfoHeader.add(convertIntToBytes(BitmapCreator.BMP_HEIGHT, 8));

//for bi planes
bitMapInfoHeader.add(convertIntToBytes(1, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));
//bitMapInfoHeader.add(convertIntToBytes(1, 8));

//for bi bit count
bitMapInfoHeader.add(convertIntToBytes(BitmapCreator.BMP_NUM_BITS_FOR_COLORS, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));
//bitMapInfoHeader.add(convertIntToBytes(BitmapCreator.BMP_NUM_BITS_FOR_COLORS, 8));

//for bi compression
bitMapInfoHeader.add(convertIntToBytes(0, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));

//for bi size image
bitMapInfoHeader.add(convertIntToBytes(0, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));

//for bi x pixels per meter
bitMapInfoHeader.add(convertIntToBytes(0, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));

//for bi y pixels per meter
bitMapInfoHeader.add(convertIntToBytes(0, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));

//for bi clr used
bitMapInfoHeader.add(convertIntToBytes(0, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));

//for bi clr important
bitMapInfoHeader.add(convertIntToBytes(0, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));
}

public void setUpFileHeader()
{
//for bfType- should be BM for bitmap- two byte equivalent of ASCII 'B''M'
bitMapFileHeader.add(convertIntToBytes(77, 8)); //'M' reverse order??
bitMapFileHeader.add(convertIntToBytes(66, 8)); //'B'
//bitMapFileHeader.add(convertIntToBytes(19778, 16));

//for bfSize- size of file in bytes
bitMapFileHeader.add(convertIntToBytes(BitmapCreator.BMP_FILE_SIZE, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));
//bitMapFileHeader.add(convertIntToBytes(BitmapCreator.BMP_FILE_SIZE, 8));

//for bfReserved 1 and 2- unused should be 0
bitMapFileHeader.add(convertIntToBytes(0, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));

bitMapFileHeader.add(convertIntToBytes(0, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));

//for bfOffBits- offset into the real data
bitMapFileHeader.add(convertIntToBytes(118, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));
bitMapInfoHeader.add(convertIntToBytes(0, 8));
//bitMapFileHeader.add(convertIntToBytes(118, 8));
}

public void setUpRgbQuadArray()
{
rgbQuadArray.add("11111111111111111111111100000000");
rgbQuadArray.add("00000000000000000000000000000000");
rgbQuadArray.add("11111111111111111111111100000000");
rgbQuadArray.add("00000000000000000000000000000000");
rgbQuadArray.add("11111111111111111111111100000000");
rgbQuadArray.add("00000000000000000000000000000000");
rgbQuadArray.add("11111111111111111111111100000000");
rgbQuadArray.add("00000000000000000000000000000000");
rgbQuadArray.add("11111111111111111111111100000000");
rgbQuadArray.add("00000000000000000000000000000000");
rgbQuadArray.add("11111111111111111111111100000000");
rgbQuadArray.add("00000000000000000000000000000000");
rgbQuadArray.add("11111111111111111111111100000000");
rgbQuadArray.add("00000000000000000000000000000000");
rgbQuadArray.add("11111111111111111111111100000000");
rgbQuadArray.add("00000000000000000000000000000000");
}
public void addPixels(List < Byte > ps)
{
for(int i = 0;i < BitmapCreator.BMP_HEIGHT;i++)
{
for(int j = 0;j < BitmapCreator.BMP_WIDTH;j++)
{
//
//pixels.add(convertIntToBytes((i * j) % 16, 4));
pixels.add(convertIntToBytes(128, 8));
pixels.add(convertIntToBytes(128, 8));
pixels.add(convertIntToBytes(128, 8));
}
}
}

public void writeToFile(String filename)
{
try
{
//create a File object representing the file
File outputFile = new File(filename);
FileOutputStream out = new FileOutputStream(outputFile);

StringBuffer bits = new StringBuffer();

System.out.print("File Header: ");
//add the bit map header file bits
for(int i = 0;i < bitMapFileHeader.size();i++)
{
System.out.print(bitMapFileHeader.get(i) + " ");
bits.append(bitMapFileHeader.get(i));
}
System.out.println();

System.out.print("Info Header: ");
//add the bit map info header bits
for(int i = 0;i < bitMapInfoHeader.size();i++)
{
bits.append(bitMapInfoHeader.get(i));
System.out.print(bitMapInfoHeader.get(i) + " ");
}
System.out.println();

/*
System.out.print("Quad array: ");
//add the bit map info header bits
for(int i = 0;i < rgbQuadArray.size();i++)
{
System.out.print(rgbQuadArray.get(i) + " ");
bits.append(rgbQuadArray.get(i));
}
System.out.println();
*/

System.out.print("Pixels: ");
//add the bit map info header bits
for(int i = 0;i < pixels.size();i++)
{
System.out.print(pixels.get(i) + " ");
bits.append(pixels.get(i));
}
System.out.println();

String bits2 = bits.toString();

for(int i = 0;i < bits2.length();i = i + 8)
{
StringBuffer b = new StringBuffer();

for(int j = 0;j < 8;j++)
{
b.append(bits2.charAt(i + j));
}

System.out.println(Integer.parseInt(b.toString(), 2));
out.write(Integer.parseInt(b.toString(), 2));
}

out.close();
}
catch (Exception e)
{
e.printStackTrace();
}

}
public String convertIntToBytes(int number, int numBits)
{
//list of bytes for the given int
String retVal = null;

//add 31 0's and the binary string of the number passed in (the number passed in
//will produce at least one additional bit to make 32 total bits)
StringBuffer numberBuffer = new StringBuffer("0000000000000000000000000000000" + Integer.toBinaryString(number));

//reverse the buffer, convert to a string, and pull out the first numBits bits
String revBits = numberBuffer.reverse().toString().substring(0, numBits);

//create a new buffer with the reverse bits
numberBuffer = new StringBuffer(revBits);

//reverse them back
numberBuffer.reverse();

//create a string from them
retVal = numberBuffer.toString();

return retVal;
}
public static void main(String[] args)
{
BitmapCreator bm = new BitmapCreator();
bm.writeToFile("markfile.bmp");

}
}

Comments

  • Josh CodeJosh Code Posts: 675Member
    I also implemented some classes for interacting with Windows Bitmap format files. It is submitted here and comes with some documentation that could help you.



    You can get the code here:
    http://www.programmersheaven.com/download/54668/download.aspx


    : I am trying to write a class that will create a simple bitmap using
    : an array of pixel values. I found the format of bmp's here
    : http://www.fortunecity.com/skyscraper/windows/364/bmpffrmt.html
    :
    : It appears that I am creating the file correctly but my bmp's don't
    : show up. Any advice would be much appreciated. Here is the code:
    :
    : import java.util.*;
    : import java.io.*;
    :
    : public class BitmapCreator
    : {
    : private List < String > bitMapFileHeader = new ArrayList< String
    : >();
    : private List < String > bitMapInfoHeader = new ArrayList< String
    : >();
    : private List < String > rgbQuadArray = new ArrayList < String >();
    : private List < String > pixels = new ArrayList < String >();
    :
    : private final static int BMP_WIDTH = 4;
    : private final static int BMP_HEIGHT = 4;
    : private final static int BMP_NUM_BITS_FOR_COLORS = 24;
    : private final static int BMP_PALETTE_SIZE = (int)Math.pow(2.0,
    : (double)BitmapCreator.BMP_NUM_BITS_FOR_COLORS);
    : private final static int BMP_IMAGE_SIZE = BMP_HEIGHT * BMP_WIDTH;
    : private final static int BMP_FILE_SIZE = BMP_IMAGE_SIZE + 40 + 14;
    :
    : public BitmapCreator()
    : {
    : setUpFileHeader();
    : setUpInfoHeader();
    : //setUpRgbQuadArray();
    : addPixels(null);
    : }
    :
    : public void setUpInfoHeader()
    : {
    : //for bi size
    : bitMapInfoHeader.add(convertIntToBytes(40, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : //bitMapInfoHeader.add(convertIntToBytes(40, 8));
    :
    : //for bi width
    : bitMapInfoHeader.add(convertIntToBytes(BitmapCreator.BMP_WIDTH,
    : 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : //bitMapInfoHeader.add(convertIntToBytes(BitmapCreator.BMP_WIDTH,
    : 8));
    :
    : //for bi height
    : bitMapInfoHeader.add(convertIntToBytes(BitmapCreator.BMP_HEIGHT,
    : 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : //bitMapInfoHeader.add(convertIntToBytes(BitmapCreator.BMP_HEIGHT,
    : 8));
    :
    : //for bi planes
    : bitMapInfoHeader.add(convertIntToBytes(1, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : //bitMapInfoHeader.add(convertIntToBytes(1, 8));
    :
    : //for bi bit count
    : bitMapInfoHeader.add(convertIntToBytes(BitmapCreator.BMP_NUM_BITS_F
    : OR_COLORS, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : //bitMapInfoHeader.add(convertIntToBytes(BitmapCreator.BMP_NUM_BITS
    : _FOR_COLORS, 8));
    :
    : //for bi compression
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    :
    : //for bi size image
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    :
    : //for bi x pixels per meter
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    :
    : //for bi y pixels per meter
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    :
    : //for bi clr used
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    :
    : //for bi clr important
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : }
    :
    : public void setUpFileHeader()
    : {
    : //for bfType- should be BM for bitmap- two byte equivalent of
    : ASCII 'B''M'
    : bitMapFileHeader.add(convertIntToBytes(77, 8)); //'M' reverse
    : order??
    : bitMapFileHeader.add(convertIntToBytes(66, 8)); //'B'
    : //bitMapFileHeader.add(convertIntToBytes(19778, 16));
    :
    : //for bfSize- size of file in bytes
    : bitMapFileHeader.add(convertIntToBytes(BitmapCreator.BMP_FILE_SIZE,
    : 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : //bitMapFileHeader.add(convertIntToBytes(BitmapCreator.BMP_FILE_SIZ
    : E, 8));
    :
    : //for bfReserved 1 and 2- unused should be 0
    : bitMapFileHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    :
    : bitMapFileHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    :
    : //for bfOffBits- offset into the real data
    : bitMapFileHeader.add(convertIntToBytes(118, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : //bitMapFileHeader.add(convertIntToBytes(118, 8));
    : }
    :
    : public void setUpRgbQuadArray()
    : {
    : rgbQuadArray.add("11111111111111111111111100000000");
    : rgbQuadArray.add("00000000000000000000000000000000");
    : rgbQuadArray.add("11111111111111111111111100000000");
    : rgbQuadArray.add("00000000000000000000000000000000");
    : rgbQuadArray.add("11111111111111111111111100000000");
    : rgbQuadArray.add("00000000000000000000000000000000");
    : rgbQuadArray.add("11111111111111111111111100000000");
    : rgbQuadArray.add("00000000000000000000000000000000");
    : rgbQuadArray.add("11111111111111111111111100000000");
    : rgbQuadArray.add("00000000000000000000000000000000");
    : rgbQuadArray.add("11111111111111111111111100000000");
    : rgbQuadArray.add("00000000000000000000000000000000");
    : rgbQuadArray.add("11111111111111111111111100000000");
    : rgbQuadArray.add("00000000000000000000000000000000");
    : rgbQuadArray.add("11111111111111111111111100000000");
    : rgbQuadArray.add("00000000000000000000000000000000");
    : }
    : public void addPixels(List < Byte > ps)
    : {
    : for(int i = 0;i < BitmapCreator.BMP_HEIGHT;i++)
    : {
    : for(int j = 0;j < BitmapCreator.BMP_WIDTH;j++)
    : {
    : //
    : //pixels.add(convertIntToBytes((i * j) % 16, 4));
    : pixels.add(convertIntToBytes(128, 8));
    : pixels.add(convertIntToBytes(128, 8));
    : pixels.add(convertIntToBytes(128, 8));
    : }
    : }
    : }
    :
    : public void writeToFile(String filename)
    : {
    : try
    : {
    : //create a File object representing the file
    : File outputFile = new File(filename);
    : FileOutputStream out = new FileOutputStream(outputFile);
    :
    : StringBuffer bits = new StringBuffer();
    :
    : System.out.print("File Header: ");
    : //add the bit map header file bits
    : for(int i = 0;i < bitMapFileHeader.size();i++)
    : {
    : System.out.print(bitMapFileHeader.get(i) + " ");
    : bits.append(bitMapFileHeader.get(i));
    : }
    : System.out.println();
    :
    : System.out.print("Info Header: ");
    : //add the bit map info header bits
    : for(int i = 0;i < bitMapInfoHeader.size();i++)
    : {
    : bits.append(bitMapInfoHeader.get(i));
    : System.out.print(bitMapInfoHeader.get(i) + " ");
    : }
    : System.out.println();
    :
    : /*
    : System.out.print("Quad array: ");
    : //add the bit map info header bits
    : for(int i = 0;i < rgbQuadArray.size();i++)
    : {
    : System.out.print(rgbQuadArray.get(i) + " ");
    : bits.append(rgbQuadArray.get(i));
    : }
    : System.out.println();
    : */
    :
    : System.out.print("Pixels: ");
    : //add the bit map info header bits
    : for(int i = 0;i < pixels.size();i++)
    : {
    : System.out.print(pixels.get(i) + " ");
    : bits.append(pixels.get(i));
    : }
    : System.out.println();
    :
    : String bits2 = bits.toString();
    :
    : for(int i = 0;i < bits2.length();i = i + 8)
    : {
    : StringBuffer b = new StringBuffer();
    :
    : for(int j = 0;j < 8;j++)
    : {
    : b.append(bits2.charAt(i + j));
    : }
    :
    : System.out.println(Integer.parseInt(b.toString(), 2));
    : out.write(Integer.parseInt(b.toString(), 2));
    : }
    :
    : out.close();
    : }
    : catch (Exception e)
    : {
    : e.printStackTrace();
    : }
    :
    : }
    : public String convertIntToBytes(int number, int numBits)
    : {
    : //list of bytes for the given int
    : String retVal = null;
    :
    : //add 31 0's and the binary string of the number passed in (the
    : number passed in
    : //will produce at least one additional bit to make 32 total bits)
    : StringBuffer numberBuffer = new
    : StringBuffer("0000000000000000000000000000000" +
    : Integer.toBinaryString(number));
    :
    : //reverse the buffer, convert to a string, and pull out the first
    : numBits bits
    : String revBits = numberBuffer.reverse().toString().substring(0,
    : numBits);
    :
    : //create a new buffer with the reverse bits
    : numberBuffer = new StringBuffer(revBits);
    :
    : //reverse them back
    : numberBuffer.reverse();
    :
    : //create a string from them
    : retVal = numberBuffer.toString();
    :
    : return retVal;
    : }
    : public static void main(String[] args)
    : {
    : BitmapCreator bm = new BitmapCreator();
    : bm.writeToFile("markfile.bmp");
    :
    : }
    : }
    :
    :
  • Josh CodeJosh Code Posts: 675Member
    I also implemented some classes for interacting with Windows Bitmap format files. It is submitted here and comes with some documentation that could help you.

    There is also an advanced imaging API(jai) that comes with support for bitmaps and other formats.

    You can get the code for my bitmap software here:
    http://www.programmersheaven.com/download/54668/download.aspx


    : I am trying to write a class that will create a simple bitmap using
    : an array of pixel values. I found the format of bmp's here
    : http://www.fortunecity.com/skyscraper/windows/364/bmpffrmt.html
    :
    : It appears that I am creating the file correctly but my bmp's don't
    : show up. Any advice would be much appreciated. Here is the code:
    :
    : import java.util.*;
    : import java.io.*;
    :
    : public class BitmapCreator
    : {
    : private List < String > bitMapFileHeader = new ArrayList< String
    : >();
    : private List < String > bitMapInfoHeader = new ArrayList< String
    : >();
    : private List < String > rgbQuadArray = new ArrayList < String >();
    : private List < String > pixels = new ArrayList < String >();
    :
    : private final static int BMP_WIDTH = 4;
    : private final static int BMP_HEIGHT = 4;
    : private final static int BMP_NUM_BITS_FOR_COLORS = 24;
    : private final static int BMP_PALETTE_SIZE = (int)Math.pow(2.0,
    : (double)BitmapCreator.BMP_NUM_BITS_FOR_COLORS);
    : private final static int BMP_IMAGE_SIZE = BMP_HEIGHT * BMP_WIDTH;
    : private final static int BMP_FILE_SIZE = BMP_IMAGE_SIZE + 40 + 14;
    :
    : public BitmapCreator()
    : {
    : setUpFileHeader();
    : setUpInfoHeader();
    : //setUpRgbQuadArray();
    : addPixels(null);
    : }
    :
    : public void setUpInfoHeader()
    : {
    : //for bi size
    : bitMapInfoHeader.add(convertIntToBytes(40, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : //bitMapInfoHeader.add(convertIntToBytes(40, 8));
    :
    : //for bi width
    : bitMapInfoHeader.add(convertIntToBytes(BitmapCreator.BMP_WIDTH,
    : 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : //bitMapInfoHeader.add(convertIntToBytes(BitmapCreator.BMP_WIDTH,
    : 8));
    :
    : //for bi height
    : bitMapInfoHeader.add(convertIntToBytes(BitmapCreator.BMP_HEIGHT,
    : 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : //bitMapInfoHeader.add(convertIntToBytes(BitmapCreator.BMP_HEIGHT,
    : 8));
    :
    : //for bi planes
    : bitMapInfoHeader.add(convertIntToBytes(1, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : //bitMapInfoHeader.add(convertIntToBytes(1, 8));
    :
    : //for bi bit count
    : bitMapInfoHeader.add(convertIntToBytes(BitmapCreator.BMP_NUM_BITS_F
    : OR_COLORS, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : //bitMapInfoHeader.add(convertIntToBytes(BitmapCreator.BMP_NUM_BITS
    : _FOR_COLORS, 8));
    :
    : //for bi compression
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    :
    : //for bi size image
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    :
    : //for bi x pixels per meter
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    :
    : //for bi y pixels per meter
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    :
    : //for bi clr used
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    :
    : //for bi clr important
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : }
    :
    : public void setUpFileHeader()
    : {
    : //for bfType- should be BM for bitmap- two byte equivalent of
    : ASCII 'B''M'
    : bitMapFileHeader.add(convertIntToBytes(77, 8)); //'M' reverse
    : order??
    : bitMapFileHeader.add(convertIntToBytes(66, 8)); //'B'
    : //bitMapFileHeader.add(convertIntToBytes(19778, 16));
    :
    : //for bfSize- size of file in bytes
    : bitMapFileHeader.add(convertIntToBytes(BitmapCreator.BMP_FILE_SIZE,
    : 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : //bitMapFileHeader.add(convertIntToBytes(BitmapCreator.BMP_FILE_SIZ
    : E, 8));
    :
    : //for bfReserved 1 and 2- unused should be 0
    : bitMapFileHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    :
    : bitMapFileHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    :
    : //for bfOffBits- offset into the real data
    : bitMapFileHeader.add(convertIntToBytes(118, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : bitMapInfoHeader.add(convertIntToBytes(0, 8));
    : //bitMapFileHeader.add(convertIntToBytes(118, 8));
    : }
    :
    : public void setUpRgbQuadArray()
    : {
    : rgbQuadArray.add("11111111111111111111111100000000");
    : rgbQuadArray.add("00000000000000000000000000000000");
    : rgbQuadArray.add("11111111111111111111111100000000");
    : rgbQuadArray.add("00000000000000000000000000000000");
    : rgbQuadArray.add("11111111111111111111111100000000");
    : rgbQuadArray.add("00000000000000000000000000000000");
    : rgbQuadArray.add("11111111111111111111111100000000");
    : rgbQuadArray.add("00000000000000000000000000000000");
    : rgbQuadArray.add("11111111111111111111111100000000");
    : rgbQuadArray.add("00000000000000000000000000000000");
    : rgbQuadArray.add("11111111111111111111111100000000");
    : rgbQuadArray.add("00000000000000000000000000000000");
    : rgbQuadArray.add("11111111111111111111111100000000");
    : rgbQuadArray.add("00000000000000000000000000000000");
    : rgbQuadArray.add("11111111111111111111111100000000");
    : rgbQuadArray.add("00000000000000000000000000000000");
    : }
    : public void addPixels(List < Byte > ps)
    : {
    : for(int i = 0;i < BitmapCreator.BMP_HEIGHT;i++)
    : {
    : for(int j = 0;j < BitmapCreator.BMP_WIDTH;j++)
    : {
    : //
    : //pixels.add(convertIntToBytes((i * j) % 16, 4));
    : pixels.add(convertIntToBytes(128, 8));
    : pixels.add(convertIntToBytes(128, 8));
    : pixels.add(convertIntToBytes(128, 8));
    : }
    : }
    : }
    :
    : public void writeToFile(String filename)
    : {
    : try
    : {
    : //create a File object representing the file
    : File outputFile = new File(filename);
    : FileOutputStream out = new FileOutputStream(outputFile);
    :
    : StringBuffer bits = new StringBuffer();
    :
    : System.out.print("File Header: ");
    : //add the bit map header file bits
    : for(int i = 0;i < bitMapFileHeader.size();i++)
    : {
    : System.out.print(bitMapFileHeader.get(i) + " ");
    : bits.append(bitMapFileHeader.get(i));
    : }
    : System.out.println();
    :
    : System.out.print("Info Header: ");
    : //add the bit map info header bits
    : for(int i = 0;i < bitMapInfoHeader.size();i++)
    : {
    : bits.append(bitMapInfoHeader.get(i));
    : System.out.print(bitMapInfoHeader.get(i) + " ");
    : }
    : System.out.println();
    :
    : /*
    : System.out.print("Quad array: ");
    : //add the bit map info header bits
    : for(int i = 0;i < rgbQuadArray.size();i++)
    : {
    : System.out.print(rgbQuadArray.get(i) + " ");
    : bits.append(rgbQuadArray.get(i));
    : }
    : System.out.println();
    : */
    :
    : System.out.print("Pixels: ");
    : //add the bit map info header bits
    : for(int i = 0;i < pixels.size();i++)
    : {
    : System.out.print(pixels.get(i) + " ");
    : bits.append(pixels.get(i));
    : }
    : System.out.println();
    :
    : String bits2 = bits.toString();
    :
    : for(int i = 0;i < bits2.length();i = i + 8)
    : {
    : StringBuffer b = new StringBuffer();
    :
    : for(int j = 0;j < 8;j++)
    : {
    : b.append(bits2.charAt(i + j));
    : }
    :
    : System.out.println(Integer.parseInt(b.toString(), 2));
    : out.write(Integer.parseInt(b.toString(), 2));
    : }
    :
    : out.close();
    : }
    : catch (Exception e)
    : {
    : e.printStackTrace();
    : }
    :
    : }
    : public String convertIntToBytes(int number, int numBits)
    : {
    : //list of bytes for the given int
    : String retVal = null;
    :
    : //add 31 0's and the binary string of the number passed in (the
    : number passed in
    : //will produce at least one additional bit to make 32 total bits)
    : StringBuffer numberBuffer = new
    : StringBuffer("0000000000000000000000000000000" +
    : Integer.toBinaryString(number));
    :
    : //reverse the buffer, convert to a string, and pull out the first
    : numBits bits
    : String revBits = numberBuffer.reverse().toString().substring(0,
    : numBits);
    :
    : //create a new buffer with the reverse bits
    : numberBuffer = new StringBuffer(revBits);
    :
    : //reverse them back
    : numberBuffer.reverse();
    :
    : //create a string from them
    : retVal = numberBuffer.toString();
    :
    : return retVal;
    : }
    : public static void main(String[] args)
    : {
    : BitmapCreator bm = new BitmapCreator();
    : bm.writeToFile("markfile.bmp");
    :
    : }
    : }
    :
    :
  • zibadianzibadian Posts: 6,349Member
    Why don't you use the ImageIO class instead. This can handle a large number of file-types (including the bitmap).
    All you need to do is to create an image in a BufferedImage class. Then you can call the ImageIO.write() to create the file.
  • kawach123kawach123 Posts: 1Member
    : Why don't you use the ImageIO class instead. This can handle a large
    : number of file-types (including the bitmap).
    : All you need to do is to create an image in a BufferedImage class.
    : Then you can call the ImageIO.write() to create the file.

    Hi
    I wrote below working code, hope it will help every one who wants to
    create bmp file from array of pixels. Once bmp file is created we can manipulate it because Java will automatically add specific headers.


    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    public class FinalBmpCreation {
    public static void main(String[] args) throws IOException {

    String path2 = "D:/Java/eclipse/Test11.bmp";

    int i =0;
    int w = 450;
    int h = 350;
    int [] imageData = new int[w * h];
    for(int y =0;y<h;y++){
    for(int x=0; x<w;x++){
    int r =(x^y)&0xff;
    int g = (x*2^y*2)&0xff;
    int b = (x*4^y*4)&0xff;
    imageData[i++] = (255<<24)|(r <<16)|(g <<8)|b;}}

    BufferedImage finalImage;
    finalImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);//TYPE_INT_ARGB
    finalImage.setRGB(0, 0, w, h, imageData, 0 ,w);
    ImageIO.write(finalImage, "bmp", new File(path2));

    }
    }

    Bye
    Rajiv
  • swatimittalswatimittal Posts: 2Member
    @ Rajiv,

    I have a binary file i need to convert that binary file into bmp file.
    Direct conversion using "ImageIO.write(image, "bmp", OutputPath)" is not working. I have a c# sharp code for same conversion that is working properly.
    I am sending it to you.
    Please convert it into java.

    [CODE]
    private static String CreateBitmap(String filePath, String OutputPath)
    {
    try
    {
    FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
    BinaryReader binary = new BinaryReader(fs);
    byte[] data = binary.ReadBytes((int)fs.Length);
    byte work = 0;

    for (int i = 0; i < data.Length; i += 4)
    {
    work = data[i + 1];
    data[i] = data[i + 3];
    data[i + 1] = data[i + 2];
    data[i + 2] = work;
    data[i + 3] = 255;
    }


    using (Bitmap bmp = new Bitmap(320, 240, PixelFormat.Format32bppArgb))
    {
    BitmapData bd = bmp.LockBits(
    new Rectangle(0, 0, 320, 240),
    ImageLockMode.WriteOnly,
    PixelFormat.Format32bppArgb);

    Marshal.Copy(data, 0, bd.Scan0, data.Length);

    bmp.UnlockBits(bd);

    //Console.WriteLine("Output bmp path : " + OutputPath);
    //Console.Write(".");

    // Only once in a while, saving the BMP fails, so retry up to a number of times (50).
    int iTry = 0;
    while (iTry < 5)
    {
    try
    {
    bmp.Save(OutputPath, ImageFormat.Bmp);
    break;
    }
    catch (Exception e)
    {
    if (iTry == 0)
    Console.WriteLine("BMP save failed: " + OutputPath);
    iTry++;
    System.Console.WriteLine("... try #" + iTry);
    System.Console.WriteLine(e.ToString());
    Thread.Sleep(1000);
    }
    }

    //Console.WriteLine("");
    }

    binary.Close();
    fs.Close();
    }
    catch (Exception e)
    {
    System.Console.WriteLine(e.ToString());
    return e.Message;
    }

    return "";

    }

    [/CODE]


    where file path is bin file path and outpath is bmp file path.
  • swatimittalswatimittal Posts: 2Member
    At last I am able to convert the .bin file into .bmp
    This is the exact code --

    [CODE]
    import java.awt.image.BufferedImage;
    import java.io.DataInputStream;
    import java.io.EOFException;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    public class Test3 {
    public static void main(String[] args) throws IOException {


    int i =0;
    int w = 320;
    int h = 240;
    int imageData[] = new int[w * h];

    FileInputStream file_input = new FileInputStream ("D:\abc.bin");
    DataInputStream data_in = new DataInputStream (file_input );

    while (true) {
    try {
    imageData[i++] = data_in.readInt ();

    }
    catch (EOFException eof) {
    System.out.println ("End of File");
    break;
    }
    }
    BufferedImage finalImage;
    finalImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);//TYPE_INT_ARGB
    finalImage.setRGB(0, 0, w, h, imageData, 0 ,w);
    ImageIO.write(finalImage, "bmp", new File("D:\abc.bmp"));

    }
    }
    [/CODE]
Sign In or Register to comment.