国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

如何將BufferedImage實例保存為BMP文件

2019-11-18 13:08:17
字體:
來源:轉載
供稿:網友

  要將BufferedImage實例保存為BMP文件,就需要知道BMP文件的格式,可以參考我轉載的文章:《BMP文件格式》。
  
  下面是我的將BufferedImage實例保存為24位色BMP文件的實現。
  
  首先是BMP文件相關的兩個頭結構:BMPFileHeader和BMPInfoHeader。
  
  /**//*
  * Created on 2005-6-21
  *
  * TODO To change the template for this generated file go to
  * Window - PReferences - java - Code Style - Code Templates
  */
  package org.lotus.image.codec.bmp;
  
  /**//**
  * <p> Title: BMP文件的頭結構</p>
  *
  * <p> Description: BMP文件的頭結構固定是14個字節,其定義如下:</p>
  * <p>
  * byte[2] bfType;          指定文件類型,必須是0x424D,即字符串“BM”,也就是說所有.bmp文件的頭兩個字節都是“BM“
  * byte[4] bfSize;          指定文件大小,包括這14個字節
  * byte[2] bfReserved1;      保留字
  * byte[2] bfReserved2;      保留字
  * byte[4] bfOffBits;        為從文件頭到實際的位圖數據的偏移字節數
  * </p>
  *
  * <p> Copyright: Copyright (c) 2005</p>
  *
  * <p> Company: 21Lotus</p>
  *
  * @author George Hill
  * @version 1.0
  */
  
  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;
  }
  
  }
  
  /**//*
  * Created on 2005-6-21
  *
  * TODO To change the template for this generated file go to
  * Window - Preferences - Java - Code Style - Code Templates
  */
  package org.lotus.image.codec.bmp;
  
  /**//**
  * <p>Title: BMP文件內容的頭結構</p>
  *
  * <p>Description: BMP文件內容的頭結構固定是40個字節,其定義如下:</p>
  * <p>
  * byte[4] biSize;              指定這個結構的長度,為40
  * byte[4] biWidth;              指定圖象的寬度,單位是象素
  * byte[4] biHeight;            指定圖象的高度,單位是象素
  * byte[2] biplanes;            必須是1,不用考慮
  * byte[2] biBitCount;          指定表示顏色時要用到的位數,常用的值為1(黑白二色圖), 4(16色圖), 8(256色), 24(真彩色圖)
  * byte[4] biCompression;        指定位圖是否壓縮
  * byte[4] biSizeImage;          指定實際的位圖數據占用的字節數
  * byte[4] biXPelsPerMeter;      指定目標設備的水平分辨率,單位是每米的象素個數
  * byte[4] biYPelsPerMeter;      指定目標設備的垂直分辨率,單位是每米的象素個數
  * byte[4] biClrUsed;            指定本圖象實際用到的顏色數,假如該值為零,則用到的顏色數為2biBitCount
  * byte[4] biClrImportant;      指定本圖象中重要的顏色數,假如該值為零,則認為所有的顏色都是重要的
  * </p>
  *
  * <p>Copyright: Copyright (c) 2005</p>
  *
  * <p>Company: 21Lotus</p>
  *
  * @author George Hill
  * @version 1.0
  */
  
  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寫的接口類BMPEncoder。
  
  /**//*
  * Created on 2005-6-21
  *
  * TODO To change the template for this generated file go to
  * Window - Preferences - Java - Code Style - Code Templates
  */
  package org.lotus.image.codec.bmp;
  
  import java.awt.image.*;
  import java.io.IOException;
  
  /**//**
  * <p>Title: </p>
  *
  * <p>Description: </p>
  *
  * <p>Copyright: Copyright (c) 2005</p>
  *
  * <p>Company: 21Lotus</p>
  *
  * @author George Hill
  * @version 1.0
  */
  
  public 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接口的實現BMPEncoderImpl。
  
  /**//*
  * Created on 2005-6-21
  *
  * TODO To change the template for this generated file go to
  * Window - Preferences - Java - Code Style - Code Templates
  */
  package org.lotus.image.codec.bmp;
  
  import java.awt.image.*;
  import java.io.*;
  
  /**//**
  * <p>Title: </p>
  *
  * <p>Description: </p>
  *
  * <p>Copyright: Copyright (c) 2005</p>
  *
  * <p>Company: 21Lotus</p>
  *
  * @author George Hill
  * @version 1.0
  */
  
  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 %

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 逊克县| 白山市| 自贡市| 宁远县| 沭阳县| 佳木斯市| 三亚市| 宜城市| 定襄县| 苏尼特右旗| 加查县| 临颍县| 克拉玛依市| 广德县| 玉树县| 丰县| 青岛市| 广饶县| 雷波县| 英山县| 福州市| 绍兴市| 西乌珠穆沁旗| 茂名市| 个旧市| 青阳县| 西盟| 德州市| 双桥区| 山阳县| 永修县| 永平县| 洛扎县| 磐石市| 乐业县| 西城区| 信丰县| 凤城市| 南投市| 玉山县| 昌图县|