要將BufferedImage實例保存為BMP文件
下面是我的將BufferedImage實例保存為
首先是BMP文件相關的兩個頭結構
/**//*
* Created on
*
* TODO To change the template for this generated file go to
* Window
*/
package dec
/**//**
* <p> Title: BMP文件的頭結構</p>
*
* <p> Description: BMP文件的頭結構固定是
* <p>
* byte[
* byte[
* byte[
* byte[
* byte[
* </p>
*
* <p> Copyright: Copyright (c)
*
* <p> Company:
*
* @author George Hill
* @version
*/
class BMPFileHeader {
// Header data
private byte[] data = new byte[
public byte[] getData() {
return this
}
// BMP file size
private int size;
public int getSize() {
return this
}
private int offset;
public int getOffset() {
return this
}
BMPFileHeader(int size
this
this
data[
data[
int value = size;
data[
value = value >>>
data[
value = value >>>
data[
value = value >>>
data[
value = offset;
data[
value = value >>>
data[
value = value >>>
data[
value = value >>>
data[
}
}
/**//*
* Created on
*
* TODO To change the template for this generated file go to
* Window
*/
package dec
/**//**
* <p>Title: BMP文件內容的頭結構</p>
*
* <p>Description: BMP文件內容的頭結構固定是
* <p>
* byte[
* byte[
* byte[
* byte[
* byte[
* byte[
* byte[
* byte[
* byte[
* byte[
* byte[
* </p>
*
* <p>Copyright: Copyright (c)
*
* <p>Company:
*
* @author George Hill
* @version
*/
class BMPInfoHeader {
private byte[] data = new byte[
public byte[] getData() {
return this
}
private int width;
public int getWidth() {
return this
}
private int height;
public int getHeight() {
return this
}
public int bitCount;
public int getBitCount() {
return this
}
public BMPInfoHeader(int width
this
this
this
data[
int value = width;
data[
value = value >>>
data[
value = value >>>
data[
value = value >>>
data[
value = height;
data[
value = value >>>
data[
value = value >>>
data[
value = value >>>
data[
data[
data[
value = width * height *
if (width %
value += (width %
data[
value = value >>>
data[
value = value >>>
data[
value = value >>>
data[
}
}
仿照dec
/**//*
* Created on
*
* TODO To change the template for this generated file go to
* Window
*/
package dec
import java
import java
/**//**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c)
*
* <p>Company:
*
* @author George Hill
* @version
*/
public interface BMPEncoder {
public void encode(BufferedImage bi) throws IOException;
public static final int BIT_COUNT_BLACKWHITE =
public static final int BIT_COUNT_
public static final int BIT_COUNT_
public static final int BIT_COUNT_TRUECOLORS =
}
BMPEncoder接口的實現BMPEncoderImpl
/**//*
* Created on
*
* TODO To change the template for this generated file go to
* Window
*/
package dec
import java
import java
/**//**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c)
*
* <p>Company:
*
* @author George Hill
* @version
*/
class BMPEncoderImpl implements BMPEncoder {
private OutputStream out;
public BMPEncoderImpl(OutputStream out) {
this
}
public void encode(BufferedImage bi) throws IOException {
int width = bi
int height = bi
boolean needBlank = (width %
int size = width * height *
if (needBlank) {
size += (width %
}
BMPFileHeader fileHeader = new BMPFileHeader(size
BMPInfoHeader infoHeader = new BMPInfoHeader(width
byte[] rgbs = new byte[
byte[] blank = new byte[width %
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19624.html