java – Merge images into one file without creating the full image in memory-ThrowExceptions

Exception or error:

I would need to “chunk process” some Bitmaps of any size in Android. I can easily use BitmapRegionDecoder to create smaller chunks for processing, but once done, I need to reassemble them into one file. Because the final image can be any size, creating the corresponding Bitmap in memory and writing on it using a Canvas is not an option.

I have seen this thread but I would like to do this without external library (I am limited with these as using Android). Ideally I am after some kind of BitmapRegionEncoder. I do not really care about the output format as long as it is image (PNG, JPG or even BMP). I am also happy to do it in C using JNI if not possible in Java.

How to solve:

A simple way of doing this is to store the chunks in a table structure, then read them back and write the data to back to file as image file.

Here is a sample table structure.

public class Chunk
    private int chunkId;
    private byte[] chunkdata;
    private int nextchunkId;

Method to read a chunk from the table

private Chunk getChunk(int index){
   Chunk chunk = null; 
   if(index == 1){ // this assumes that the chunk id starts from 1
      //get and return Chunk where chunkId == 1 from the table
      // get and return Chunk where nextchunkId == index from the table
   return chunk

Now write the chunks to a binary file directly

private void mergeChunksToFile(){
   int index = 1; // this assumes that the chunk id starts from 1
   // Create a binary file in append mode to store the data, which is the image
   Chunk chunk = getChunk(index);
   while(chunk != null){
      // Here, write chunk.chunkdata to the binary file

      index = chunk.nextchunkId;

      // get the next chunk
      chunk = getChunk(index);

This might not be the best solution, but it should assist you have an idea of how to go about it without using any external library


What I ended up doing:

public class BitmapChunkWriter {

    private static final int BMP_WIDTH_OF_TIMES = 4;
    private static final int BYTE_PER_PIXEL = 3;

    private FileOutputStream fos;
    private byte[] dummyBytesPerRow;
    private boolean hasDummy;
    private int imageSize;
    private int fileSize;
    private int rowWidthInBytes;

     * Android Bitmap Object to Window's v3 24bit Bmp Format File
     * @param imageWidth
     * @param imageHeight
     * @param filePath
     * @return file saved result
    public void writeHeader(int imageWidth, int imageHeight, String filePath) throws IOException {

        //image dummy data size
        //reason : the amount of bytes per image row must be a multiple of 4 (requirements of bmp format)
        dummyBytesPerRow = null;
        hasDummy = false;
        rowWidthInBytes = BYTE_PER_PIXEL * imageWidth; //source image width * number of bytes to encode one pixel.
        if (rowWidthInBytes % BMP_WIDTH_OF_TIMES > 0) {
            hasDummy = true;
            //the number of dummy bytes we need to add on each row
            dummyBytesPerRow = new byte[(BMP_WIDTH_OF_TIMES - (rowWidthInBytes % BMP_WIDTH_OF_TIMES))];
            //just fill an array with the dummy bytes we need to append at the end of each row
            for (int i = 0; i < dummyBytesPerRow.length; i++) {
                dummyBytesPerRow[i] = (byte) 0xFF;

        //the number of bytes used in the file to store raw image data (excluding file headers)
        imageSize = (rowWidthInBytes + (hasDummy ? dummyBytesPerRow.length : 0)) * imageHeight;
        //file headers size
        int imageDataOffset = 0x36;

        //final size of the file
        fileSize = imageSize + imageDataOffset;

        //ByteArrayOutputStream baos = new ByteArrayOutputStream(fileSize);
        ByteBuffer buffer = ByteBuffer.allocate(imageDataOffset);

         * BITMAP FILE HEADER Write Start
        buffer.put((byte) 0x42);
        buffer.put((byte) 0x4D);


        buffer.put(writeShort((short) 0));
        buffer.put(writeShort((short) 0));

        //image data start offset

        /** BITMAP FILE HEADER Write End */


        /** BITMAP INFO HEADER Write Start */

        //width, height
        //if we add 3 dummy bytes per row : it means we add a pixel (and the image width is modified.
        buffer.put(writeInt(imageWidth + (hasDummy ? (dummyBytesPerRow.length == 3 ? 1 : 0) : 0)));

        buffer.put(writeShort((short) 1));

        //bit count
        buffer.put(writeShort((short) 24));

        //bit compression

        //image data size

        //horizontal resolution in pixels per meter

        //vertical resolution in pixels per meter (unreliable)



        fos = new FileOutputStream(filePath);

    public void writeChunk(Bitmap bitmap) throws IOException {

        int chunkWidth = bitmap.getWidth();
        int chunkHeight = bitmap.getHeight();

        //an array to receive the pixels from the source image
        int[] pixels = new int[chunkWidth * chunkHeight];
        //Android Bitmap Image Data
        bitmap.getPixels(pixels, 0, chunkWidth, 0, 0, chunkWidth, chunkHeight);

        //the number of bytes used in the file to store raw image data (excluding file headers)
        //int imageSize = (rowWidthInBytes + (hasDummy ? dummyBytesPerRow.length : 0)) * height;

        int chunkSize = (rowWidthInBytes + (hasDummy ? dummyBytesPerRow.length : 0)) * chunkHeight;
        ByteBuffer buffer = ByteBuffer.allocate(chunkSize);

        int row = chunkHeight;
        int col = chunkWidth;
        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));
            endPosition = startPosition;
            startPosition = startPosition - col;


    public void finish() throws IOException {

     * Write integer to little-endian
     * @param value
     * @return
     * @throws IOException
    private static byte[] writeInt(int value) throws IOException {
        byte[] b = new byte[4];

        b[0] = (byte)(value & 0x000000FF);
        b[1] = (byte)((value & 0x0000FF00) >> 8);
        b[2] = (byte)((value & 0x00FF0000) >> 16);
        b[3] = (byte)((value & 0xFF000000) >> 24);

        return b;

     * Write short to little-endian byte array
     * @param value
     * @return
     * @throws IOException
    private static byte[] writeShort(short value) throws IOException {
        byte[] b = new byte[2];

        b[0] = (byte)(value & 0x00FF);
        b[1] = (byte)((value & 0xFF00) >> 8);

        return b;

Leave a Reply

Your email address will not be published. Required fields are marked *