00000006.htm
来自「水木清华BBS」· HTM 代码 · 共 289 行 · 第 1/2 页
HTM
289 行
<HTML><HEAD> <TITLE>BBS水木清华站∶精华区</TITLE></HEAD><BODY><CENTER><H1>BBS水木清华站∶精华区</H1></CENTER>发信人: henrywang (海盗), 信区: Java, 读者数: 10 <BR>标 题: Re: 我想将canvas里头的图画保存成为gif或jpg图象,how <BR>发信站: BBS 水木清华站 (Sun Jan 10 09:00:22 1999) <BR> <BR>【 在 weffen (John) 的大作中提到: 】 <BR>∶<I> 有没有这样的库? </I><BR>∶<I> 或者怎样实现? </I><BR> <BR>给你一个存成bmp的类,至于jpg和gif照此琢磨着玩吧 <BR>package testPrint; <BR> <BR>import java.awt.*; <BR>import java.io.*; <BR>import java.awt.image.*; <BR> <BR>public class BMPFile extends Component { <BR> <BR> //--- Private constants <BR> private final static int BITMAPFILEHEADER_SIZE = 14; <BR> private final static int BITMAPINFOHEADER_SIZE = 40; <BR> <BR> //--- Private variable declaration <BR> <BR> //--- Bitmap file header <BR> private byte bitmapFileHeader [] = new byte [14]; <BR> private byte bfType [] = {'B', 'M'}; <BR> private int bfSize = 0; <BR> private int bfReserved1 = 0; <BR> private int bfReserved2 = 0; <BR> private int bfOffBits = BITMAPFILEHEADER_SIZE + BITMAPINFOHEADER_SIZE; <BR> <BR> //--- Bitmap info header <BR> private byte bitmapInfoHeader [] = new byte [40]; <BR> private int biSize = BITMAPINFOHEADER_SIZE; <BR> private int biWidth = 0; <BR> private int biHeight = 0; <BR> private int biPlanes = 1; <BR> private int biBitCount = 24; <BR> private int biCompression = 0; <BR> private int biSizeImage = 0x030000; <BR> private int biXPelsPerMeter = 0x0; <BR> private int biYPelsPerMeter = 0x0; <BR> private int biClrUsed = 0; <BR> private int biClrImportant = 0; <BR> <BR> //--- Bitmap raw data <BR> private int bitmap []; <BR> <BR> //--- File section <BR> private FileOutputStream fo; <BR> <BR> //--- Default constructor <BR> public BMPFile() { <BR> <BR> } <BR> <BR> public void saveBitmap (String parFilename, Image parImage, int <BR>parWidth, int parHeight) { <BR> <BR> try { <BR> fo = new FileOutputStream (parFilename); <BR> save (parImage, parWidth, parHeight); <BR> fo.close (); <BR> } <BR> catch (Exception saveEx) { <BR> saveEx.printStackTrace (); <BR> } <BR> <BR> } <BR> <BR> /* <BR> * The saveMethod is the main method of the process. This method <BR> * will call the convertImage method to convert the memory image to <BR> * a byte array; method writeBitmapFileHeader creates and writes <BR> * the bitmap file header; writeBitmapInfoHeader creates the <BR> * information header; and writeBitmap writes the image. <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> * convertImage converts the memory image to the bitmap format (BRG). <BR> * It also computes some information for the bitmap info header. <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> pad = (parWidth * 3) % 4; <BR> if (pad != 0) <BR> pad = (4 - pad) * 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 converts the image returned from the pixel grabber to <BR> * the format required. Remember: scan lines are inverted in <BR> * a bitmap file! <BR> * <BR> * Each scan line must be padded to an even 4-byte boundary. <BR> */ <BR> private void writeBitmap () { <BR> <BR> int size; <BR> int value; <BR> int j; <BR>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?