BMP保存

1.BMP文件格式

   BMP(Bitmap-File)图形文件是Windows采用的图形文件格式,在Windows环境下运行的所有图象处理软件    都支持BMP图     象文件格式。Windows系统内部各图像绘制操作都是以BMP为基础的。BMP位图文件默认的文件扩展名是BMP或者bmp(有     时它也会以.DIB或.RLE作扩展名)。

 

   结构:

          位图文件头、位图信息头、彩色表和定义位图的字节阵列。

 

2.首先设定文件保存和打开的方法

   保存:

   public boolean saveFile(String path,int [][] colors,JPanel panel){

		boolean state = false;
		try{
			//创建流对象
			FileOutputStream fos = new FileOutputStream(path);
			BufferedOutputStream bos = new BufferedOutputStream(fos);
			DataOutputStream dos = new DataOutputStream(bos);
			//写入数据到文件
			//获取宽度和高度
			int width = panel.getWidth();
			int height = panel.getHeight();
			//先写入文件头部信息(宽和高)
			dos.writeInt(width);
			dos.writeInt(height);
			//开始循环写入文件的内容部分
			for(int i=0;i<width;i++){
				for(int j=0;j<height;j++){
					dos.writeInt(colors[i][j]);
				}
			}
			dos.flush();
			dos.close();
			bos.close();
			fos.close();
			state = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return state;
	}

    BMP文件存储数据时,图像的扫描方式是按从左到右、从下到上的顺序

 

    打开

    public int[][] openFile(String path){

		int [][] colors = null;
		try{
			//创建流对象
			FileInputStream fis = new FileInputStream(path);
			BufferedInputStream bis = new BufferedInputStream(fis);
			DataInputStream dis = new DataInputStream(bis);
			//写入数据到文件
			//获取宽度和高度
			int width = dis.readInt();
			int height = dis.readInt();
			//实例化数组
			colors = new int[width][height];
			//开始循环写入文件的内容部分
			for(int i=0;i<width;i++){
				for(int j=0;j<height;j++){
					colors[i][j] = dis.readInt();
				}
			}
			dis.close();
			bis.close();
			fis.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		return colors;
	}
}

 

3.获取画图区域上的像素点

   public static int [][] savePic(JPanel panel){

		try {
			//实例化Robot对象
			robot = new Robot();
		} catch (AWTException e) {
			e.printStackTrace();
		}
		//获取面板的宽度和高度
		width = panel.getWidth();
		height = panel.getHeight();
		//获取面板的起始坐标
		Point p = panel.getLocation();
		//根据起始坐标、宽和高来截取一个矩形区域
		Rectangle rect = new Rectangle(p.x,p.y,width,height);
		//定义一个缓冲图片对象
		BufferedImage image  = robot.createScreenCapture(rect);
		//实例化一个二维数组
		colors = new int[width][height];
		//循环遍历图片对象
		for(int i=0;i<width;i++){
			for(int j=0;j<height;j++){
				colors[i][j] = image.getRGB(i, j);
			}
		}
		return colors;
	}
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将位图保存BMP 格式,可以使用以下步骤: 1. 创建一个 BITMAPFILEHEADER 结构体,用于保存 BMP 文件头信息。 2. 创建一个 BITMAPINFOHEADER 结构体,用于保存位图信息头。 3. 计算位图像素数据的大小,用于分配内存。 4. 将位图像素数据按照 BMP 文件格式存储到内存中。 5. 将 BITMAPFILEHEADER 和 BITMAPINFOHEADER 写入 BMP 文件开头。 6. 将位图像素数据写入 BMP 文件中。 以下是一个示例代码,用于将位图保存BMP 格式: ```c++ #include <fstream> #include <iostream> #include <vector> #include <cstring> using namespace std; #pragma pack(push, 1) // BMP 文件头 typedef struct { uint16_t bfType; // 文件类型,必须为 BM uint32_t bfSize; // 文件大小,单位为字节 uint16_t bfReserved1; // 保留字段,必须为 0 uint16_t bfReserved2; // 保留字段,必须为 0 uint32_t bfOffBits; // 位图数据的起始位置,单位为字节 } BITMAPFILEHEADER; // BMP 位图信息头 typedef struct { uint32_t biSize; // 信息头大小,必须为 40 int32_t biWidth; // 图像宽度,单位为像素 int32_t biHeight; // 图像高度,单位为像素 uint16_t biPlanes; // 位平面数,必须为 1 uint16_t biBitCount; // 每像素位数,一般为 24 uint32_t biCompression; // 压缩方式,一般为 0 uint32_t biSizeImage; // 图像数据大小,单位为字节 int32_t biXPelsPerMeter; // 水平分辨率,单位为像素/meter int32_t biYPelsPerMeter; // 垂直分辨率,单位为像素/meter uint32_t biClrUsed; // 颜色数,一般为 0 uint32_t biClrImportant; // 重要颜色数,一般为 0 } BITMAPINFOHEADER; #pragma pack(pop) // 将位图保存BMP 格式 bool save_bitmap(const char *filename, const uint8_t *data, int width, int height) { // 计算位图像素数据大小 int data_size = width * height * 3; // 创建 BMP 文件头 BITMAPFILEHEADER file_header; memset(&file_header, 0, sizeof(file_header)); file_header.bfType = 0x4D42; // "BM" file_header.bfSize = sizeof(file_header) + sizeof(BITMAPINFOHEADER) + data_size; file_header.bfOffBits = sizeof(file_header) + sizeof(BITMAPINFOHEADER); // 创建 BMP 位图信息头 BITMAPINFOHEADER info_header; memset(&info_header, 0, sizeof(info_header)); info_header.biSize = sizeof(info_header); info_header.biWidth = width; info_header.biHeight = height; info_header.biPlanes = 1; info_header.biBitCount = 24; info_header.biSizeImage = data_size; // 打开文件并写入 BMP 文件头和位图信息头 ofstream outfile(filename, ios::binary); if (!outfile) { cerr << "Failed to open file: " << filename << endl; return false; } outfile.write(reinterpret_cast<const char*>(&file_header), sizeof(file_header)); outfile.write(reinterpret_cast<const char*>(&info_header), sizeof(info_header)); // 写入位图像素数据 outfile.write(reinterpret_cast<const char*>(data), data_size); outfile.close(); return true; } ``` 这个函数需要传入一个指向位图像素数据的指针,以及位图的宽度和高度。函数会将位图保存BMP 格式,并返回一个布尔值表示保存是否成功。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值