小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

保存BMP圖像的JAVA類(lèi)

 ytkomn 2011-09-19

將BufferedImage保存為BMP格式的文件,可以下載SUN公司的JAI,調(diào)用里面的API;或者自己編寫(xiě)類(lèi),按照BMP的格式逐個(gè)寫(xiě)入.

下面的類(lèi)實(shí)現(xiàn)了該功能,主類(lèi)為BMPSaver.

import java.awt.image.*;
import java.io.*;
import java.awt.image.*;
import java.io.IOException;
import java.io.OutputStream;
/**//**
* Title: BMP文件的頭結(jié)構(gòu)
*
* Description: BMP文件的頭結(jié)構(gòu)固定是14個(gè)字節(jié),其定義如下:
*
* byte[2] bfType; 指定文件類(lèi)型,必須是0x424D,即字符串“BM”,也就是說(shuō)所有.bmp文件的頭兩個(gè)字節(jié)都是“BM“
* byte[4] bfSize; 指定文件大小,包括這14個(gè)字節(jié)
* byte[2] bfReserved1; 保留字
* byte[2] bfReserved2; 保留字
* byte[4] bfOffBits; 為從文件頭到實(shí)際的位圖數(shù)據(jù)的偏移字節(jié)數(shù)
*/

class BMPFileHeader
{

// Header data
private byte[] data = new byte[14];

public byte[] getData() {
return this.data;
}

// BMP file size
private int size;

public int getSize() {
return this.size;
}

private int offset;

public int getOffset() {
return this.offset;
}

BMPFileHeader(int size, int offset) {
this.size = size;
this.offset = offset;

data[0] = 'B';
data[1] = 'M';

int value = size;
data[2] = (byte) value;
value = value >>> 8;
data[3] = (byte) value;
value = value >>> 8;
data[4] = (byte) value;
value = value >>> 8;
data[5] = (byte) value;

value = offset;
data[10] = (byte) value;
value = value >>> 8;
data[11] = (byte) value;
value = value >>> 8;
data[12] = (byte) value;
value = value >>> 8;
data[13] = (byte) value;
}

}


/**//**
* Title: BMP文件內(nèi)容的頭結(jié)構(gòu)
*
* Description: BMP文件內(nèi)容的頭結(jié)構(gòu)固定是40個(gè)字節(jié),其定義如下:
*
* byte[4] biSize; 指定這個(gè)結(jié)構(gòu)的長(zhǎng)度,為40
* byte[4] biWidth; 指定圖象的寬度,單位是象素
* byte[4] biHeight; 指定圖象的高度,單位是象素
* byte[2] biPlanes; 必須是1,不用考慮
* byte[2] biBitCount; 指定表示顏色時(shí)要用到的位數(shù),常用的值為1(黑白二色圖), 4(16色圖), 8(256色), 24(真彩色圖)
* byte[4] biCompression; 指定位圖是否壓縮
* byte[4] biSizeImage; 指定實(shí)際的位圖數(shù)據(jù)占用的字節(jié)數(shù)
* byte[4] biXPelsPerMeter; 指定目標(biāo)設(shè)備的水平分辨率,單位是每米的象素個(gè)數(shù)
* byte[4] biYPelsPerMeter; 指定目標(biāo)設(shè)備的垂直分辨率,單位是每米的象素個(gè)數(shù)
* byte[4] biClrUsed; 指定本圖象實(shí)際用到的顏色數(shù),如果該值為零,則用到的顏色數(shù)為2biBitCount
* byte[4] biClrImportant; 指定本圖象中重要的顏色數(shù),如果該值為零,則認(rèn)為所有的顏色都是重要的
*
*/

class BMPInfoHeader {

private byte[] data = new byte[40];

public byte[] getData() {
return this.data;
}

private int width;

public int getWidth() {
return this.width;
}

private int height;

public int getHeight() {
return this.height;
}

public int bitCount;

public int getBitCount() {
return this.bitCount;
}

public BMPInfoHeader(int width, int height, int bitCount) {
this.width = width;
this.height = height;
this.bitCount = bitCount;

data[0] = 40;

int value = width;
data[4] = (byte) value;
value = value >>> 8;
data[5] = (byte) value;
value = value >>> 8;
data[6] = (byte) value;
value = value >>> 8;
data[7] = (byte) value;

value = height;
data[8] = (byte) value;
value = value >>> 8;
data[9] = (byte) value;
value = value >>> 8;
data[10] = (byte) value;
value = value >>> 8;
data[11] = (byte) value;

data[12] = 1;

data[14] = (byte) bitCount;

value = width * height * 3;
if (width % 4 != 0)
value += (width % 4) * height;
data[20] = (byte) value;
value = value >>> 8;
data[21] = (byte) value;
value = value >>> 8;
data[22] = (byte) value;
value = value >>> 8;
data[23] = (byte) value;
}

}

//仿照com.sun.image.codec.jpeg.JPEGImageEncoder寫(xiě)的接口類(lèi)BMPEncoder。

 

 

interface BMPEncoder {

public void encode(BufferedImage bi) throws IOException;

public static final int BIT_COUNT_BLACKWHITE = 1;
public static final int BIT_COUNT_16COLORS = 4;
public static final int BIT_COUNT_256COLORS = 8;
public static final int BIT_COUNT_TRUECOLORS = 24;

}

//BMPEncoder接口的實(shí)現(xiàn)BMPEncoderImpl。

class BMPEncoderImpl implements BMPEncoder {

private OutputStream out;

public BMPEncoderImpl(OutputStream out) {
this.out = out;
}

public void encode(BufferedImage bi) throws IOException
{
int width = bi.getWidth();
int height = bi.getHeight();

boolean needBlank = (width % 4 != 0);

int size = width * height * 3;
if (needBlank) {
size += (width % 4) * height;
}

BMPFileHeader fileHeader = new BMPFileHeader(size, 54);
BMPInfoHeader infoHeader = new BMPInfoHeader(width, height, BIT_COUNT_TRUECOLORS);

byte[] rgbs = new byte[3];
byte[] blank = new byte[width % 4];

out.write(fileHeader.getData());
out.write(infoHeader.getData());

int index = 0;
for (int y = height - 1; y >= 0; y--)
{
for (int x = 0; x < width; x++)
{
index += 3;

int rgb = bi.getRGB(x, y);
rgbs[0] = (byte) rgb;
rgb = rgb >>> 8;
rgbs[1] = (byte) rgb;
rgb = rgb >>> 8;
rgbs[2] = (byte) rgb;

out.write(rgbs);

if (needBlank && (index % (width * 3) == 0))
{
out.write(blank);
}
}
}
}

}

//一個(gè)工廠類(lèi)BMPCodec。

class BMPCodec {

public static BMPEncoder createBMPEncoder(OutputStream dest) {
return new BMPEncoderImpl(dest);
}

}

public class BMPSaver
{
public BMPSaver(FileOutputStream out,BufferedImage image) throws Exception
{
BMPCodec.createBMPEncoder(out).encode(image);
}
};

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類(lèi)似文章 更多