此Demo用来说明保存成Bmp格式
步骤1:以以前做的画图板为例,点击"保存按钮后",保存到"E:\\test3.bmp"
运行效果:
源码:
private Robot robot; @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String actionCommand = e.getActionCommand(); // System.out.println("得到的ActionCommand"+actionCommand); if(actionCommand.equals("保存")){ try { robot = new Robot(); } catch (AWTException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } Rectangle screenRect = new Rectangle(drawpanelX,drawpanelY,drawpanelWidth,drawpanelHeight); BufferedImage img =robot.createScreenCapture(screenRect); BMPWriter bmp = new BMPWriter(); //IndexColorModel mf = new IndexColorModel(8, 16, r, g, b); File out = new File("E:\\test3.bmp"); bmp.write(img, out); } }
步骤2 :BMPWriter,用来保存成BMP
import java.awt.image.BufferedImage; import java.io.DataOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class BMPWriter { public static void write(BufferedImage img, File out) { int width = img.getWidth(); int tripleWidth = width * 3; int height = img.getHeight(); int fullTriWidth = tripleWidth % 4 == 0 ? tripleWidth : 4 * ((tripleWidth / 4) + 1); int[] px = new int[width * height]; px = img.getRGB(0, 0, width, height, px, 0, width); byte[] rgbs = new byte[tripleWidth * height]; int index = 0; // r, g, b数组 for (int i = height - 1; i >= 0; i--) { for (int j = 0; j < width; j++) { int pixel = px[i * width + j]; rgbs[index++] = (byte) pixel; rgbs[index++] = (byte) (pixel >>> 8); rgbs[index++] = (byte) (pixel >>> 16); } } // 补齐扫描行长度为4的倍数 byte[] fullrgbs = new byte[fullTriWidth * height]; for (int i = 0; i < height; i++) { for (int j = 0; j < fullTriWidth; j++) { if (j < tripleWidth) { fullrgbs[fullTriWidth * i + j] = rgbs[i * tripleWidth + j]; } else { fullrgbs[fullTriWidth * i + j] = 0; } } } index = 0; int fheader = 14; int infoheader = 40; int board = 0; int offset = fheader + infoheader + board; int length = width * height * 3 + offset; short frame = 1; short deep = 24; int fbl = 3800; DataOutputStream dos = null; try { FileOutputStream fos = new FileOutputStream(out); dos = new DataOutputStream(fos); dos.write('B'); dos.write('M');// 1格式头 wInt(dos, length);// 2-3文件大小 wInt(dos, 0);// 4-5保留 wInt(dos, offset);// 6-7偏移量 wInt(dos, infoheader);// 8-9头信息 wInt(dos, width);// 10-11宽 wInt(dos, height);// 12-13高 wShort(dos, frame);// 14 = 1帧数 wShort(dos, deep);// 15 = 24位数 wInt(dos, 0);// 16-17压缩 wInt(dos, 4);// 18-19 size wInt(dos, fbl);// 20-21水平分辨率 wInt(dos, fbl);// 22-23垂直分辨率 wInt(dos, 0);// 24-25颜色索引 0为所有 wInt(dos, 0);// 26-27重要颜色索引 0为所有 // wInt(0);//28-35 // wInt(0); // wInt(0); // wInt(0);//28-35彩色板 dos.write(fullrgbs); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { dos.close(); } catch (IOException e) { e.printStackTrace(); } } } private static void wInt(DataOutputStream dos, int i) throws IOException { dos.write(i); dos.write(i >> 8); dos.write(i >> 16); dos.write(i >> 24); } private static void wShort(DataOutputStream dos, short i) throws IOException { dos.write(i); dos.write(i >> 8); } }
总结:BMPWriter这个类网上很多的,写法也大同小异,我见的一般就是传个Color[][],然而,当初怎么也搞不出了,最近突然脑子闪了一下,用步骤一,得到BufferedImage,里面就有Color[][],就能很好实现存储为BMP了.
相关推荐
利用微软提供的directshow从视屏流中每隔0.3秒连续抓取图像帧,并自动按顺序保存成BMP格式
BMP格式图片信息读取及保存:宽、高、每像素所占位数、灰度图像的颜色表、位图RGB数据等
截屏并保存为BMP文件,截取屏幕上任意矩形,并保存为BMP图片文件
BufferedImage 保存 BMP格式 文件
通过窗口DC生成HBITMAP 然后通过GDI+保存成JPG和BMP格式的图片
用自制画板打开和保存BMP格式文件。画板代码如下
主要介绍了Android中把bitmap存成BMP格式图片的方法,需要的朋友可以参考下
在Linux操作系统下,将framebuffer中的缓存数据(也就是raw格式的数据)转换成BMP格式的图片
VB实现将BMP格式图片转化为ICO图标
使用VC6实现bmp格式图片的打开与保存操作,使用VC6实现bmp格式图片的打开与保存操作
该资源主要实现实时获取屏幕即桌面图像并保存为bmp格式的图片到工程底下。主要依赖于GDI编程来绘制图像.
利用Windows API 首先得到一个指定屏幕区域的矩形范围,然后其保存到一个位图中,最后将该位图保存到硬盘中.
CopyScreen(char* filename,int left,int top,int right,int bottom)通过调用此函数,把屏幕上的矩形区域保存为一个bmp格式的图片
从AVI视频中提取每一帧并保存为BMP格式的程序-avi_frame.m 刚刚接触MATLAB不久。。。 目前在做视频的处理。。 有点进步就发上来与大家分享。。 代码: str='D:\\Program Files\\MATLAB71\\work\\1....
一个用.VB 写的. 将BMP格式的图像文件 转换JPG格式.支持命令行,支持批处理.
修改图片的方法 需要工具: 1.) MRP编辑器 2.) GZIP压缩工具 3.) 16进制编辑器 4.) 必备图片工具PS (当然如果你有更好的工具也可以) 开始正题: ...会在右边的图像框看见你... 选择保存成BMP格式,点确定,就会进入另一选项,
将预览的到的每一帧yuv420数据通过转码(yuv420->rgb565->bmp),保存
一个图像的读出与保存的程序,希望对初学者有所帮助!
该代码经过实践,能够完成从jpeg到bmp图片的转换,特别是在嵌入式当中,在网络传输时候进行压缩,然后在使用时候进行解压缩,然后显示在屏幕上
1/打开程序 ...6/你一定不只是要build我给出的front图片吧,打开画图板自己画几个灰度图作为front吧,记住保存成bmp格式哦,然后build看看,如果有漂亮的结果不妨贴到这里来给大家看看啦。 样例: