欢迎光临
我们一直在努力

用 Java 保存位图文件-JSP教程,Java技巧及代码

建站超值云服务器,限时71元/月

摘要

  虽然 java 提供了几种打开图像的机制,但保存图像并不是它的强项。这篇技巧将讲述如何将图像保存在 24 位位图文件中。另外,jean-pierre 还提供了将图像文件写入位图文件所需的全部代码。

  这篇技巧是 "在 java 应用程序中加载位图文件的逐步指南" 的补充,那篇技巧说明了在 java 应用程序中加载位图文件的过程。本月我再提供一篇教程,说明如何将图像保存在 24 位位图文件中,其中还包含将图像对象写入位图文件的代码片断。

  如果您在 microsoft windows 环境中工作,那么创建位图文件的功能将为您提供许多方便。例如,在我的上一个项目中,我必须将 java 与 microsoft access 对接。java 程序允许用户在屏幕上绘图。这幅图随后被打印到 microsoft access 报表中。由于 java 不支持 ole,我的唯一选择就是创建该图的一个位图文件,并通知 microsoft access 报表在何处能找到这个位图文件。如果您写过向剪贴板发送图像的应用程序,则这个技巧可能对您有用 — 尤其是当您将这个信息传递给另一个应用程序时。

  位图文件的格式

  位图文件格式支持 4 位 rle(行程长度编码)以及 8 位和 24 位编码。因为我们只处理 24 位格式,所以下面我们查看一下该文件的结构。

  位图文件分为三个部分。我已将它们列在下面。

  第 1 部分:位图文件的标头

  标头包含位图文件的类型大小信息和版面信息。结构如下(摘自 c 语言结构定义):

 

typedef struct tagbitmapfileheader {

   uint bftype;

   dword bfsize;

   uint bfreserved1;

   uint bfreserved2;

   dword bfoffbits;

  }bitmapfileheader;

 下面是对这个清单中的代码元素的说明:

  bftype:指定文件类型,其值始终为 bm。

  bfsize:指定整个文件的大小(以字节为单位)。

  bfreserved1:保留 — 必须为 0。

  bfreserved2:保留 — 必须为 0。

  bfoffbits:指定从 bitmapfileheader 到图像首部的字节偏移量。

  现在您已经明白位图标头的用途就是标识位图文件。读取位图文件的每个程序都使用位图标头来进行文件验证。

  第 2 部分:位图信息标头

  随后的标头称为信息标头,其中包含图像本身的属性。

  下面说明如何指定 windows 3.0(或更高版本)设备独立位图 (dib) 的大小和颜色格式:

  typedef struct tagbitmapinfoheader {

    dword bisize;

    long biwidth;

    long biheight;

    word biplanes;

    word bibitcount;

    dword bicompression;

    dword bisizeimage;

    long bixpelspermeter;

    long biypelspermeter;

    dword biclrused;

    dword biclrimportant;

  } bitmapinfoheader;

  

  以上代码清单的每个元素说明如下:

   bisize:指定 bitmapinfoheader 结构所需的字节数。

   biwidth:指定位图的宽度(以象素为单位)。

   biheight:指定位图的高度(以象素为单位)。

   biplanes:指定目标设备的位面数。这个成员变量的值必须为 1。

   bibitcount:指定每个象素的位数。其值必须为 1、4、8 或 24。

   bicompression:指定压缩位图的压缩类型。在 24 位格式中,该变量被设置为 0。

   bisizeimage:指定图像的大小(以字节为单位)。如果位图的格式是 bi_rgb,则将此成员变量设置为 0 是有效的。

   bixpelspermeter:为位图指定目标设备的水平分辨率(以“象素/米”为单位)。应用程序可用该值从最符合当前设备特征的资源群组中选择一个位图。

   biypelspermeter:为位图指定目标设备的垂直分辨率(以“象素/米”为单位)。

   biclrused:指定位图实际所用的颜色表中的颜色索引数。如果 bibitcount 设为 24,则 biclrused 指定用来优化 windows 调色板性能的参考颜色表。

   biclrimportant:指定对位图的显示有重要影响的颜色索引数。如果此值为 0,则所有颜色都很重要。

  现在已定义了创建图像所需的全部信息。

  第 3 部分:图像

  在 24 位格式中,图像中的每个象素都由存储为 brg 的三字节 rgb 序列表示。每个扫描行都被补足到 4 位。为了使这个过程稍复杂一点,图像是自底而上存储的,即第一个扫描行是图像中的最后一个扫描行。下图显示了标头 (bitmapheader) 和 (bitmapinfoheader) 以及部分图像。各个部分由垂线分隔:

0000000000 4d42 b536 0002 0000 0000 0036 0000 | 0028

0000000020 0000 0107 0000 00e0 0000 0001 0018 0000

0000000040 0000 b500 0002 0ec4 0000 0ec4 0000 0000

0000000060 0000 0000 0000 | ffff ffff ffff ffff ffff

0000000100 ffff ffff ffff ffff ffff ffff ffff ffff

*

在,我们开始检视代码

  现在我们已经知道了 24 位位图文件的结构,下面就是您期待已久的内容:用来将图像对象写入位图文件的代码。

import java.awt.*;

import java.io.*;

import java.awt.image.*;

public class bmpfile extends component {

file://— 私有常量

private final static int bitmapfileheader_size = 14;

private final static int bitmapinfoheader_size = 40;

file://— 私有变量声明

file://— 位图文件标头

private byte bitmapfileheader [] = new byte [14];

private byte bftype [] = {b, m};

private int bfsize = 0;

private int bfreserved1 = 0;

private int bfreserved2 = 0;

private int bfoffbits = bitmapfileheader_size + bitmapinfoheader_size;

file://— 位图信息标头

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

private int bisize = bitmapinfoheader_size;

private int biwidth = 0;

private int biheight = 0;

private int biplanes = 1;

private int bibitcount = 24;

private int bicompression = 0;

private int bisizeimage = 0x030000;

private int bixpelspermeter = 0x0;

private int biypelspermeter = 0x0;

private int biclrused = 0;

private int biclrimportant = 0;

file://— 位图原始数据

private int bitmap [];

file://— 文件部分

private fileoutputstream fo;

file://— 缺省构造函数

public bmpfile() {

}

public void savebitmap (string parfilename, image parimage, int

parwidth, int parheight) {

try {

fo = new fileoutputstream (parfilename);

save (parimage, parwidth, parheight);

fo.close ();

}

catch (exception saveex) {

saveex.printstacktrace ();

}

}

/*

* savemethod 是该进程的主方法。该方法

* 将调用 convertimage 方法以将内存图像转换为

* 字节数组;writebitmapfileheader 方法创建并写入

* 位图文件标头;writebitmapinfoheader 创建

* 信息标头;writebitmap 写入图像。

*

*/

private void save (image parimage, int parwidth, int parheight) {

try {

convertimage (parimage, parwidth, parheight);

writebitmapfileheader ();

writebitmapinfoheader ();

writebitmap ();

}

catch (exception saveex) {

saveex.printstacktrace ();

}

}

/*

* convertimage 将内存图像转换为位图格式 (brg)。

* 它还计算位图信息标头所用的某些信息。

*

*/

private boolean convertimage (image parimage, int parwidth, int parheight) {

int pad;

bitmap = new int [parwidth * parheight];

pixelgrabber pg = new pixelgrabber (parimage, 0, 0, parwidth, parheight,

bitmap, 0, parwidth);

try {

pg.grabpixels ();

}

catch (interruptedexception e) {

e.printstacktrace ();

return (false);

}

pad = (4 – ((parwidth * 3) % 4)) * parheight;

bisizeimage = ((parwidth * parheight) * 3) + pad;

bfsize = bisizeimage + bitmapfileheader_size +

bitmapinfoheader_size;

biwidth = parwidth;

biheight = parheight;

return (true);

}

/*

* writebitmap 将象素捕获器返回的图像转换为

* 所需的格式。请记住:扫描行在位图文件中是

* 反向存储的!

*

* 每个扫描行必须补足为 4 个字节。

*/

private void writebitmap () {

int size;

int value;

int j;

int i;

int rowcount;

int rowindex;

int lastrowindex;

int pad;

int padcount;

byte rgb [] = new byte [3];

size = (biwidth * biheight) – 1;

pad = 4 – ((biwidth * 3) % 4);

if (pad == 4) // <==== 错误修正

pad = 0; // <==== 错误修正

rowcount = 1;

padcount = 0;

rowindex = size – biwidth;

lastrowindex = rowindex;

try {

for (j = 0; j < size; j++) {

value = bitmap [rowindex];

rgb [0] = (byte) (value & 0xff);

rgb [1] = (byte) ((value >> 8) & 0xff);

rgb [2] = (byte) ((value >> 16) & 0xff);

fo.write (rgb);

if (rowcount == biwidth) {

padcount += pad;

for (i = 1; i <= pad; i++) {

fo.write (0x00);

}

rowcount = 1;

rowindex = lastrowindex – biwidth;

lastrowindex = rowindex;

}

else

rowcount++;

rowindex++;

}

file://— 更新文件大小

bfsize += padcount – pad;

bisizeimage += padcount – pad;

}

catch (exception wb) {

wb.printstacktrace ();

}

}

/*

* writebitmapfileheader 将位图文件标头写入文件中。

*

*/

private void writebitmapfileheader () {

try {

fo.write (bftype);

fo.write (inttodword (bfsize));

fo.write (inttoword (bfreserved1));

fo.write (inttoword (bfreserved2));

fo.write (inttodword (bfoffbits));

}

catch (exception wbfh) {

wbfh.printstacktrace ();

}

}

/*

*

* writebitmapinfoheader 将位图信息标头

* 写入文件中。

*

*/

private void writebitmapinfoheader () {

try {

fo.write (inttodword (bisize));

fo.write (inttodword (biwidth));

fo.write (inttodword (biheight));

fo.write (inttoword (biplanes));

fo.write (inttoword (bibitcount));

fo.write (inttodword (bicompression));

fo.write (inttodword (bisizeimage));

fo.write (inttodword (bixpelspermeter));

fo.write (inttodword (biypelspermeter));

fo.write (inttodword (biclrused));

fo.write (inttodword (biclrimportant));

}

catch (exception wbih) {

wbih.printstacktrace ();

}

}

/*

*

* inttoword 将整数转换为单字,返回值

* 存储在一个双字节数组中。

*

*/

private byte [] inttoword (int parvalue) {

byte retvalue [] = new byte [2];

retvalue [0] = (byte) (parvalue & 0x00ff);

retvalue [1] = (byte) ((parvalue >> 8) & 0x00ff);

return (retvalue);

}

/*

*

* inttodword 将整数转换为双字,返回值

* 存储在一个 4 字节数组中。

*

*/

private byte [] inttodword (int parvalue) {

byte retvalue [] = new byte [4];

retvalue [0] = (byte) (parvalue & 0x00ff);

retvalue [1] = (byte) ((parvalue >> 8) & 0x000000ff);

retvalue [2] = (byte) ((parvalue >> 16) & 0x000000ff);

retvalue [3] = (byte) ((parvalue >> 24) & 0x000000ff);

return (retvalue);

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 用 Java 保存位图文件-JSP教程,Java技巧及代码
分享到: 更多 (0)