熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

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

2022-06-13   來源: JSP教程 

  要將BufferedImage實例保存為BMP文件就需要知道BMP文件的格式可以參考我轉載的文章《BMP文件格式》
  
  下面是我的將BufferedImage實例保存為位色BMP文件的實現
  
  首先是BMP文件相關的兩個頭結構BMPFileHeader和BMPInfoHeader
  
  /**//*
  * Created on
  *
  * TODO To change the template for this generated file go to
  * Window Preferences Java Code Style Code Templates
  */
  package decbmp;
  
  /**//**
  * <p> Title: BMP文件的頭結構</p>
  *
  * <p> Description: BMP文件的頭結構固定是個字節其定義如下</p>
  * <p>
  * byte[] bfType;          指定文件類型必須是xD即字符串BM也就是說所有bmp文件的頭兩個字節都是BM
  * byte[] bfSize;          指定文件大小包括這個字節
  * byte[] bfReserved;      保留字
  * byte[] bfReserved;      保留字
  * byte[] bfOffBits;        為從文件頭到實際的位圖數據的偏移字節數
  * </p>
  *
  * <p> Copyright: Copyright (c) </p>
  *
  * <p> Company: Lotus</p>
  *
  * @author George Hill
  * @version
  */
  
  class BMPFileHeader {
  
  // Header data
  private byte[] data = new byte[];
  
  public byte[] getData() {
  return thisdata;
  }
  
  // BMP file size
  private int size;
  
  public int getSize() {
  return thissize;
  }
  
  private int offset;
  
  public int getOffset() {
  return thisoffset;
  }
  
  BMPFileHeader(int size int offset) {
  thissize = size;
  thisoffset = offset;
  
  data[] = B;
  data[] = M;
  
  int value = size;
  data[] = (byte) value;
  value = value >>> ;
  data[] = (byte) value;
  value = value >>> ;
  data[] = (byte) value;
  value = value >>> ;
  data[] = (byte) value;
  
  value = offset;
  data[] = (byte) value;
  value = value >>> ;
  data[] = (byte) value;
  value = value >>> ;
  data[] = (byte) value;
  value = value >>> ;
  data[] = (byte) value;
  }
  
  }
  
  /**//*
  * Created on
  *
  * TODO To change the template for this generated file go to
  * Window Preferences Java Code Style Code Templates
  */
  package decbmp;
  
  /**//**
  * <p>Title: BMP文件內容的頭結構</p>
  *
  * <p>Description: BMP文件內容的頭結構固定是個字節其定義如下</p>
  * <p>
  * byte[] biSize;              指定這個結構的長度
  * byte[] biWidth;              指定圖象的寬度單位是象素
  * byte[] biHeight;            指定圖象的高度單位是象素
  * byte[] biPlanes;            必須是不用考慮
  * byte[] biBitCount;          指定表示顏色時要用到的位數常用的值為(黑白二色圖) (色圖) (色) (真彩色圖)
  * byte[] biCompression;        指定位圖是否壓縮
  * byte[] biSizeImage;          指定實際的位圖數據占用的字節數
  * byte[] biXPelsPerMeter;      指定目標設備的水平分辨率單位是每米的象素個數
  * byte[] biYPelsPerMeter;      指定目標設備的垂直分辨率單位是每米的象素個數
  * byte[] biClrUsed;            指定本圖象實際用到的顏色數如果該值為零則用到的顏色數為biBitCount
  * byte[] biClrImportant;      指定本圖象中重要的顏色數如果該值為零則認為所有的顏色都是重要的
  * </p>
  *
  * <p>Copyright: Copyright (c) </p>
  *
  * <p>Company: Lotus</p>
  *
  * @author George Hill
  * @version
  */
  
  class BMPInfoHeader {
  
  private byte[] data = new byte[];
  
  public byte[] getData() {
  return thisdata;
  }
  
  private int width;
  
  public int getWidth() {
  return thiswidth;
  }
  
  private int height;
  
  public int getHeight() {
  return thisheight;
  }
  
  public int bitCount;
  
  public int getBitCount() {
  return thisbitCount;
  }
  
  public BMPInfoHeader(int width int height int bitCount) {
  thiswidth = width;
  thisheight = height;
  thisbitCount = bitCount;
  
  data[] = ;
  
  int value = width;
  data[] = (byte) value;
  value = value >>> ;
  data[] = (byte) value;
  value = value >>> ;
  data[] = (byte) value;
  value = value >>> ;
  data[] = (byte) value;
  
  value = height;
  data[] = (byte) value;
  value = value >>> ;
  data[] = (byte) value;
  value = value >>> ;
  data[] = (byte) value;
  value = value >>> ;
  data[] = (byte) value;
  
  data[] = ;
  
  data[] = (byte) bitCount;
  
  value = width * height * ;
  if (width % != )
  value += (width % ) * height;
  data[] = (byte) value;
  value = value >>> ;
  data[] = (byte) value;
  value = value >>> ;
  data[] = (byte) value;
  value = value >>> ;
  data[] = (byte) value;
  }
  
  }
  
  仿照decjpegJPEGImageEncoder寫的接口類BMPEncoder
  
  /**//*
  * Created on
  *
  * TODO To change the template for this generated file go to
  * Window Preferences Java Code Style Code Templates
  */
  package decbmp;
  
  import javaawtimage*;
  import javaioIOException;
  
  /**//**
  * <p>Title: </p>
  *
  * <p>Description: </p>
  *
  * <p>Copyright: Copyright (c) </p>
  *
  * <p>Company: Lotus</p>
  *
  * @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_COLORS = ;
  public static final int BIT_COUNT_COLORS = ;
  public static final int BIT_COUNT_TRUECOLORS = ;
  
  }
  
  BMPEncoder接口的實現BMPEncoderImpl
  
  /**//*
  * Created on
  *
  * TODO To change the template for this generated file go to
  * Window Preferences Java Code Style Code Templates
  */
  package decbmp;
  
  import javaawtimage*;
  import javaio*;
  
  /**//**
  * <p>Title: </p>
  *
  * <p>Description: </p>
  *
  * <p>Copyright: Copyright (c) </p>
  *
  * <p>Company: Lotus</p>
  *
  * @author George Hill
  * @version
  */
  
  class BMPEncoderImpl implements BMPEncoder {
  
  private OutputStream out;
  
  public BMPEncoderImpl(OutputStream out) {
  thisout = out;
  }
  
  public void encode(BufferedImage bi) throws IOException {
  int width = bigetWidth();
  int height = bigetHeight();
  
  boolean needBlank = (width % != );
  
  int size = width * height * ;
  if (needBlank) {
  size += (width % ) * height;
  }
  
  BMPFileHeader fileHeader = new BMPFileHeader(size );
  BMPInfoHeader infoHeader = new BMPInfoHeader(width height BIT_COUNT_TRUECOLORS);
  
  byte[] rgbs = new byte[];
  byte[] blank = new byte[width %
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19624.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.