bmp to Hex——图片在单片机上显示
摘要
我通过在网上寻找工具及借鉴开源社区留言,探索出一种位图转二进制写入单片机的工作流程
流水帐式流程
- 使用开源的GIMP处理图像为128x53的16bit二值化BMP文件
- 我找到一个开源工具,使用Monochrome预设导出图片,其内置大小端设置,但其将2byte作为最小粒度
- 显示不正常,可感受到以一小块为单位左右割裂颠倒。网上找到一个帖子描述了这种现象的成因。
https://forum.arduino.cc/t/solved-problem-drawing-bmp-in-oled-i2c-ssd1306-with-esp8266/417698/11
- 文章锁定前最后一个大佬给出了一个c程序,但我用不了
- 我上手修改,重拾大一下c++,用到了当时不懂现在还不懂的动态内存和指针
C++代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43//Origin:https://forum.arduino.cc/t/solved-problem-drawing-bmp-in-oled-i2c-ssd1306-with-esp8266/417698/11
//Some changes were made on the basis of the original version, which did not work properly on my VS2010
//MIT Lisences By:Zhaosn
#include <stdio.h>
const char myBitmap[] = { //this is the array char
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
char inverter(char val){ // this function invert the value
char ret;
char a = (val & 0x01)<<7;
char b = (val & 0x02)<<5;
char c = (val & 0x04)<<3;
char d = (val & 0x08)<<1;
char e = (val & 0x10)>>1;
char f = (val & 0x20)>>3;
char g = (val & 0x40)>>5;
char h = (val & 0x80)>>7;
ret = a|b|c|d|e|f|g|h;
return ret;
}
int main(void){
int size = sizeof(myBitmap);// myBitmap is the name for our array char
char *values = NULL;
int count = 0;
values = new char [size];
for(int i = 0 ; i < size; i++){
values[i] = inverter(myBitmap[i]); // here we revert each value;*
printf("0x%02x",(unsigned char)values[i]); //need "(unsigned char)" or it will display as "0xffffffxx". It is "2's complement".
if(count == 15){
printf(",\n");
count = 0;
} else if(i == size-1){
printf("\n");
} else {
printf(", ");
count++;
}
}
delete values;
}
bmp to Hex——图片在单片机上显示
https://zhaosn.github.io/2022/bmp2hex/