java读取bmp图像_JAVA实现对BMP图片的读取

本文详细介绍了BMP图像格式的结构,包括位图文件头、位图信息头、颜色和图像数据区,并提供了JAVA读取BMP图片的代码示例,帮助理解BMP图像的读取过程。
摘要由CSDN通过智能技术生成

BMP图片格式,是windows自带的一个图片格式,(*bmp),在windows的系统下都支持这种格式,bmp格式与设备无关的位图(DIB)格式,BMP简称位图,BMP的原始数据是没有经过压缩处理的  占用的空间比其它格式的图片要大

BMP由四部分组成 ,位图文件头 , 位图信息头 ,  颜色 , 图像数据区

BMP图片是三个字节为一个颜色保存,将字节拼接为int需要使用位移来做

位图文件头 (12个字节):

0000-0001:文件标识,为字母ASCII码“BM”,42 4D。

0002-0005:整个文件大小,单位字节。0006-0009:这个位置的数字是被微软保留的000A-000D:记录图像数据区的起始位置。从文件开始到位图数据(bitmap data)之间的偏移量。

位图信息头()

000E-0011:图像描述信息块的大小0012-0015:图像宽度。以像素为单位。0016-0019:图像高度。以像素为单位。001A-001B:图像的plane总数(恒为1)。001C-001D:记录像素,也就是颜色。1 - Monochrome bitmap,4 - 16 color bitmap,8 - 256 color bitmap,F - 16位位图,18 - 24bit (true color) bitmap,20 - 32位位图。

001E-0021:数据压缩方式(数值位0:不压缩;1:8位压缩;2:4位压缩;3:Bitfields压缩)。

0022-0025:图像区数据的大小。单位字节,该数必须是4的倍数。

0026-0029:

以下是一个简单的示例代码,可以实现将一个BMP图片转换为马赛克: ```c #include <stdio.h> #include <stdint.h> #define MOSAIC_SIZE 8 int main(int argc, char *argv[]) { if (argc != 3) { printf("Usage: %s input.bmp output.bmp\n", argv[0]); return 1; } FILE *input_file = fopen(argv[1], "rb"); if (!input_file) { printf("Failed to open input file: %s\n", argv[1]); return 1; } FILE *output_file = fopen(argv[2], "wb"); if (!output_file) { printf("Failed to open output file: %s\n", argv[2]); fclose(input_file); return 1; } // 读取BMP文件头 uint8_t header[54]; if (fread(header, 1, sizeof(header), input_file) != sizeof(header)) { printf("Failed to read BMP header\n"); fclose(input_file); fclose(output_file); return 1; } // 验证BMP文件格式 if (header[0] != 'B' || header[1] != 'M') { printf("Invalid BMP format\n"); fclose(input_file); fclose(output_file); return 1; } // 读取BMP图片数据 uint32_t width = *(uint32_t *)(header + 18); uint32_t height = *(uint32_t *)(header + 22); uint16_t bpp = *(uint16_t *)(header + 28) / 8; uint8_t *image_data = (uint8_t *)malloc(width * height * bpp); if (!image_data) { printf("Failed to allocate memory\n"); fclose(input_file); fclose(output_file); return 1; } if (fread(image_data, 1, width * height * bpp, input_file) != width * height * bpp) { printf("Failed to read BMP image data\n"); free(image_data); fclose(input_file); fclose(output_file); return 1; } // 将BMP图片数据转换为马赛克 uint32_t x, y; uint8_t *src, *dst; uint32_t mosaic_width = width / MOSAIC_SIZE; uint32_t mosaic_height = height / MOSAIC_SIZE; for (y = 0; y < mosaic_height; y++) { for (x = 0; x < mosaic_width; x++) { uint32_t i, j; uint32_t r = 0, g = 0, b = 0; for (j = 0; j < MOSAIC_SIZE; j++) { for (i = 0; i < MOSAIC_SIZE; i++) { src = image_data + ((y * MOSAIC_SIZE + j) * width + x * MOSAIC_SIZE + i) * bpp; r += src[2]; g += src[1]; b += src[0]; } } dst = image_data + ((y * MOSAIC_SIZE) * width + x * MOSAIC_SIZE) * bpp; dst[2] = r / (MOSAIC_SIZE * MOSAIC_SIZE); dst[1] = g / (MOSAIC_SIZE * MOSAIC_SIZE); dst[0] = b / (MOSAIC_SIZE * MOSAIC_SIZE); for (j = 0; j < MOSAIC_SIZE; j++) { for (i = 0; i < MOSAIC_SIZE; i++) { if (i == 0 && j == 0) { continue; } dst = image_data + ((y * MOSAIC_SIZE + j) * width + x * MOSAIC_SIZE + i) * bpp; dst[2] = dst[-bpp * MOSAIC_SIZE + 2]; dst[1] = dst[-bpp * MOSAIC_SIZE + 1]; dst[0] = dst[-bpp * MOSAIC_SIZE + 0]; } } } } // 写入BMP文件头 if (fwrite(header, 1, sizeof(header), output_file) != sizeof(header)) { printf("Failed to write BMP header\n"); free(image_data); fclose(input_file); fclose(output_file); return 1; } // 写入马赛克数据 if (fwrite(image_data, 1, width * height * bpp, output_file) != width * height * bpp) { printf("Failed to write BMP image data\n"); free(image_data); fclose(input_file); fclose(output_file); return 1; } free(image_data); fclose(input_file); fclose(output_file); return 0; } ``` 该程序使用标准C库函数实现了将一个BMP图片转换为马赛克的功能。具体实现思路是将图片按照一定的分辨率划分为若干个小块,然后对每个小块取平均RGB值,最终将小块的颜色统一设置为该平均值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值