从位图解码
/* * 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.nowjava; import java.io.File; /* from 时代Java公众号 - nowjava.com*/ 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); /** * 时 代 J a v a 公 众 号 - nowjava.com 提 供 **/ } 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(