麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

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

如何將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 %

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 久久久久久久久久久久久久久久久久 | 亚洲免费视| 99seav| 成人444kkkk在线观看 | 黄色大片在线免费看 | 成人免费午夜视频 | 久久华人 | 国产69精品久久久久久 | 蜜桃网站在线观看 | 伊人yinren22综合网色 | 182tv成人福利视频免费看 | 在线免费亚洲 | 97超碰资源站 | 青青国产在线视频 | 日韩黄色免费观看 | 91,视频免费看 | 久久思思爱| 精品一区二区久久久久 | 久久久久久久久久久久久九 | 精品一区二区三区中文字幕 | 一级一级一级一级毛片 | 羞羞视频免费观看网站 | 欧美一级毛片一级毛片 | 免费在线国产 | 精品国产91久久久久久浪潮蜜月 | 国产成人小视频在线观看 | 黄在线免费 | 一区二区三区在线观看视频 | 啊~用cao嗯力cao烂我视频 | av一道本 | 久国产 | 男女羞羞视频在线观看免费 | 国产激情精品一区二区三区 | 一区二区三区在线观看免费视频 | 黄色午夜剧场 | 视频一区二区三区免费观看 | 黄色特级视频 | 久久久午夜电影 | 欧美一级免费在线观看 | 毛片毛片 | 在线观看免费毛片视频 |