1+ #include " SDMMCBlockDevice.h" // Multi Media Card APIs
2+ #include " FATFileSystem.h" // Mbed API for portable and embedded systems
3+ SDMMCBlockDevice blockDevice;
4+ mbed::FATFileSystem fileSystem (" fs" );
5+
6+ #include " camera.h" // Arduino Mbed Core Camera APIs
7+ #include " himax.h" // Exclusive Camera library for the Portenta Vision Shield
8+ HM01B0 himax;
9+ Camera cam (himax);
10+
11+ // Settings for our setup
12+ #define RES_H (unsigned int )240
13+ #define RES_W (unsigned int )320
14+ #define IMAGE_MODE CAMERA_GRAYSCALE
15+ #define IMAGE_BPP (unsigned int )8
16+ // Headers info
17+ #define HEADER_FILE_HEADER (unsigned int )14
18+ #define HEADER_DIB_SIZE (unsigned int )40
19+ #define HEADER_FULL_SIZE (HEADER_FILE_HEADER + HEADER_DIB_SIZE)
20+ #define PALETTE_SIZE (2 ^ IMAGE_BPP) * 4 // 4 bytes per color
21+
22+ void setup ()
23+ {
24+ Serial.begin (115200 );
25+ while (!Serial)
26+ ;
27+
28+ // Mount SD Card
29+ mountSD ();
30+
31+ // Init the cam QVGA, 30FPS, Grayscale
32+ if (!cam.begin (CAMERA_R320x240, IMAGE_MODE, 30 ))
33+ {
34+ Serial.println (" Unable to find the camera" );
35+ }
36+
37+ // Save the headers and the image data into the .bmp file
38+ parseData ();
39+ }
40+
41+ void loop ()
42+ {
43+ while (1 )
44+ ;
45+ }
46+
47+ // Mount File system block
48+ void mountSD ()
49+ {
50+ Serial.println (" Mounting SD Card..." );
51+
52+ int error = fileSystem.mount (&blockDevice);
53+ if (error)
54+ {
55+ Serial.println (" No SD Card found" );
56+ while (1 )
57+ ;
58+ }
59+ }
60+
61+ void parseData ()
62+ {
63+ unsigned char *imgData = NULL ;
64+ int fileSize = HEADER_FILE_HEADER + RES_W * RES_H;
65+
66+ FILE *file = fopen (" /fs/image.bmp" , " w+" );
67+
68+ // Get a Frame
69+ if (cam.grabFrame (fb, 3000 ) == 0 )
70+ {
71+ // Save the raw image data (8bpp grayscale)
72+ imgData = fb.getBuffer ();
73+ }
74+ else
75+ {
76+ Serial.println (" could not grab the frame" );
77+ while (1 )
78+ ;
79+ }
80+ // Bitmap structure (Head + DIB Head + ColorMap + binary image)
81+ unsigned char bitmapFileHeader[HEADER_FILE_HEADER];
82+ unsigned char bitmapDIBHeader[HEADER_DIB_SIZE];
83+ unsigned char colorMap[PALETTE_SIZE]; // Needed for <=8bpp grayscale bitmaps
84+
85+ // Set the file headers to 0
86+ memset (bitmapFileHeader, (unsigned char )(0 ), HEADER_FILE_HEADER);
87+ memset (bitmapDIBHeader, (unsigned char )(0 ), HEADER_DIB_SIZE);
88+ memset (colorMap, (unsigned char )(0 ), PALETTE_SIZE);
89+
90+ // Write the headers info
91+ // File header
92+ bitmapFileHeader[0 ] = ' B' ;
93+ bitmapFileHeader[1 ] = ' M' ;
94+ bitmapFileHeader[2 ] = (unsigned char )(fileSize);
95+ bitmapFileHeader[3 ] = (unsigned char )(fileSize >> 8 );
96+ bitmapFileHeader[4 ] = (unsigned char )(fileSize >> 16 );
97+ bitmapFileHeader[5 ] = (unsigned char )(fileSize >> 24 );
98+ bitmapFileHeader[10 ] = (unsigned char )HEADER_FULL_SIZE + PALETTE_SIZE;
99+
100+ // Info header
101+ bitmapDIBHeader[0 ] = (unsigned char )(HEADER_DIB_SIZE);
102+ bitmapDIBHeader[4 ] = (unsigned char )(RES_W);
103+ bitmapDIBHeader[5 ] = (unsigned char )(RES_W >> 8 );
104+ bitmapDIBHeader[8 ] = (unsigned char )(RES_H);
105+ bitmapDIBHeader[8 ] = (unsigned char )(RES_H >> 8 );
106+ bitmapDIBHeader[14 ] = (unsigned char )(IMAGE_BPP);
107+
108+ // Color palette for grayscale Bitmaps (8bpp)
109+ for (int i = 0 ; i < (2 ^ IMAGE_BPP); i++)
110+ {
111+ colorMap[i * 4 ] = i;
112+ colorMap[i * 4 + 1 ] = i;
113+ colorMap[i * 4 + 2 ] = i;
114+ }
115+
116+ // Write theh bitmap file
117+ fwrite (bitmapFileHeader, 1 , HEADER_FILE_HEADER, file);
118+ fwrite (bitmapDIBHeader, 1 , HEADER_DIB_SIZE, file);
119+ fwrite (colorMap, 1 , PALETTE_SIZE, file); // Color map
120+ fwrite (imgData, 1 , RES_H * RES_W, file);
121+
122+ // Close the stream (bitmap file)
123+ fclose (file);
124+ }
0 commit comments