I am trying to calculate a BMP image and put its bytes manually, every thing is gone well, but about bytes 34-37 which are the pixel size of the BMP, I think I am wrong some where, because I don't get the true values, what I am doing is like this :
width = 68 , height = 19 ;
int pixels = new int[68 * 19] = 1292
//... 17 buffers written
int row = height;
int col = width;
int startPosition = (row - 1) * col;
int endPosition = row * col;
while( row > 0 ){
for(int i = startPosition; i < endPosition; i++ ){
buffer.put((byte)(pixels[i] & 0x000000FF));
buffer.put((byte)((pixels[i] & 0x0000FF00) >> 8));
buffer.put((byte)((pixels[i] & 0x00FF0000) >> 16));
}
if(hasDummy){
buffer.put(dummyBytesPerRow);
}
row--;
endPosition = startPosition;
startPosition = startPosition - col;
}
But my output when I am reading the bitmap bytes using this method :
getInt(getBytes(data, 34, 4))
private byte[] getBytes(byte[] data, int start, int length) {
byte[] temp = new byte[length];
System.arraycopy(data, start, temp, 0, temp.length);
return temp;
}
private int getInt(byte[] byteArray) {
int temp = 0;
temp |= byteArray[0] & 0x000000ff;
Log.i("whw", "temp0=" + Integer.toHexString(temp));
temp |= (byteArray[1] << 8);
Log.i("whw", "temp1=" + Integer.toHexString(temp));
temp |= byteArray[2] << 16;
Log.i("whw", "temp2=" + Integer.toHexString(temp));
temp |= byteArray[3] << 24;
Log.i("whw", "temp3=" + Integer.toHexString(temp));
return temp;
}
Returns 3876, while expected 1292.
Is there any thing wrong with my coding?