📄 8.html
字号:
saveEx.printStackTrace ();<BR>
}<BR>
<BR>
}<BR>
<BR>
<BR>
/*<BR>
* saveMethod 是该进程的主方法。该方法<BR>
* 将调用 convertImage 方法以将内存图像转换为<BR>
* 字节数组;writeBitmapFileHeader 方法创建并写入<BR>
* 位图文件标头;writeBitmapInfoHeader 创建 <BR>
* 信息标头;writeBitmap 写入图像。<BR>
*<BR>
*/<BR>
private void save (Image parImage, int parWidth, int parHeight) {<BR>
<BR>
try {<BR>
convertImage (parImage, parWidth, parHeight);<BR>
writeBitmapFileHeader ();<BR>
writeBitmapInfoHeader ();<BR>
writeBitmap ();<BR>
}<BR>
catch (Exception saveEx) {<BR>
saveEx.printStackTrace ();<BR>
}<BR>
}<BR>
<BR>
<BR>
/*<BR>
* convertImage 将内存图像转换为位图格式 (BRG)。<BR>
* 它还计算位图信息标头所用的某些信息。<BR>
*<BR>
*/<BR>
private boolean convertImage (Image parImage, int parWidth, int parHeight) {<BR>
<BR>
int pad;<BR>
bitmap = new int [parWidth * parHeight];<BR>
<BR>
PixelGrabber pg = new PixelGrabber (parImage, 0, 0, parWidth, parHeight,<BR>
bitmap, 0, parWidth);<BR>
<BR>
try {<BR>
pg.grabPixels ();<BR>
}<BR>
catch (InterruptedException e) {<BR>
e.printStackTrace ();<BR>
return (false);<BR>
}<BR>
<BR>
pad = (4 - ((parWidth * 3) % 4)) * parHeight;<BR>
biSizeImage = ((parWidth * parHeight) * 3) + pad;<BR>
bfSize = biSizeImage + BITMAPFILEHEADER_SIZE +<BR>
BITMAPINFOHEADER_SIZE;<BR>
biWidth = parWidth;<BR>
biHeight = parHeight;<BR>
<BR>
return (true);<BR>
}<BR>
<BR>
/*<BR>
* writeBitmap 将象素捕获器返回的图像转换为<BR>
* 所需的格式。请记住:扫描行在位图文件中是<BR>
* 反向存储的!<BR>
*<BR>
* 每个扫描行必须补足为 4 个字节。<BR>
*/<BR>
private void writeBitmap () {<BR>
<BR>
int size;<BR>
int value;<BR>
int j;<BR>
int i;<BR>
int rowCount;<BR>
int rowIndex;<BR>
int lastRowIndex;<BR>
int pad;<BR>
int padCount;<BR>
byte rgb [] = new byte [3];<BR>
<BR>
<BR>
size = (biWidth * biHeight) - 1;<BR>
pad = 4 - ((biWidth * 3) % 4);<BR>
if (pad == 4) // <==== 错误修正<BR>
pad = 0; // <==== 错误修正<BR>
rowCount = 1;<BR>
padCount = 0;<BR>
rowIndex = size - biWidth;<BR>
lastRowIndex = rowIndex;<BR>
<BR>
try {<BR>
for (j = 0; j < size; j++) {<BR>
value = bitmap [rowIndex];<BR>
rgb [0] = (byte) (value & 0xFF);<BR>
rgb [1] = (byte) ((value >> 8) & 0xFF);<BR>
rgb [2] = (byte) ((value >> 16) & 0xFF);<BR>
fo.write (rgb);<BR>
if (rowCount == biWidth) {<BR>
padCount += pad;<BR>
for (i = 1; i <= pad; i++) {<BR>
fo.write (0x00);<BR>
}<BR>
rowCount = 1;<BR>
rowIndex = lastRowIndex - biWidth;<BR>
lastRowIndex = rowIndex;<BR>
}<BR>
else<BR>
rowCount++;<BR>
rowIndex++;<BR>
}<BR>
<BR>
//--- 更新文件大小<BR>
bfSize += padCount - pad;<BR>
biSizeImage += padCount - pad;<BR>
}<BR>
catch (Exception wb) {<BR>
wb.printStackTrace ();<BR>
}<BR>
<BR>
} <BR>
<BR>
/*<BR>
* writeBitmapFileHeader 将位图文件标头写入文件中。<BR>
*<BR>
*/<BR>
private void writeBitmapFileHeader () {<BR>
<BR>
try {<BR>
fo.write (bfType);<BR>
fo.write (intToDWord (bfSize));<BR>
fo.write (intToWord (bfReserved1));<BR>
fo.write (intToWord (bfReserved2));<BR>
fo.write (intToDWord (bfOffBits));<BR>
<BR>
}<BR>
catch (Exception wbfh) {<BR>
wbfh.printStackTrace ();<BR>
}<BR>
<BR>
}<BR>
<BR>
/*<BR>
*<BR>
* writeBitmapInfoHeader 将位图信息标头<BR>
* 写入文件中。<BR>
*<BR>
*/<BR>
<BR>
private void writeBitmapInfoHeader () {<BR>
<BR>
try {<BR>
fo.write (intToDWord (biSize));<BR>
fo.write (intToDWord (biWidth));<BR>
fo.write (intToDWord (biHeight));<BR>
fo.write (intToWord (biPlanes));<BR>
fo.write (intToWord (biBitCount));<BR>
fo.write (intToDWord (biCompression));<BR>
fo.write (intToDWord (biSizeImage));<BR>
fo.write (intToDWord (biXPelsPerMeter));<BR>
fo.write (intToDWord (biYPelsPerMeter));<BR>
fo.write (intToDWord (biClrUsed));<BR>
fo.write (intToDWord (biClrImportant));<BR>
}<BR>
catch (Exception wbih) {<BR>
wbih.printStackTrace ();<BR>
}<BR>
<BR>
}<BR>
<BR>
<BR>
/*<BR>
*<BR>
* intToWord 将整数转换为单字,返回值<BR>
* 存储在一个双字节数组中。<BR>
*<BR>
*/<BR>
private byte [] intToWord (int parValue) {<BR>
<BR>
byte retValue [] = new byte [2];<BR>
<BR>
retValue [0] = (byte) (parValue & 0x00FF);<BR>
retValue [1] = (byte) ((parValue >> 8) & 0x00FF);<BR>
<BR>
return (retValue);<BR>
<BR>
}<BR>
<BR>
/*<BR>
*<BR>
* intToDWord 将整数转换为双字,返回值<BR>
* 存储在一个 4 字节数组中。<BR>
*<BR>
*/<BR>
private byte [] intToDWord (int parValue) {<BR>
<BR>
byte retValue [] = new byte [4];<BR>
<BR>
retValue [0] = (byte) (parValue & 0x00FF);<BR>
retValue [1] = (byte) ((parValue >> 8) & 0x000000FF);<BR>
retValue [2] = (byte) ((parValue >> 16) & 0x000000FF);<BR>
retValue [3] = (byte) ((parValue >> 24) & 0x000000FF);<BR>
<BR>
return (retValue);<BR>
<BR>
}<BR>
<BR>
}<BR>
</code>
<P>
<FONT SIZE="+1"><STRONG>小结</STRONG></FONT><BR>
这就是所要做的全部工作。我确信您将会发现这个类很有用,因为到 JDK 1.1.6 为止,Java 不支持用任何常用的格式保存图像。JDK 1.2 将支持创建 JPEG 图像,但不支持创建位图。所以这个类仍将填补 JDK1.2 中的空白。
<P>
如果您使用这个类并发现了改进它的方法,请通知我!下面的个人简历中有我的电子邮件地址。
<!-- end body text -->
<P>
<A NAME="bio">
<TR><TD VALIGN="TOP">
<STRONG>
<FONT SIZE="-1">作者简介</FONT></STRONG><BR>
jeanpierre.dube Jean-Pierre Dubé 是一名自由 Java 咨询者。他于 1988 年注册创办了 Infocom公司。从那时起,Infocom 已承接并开发了几套应用程序,涉及的领域包括制造业、文档管理和大规模输电线管理。他有丰富的 C 语言、Visual Basic 和 Java(最近)编程经验,他的公司现在主要使用 Java 语言。
Infocom 最近的一个项目是一种图形 API,其测试版很快就会面市。
</td>
</tr>
</td>
</tr>
</body>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -