decode From Bitmap - Java Language Basics

Java examples for Language Basics:Bit

Description

decode From Bitmap

Demo Code

/*/*from  w ww.  j a  v  a  2s.c  o  m*/
 * Syncany, www.syncany.org
 * Copyright (C) 2011 Philipp C. Heckel <philipp.heckel@gmail.com> 
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
//package com.java2s;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    public static void main(String[] argv) throws Exception {
        File srcFile = new File("Main.java");
        File dstFile = new File("Main.java");
        decodeFromBitmap(srcFile, dstFile);
    }

    private static final int BMP_OFFSET_IMAGE_WIDTH = 18;

    public static void decodeFromBitmap(File srcFile, File dstFile)
            throws Exception {
        // NOTE: For some reason, decoding from a non-FileInputStream does not
        //       always work properly. Do not create a method 
        //       decodeFromBitmap(InputStream, ...)

        InputStream is = new FileInputStream(srcFile);

        // Read BMP width from header
        is.skip(BMP_OFFSET_IMAGE_WIDTH);

        byte[] imageWidthBytes = new byte[4];
        is.read(imageWidthBytes);
        int imageWidth = toIntLE(imageWidthBytes);

        // Row length must be divisible by 4; calculate padding
        int linePadding = 4 - (imageWidth * 3 % 4);

        // Skip to the 'horizontal resolution' field
        is.skip(16);
        byte[] payloadLengthBytes = new byte[4];
        is.read(payloadLengthBytes);
        int payloadLength = toIntLE(payloadLengthBytes);

        is.skip(12);//BMP_HEADER_LENGTH - OFFSET_IMAGE_WIDTH - 4);   

        OutputStream os = new FileOutputStream(dstFile);

        byte[] row = new byte[imageWidth * 3];
        int read;
        int restOfPayload = payloadLength;

        while ((read = is.read(row)) != -1) {
            if (restOfPayload >= read) {
                os.write(row, 0, read);
                is.skip(linePadding); // skip padding

                restOfPayload -= read;
            } else {
                os.write(row, 0, restOfPayload);
                break;
            }
        }

        is.close();
        os.close();
    }

    private static int toIntLE(byte[] value) {
        return ((value[3] & 0xff) << 24) | ((value[2] & 0xff) << 16)
                | ((value[1] & 0xff) << 8) | (value[0] & 0xff);
    }
}

Related Tutorials