从无符号字符缓冲区创建图像
Create image from unsigned char buffer
我正在使用名为 Secugen Pro 20 的设备捕捉指纹,它有自己的 Linux SDK,我想捕捉指纹图像并将其保存为任何图像格式.
他们有这个typedef unsigned char BYTE;
我声明了我的 imageBuffer
BYTE *CurrentImageBuffer;
然后我使用设备规格为其分配内存
CurrentImageBuffer = malloc(device_info.ImageWidth*device_info.ImageHeight);
在我的代码中的某个时刻,我捕捉图像并将 CurrentImageBuffer
作为参数传递给捕捉函数:
SGFPM_GetImageEx(m_hFPM, CurrentImageBuffer, GET_IMAGE_TIMEOUT, NULL, GET_IMAGE_DESIRED_QUALITY)
这就是这行代码之后变量的样子(我可以确认它捕获了一个手指):
我只是不明白如何从这个缓冲区继续创建图像,因为它看起来不像 ByteArray
我什至不知道那是否是获取图像的正确位置,但看起来是正确的位置,因为它是一个缓冲区,对吧?
OBS:我是 C 的新手
获取图像的最简单方法是制作 NetPBM PGM 图像 - 请参阅 Wikipedia NetPBM page。
因此,如果您的图像是 640 像素宽 x 480 像素高,您将从 SDK 获得一个 307,200 字节的缓冲区,然后将其写入文件并检查其长度是否正确。称之为 image.raw
.
现在您只需要一个 PGM header,并且由于您的图像是灰度和二进制的,因此您需要 P5
header.
因此,在终端中,您可以将 header 放在:
{ printf "P5\n640 480\n255\n" ; cat image.raw ; } > image.pgm
如果您不熟悉该语法,您可以使用相同的语法:
printf "P5\n640 480\n255\n" > image.pgm
cat image.raw >> image.pgm
您可以使用 feh
、gimp
、Photoshop 等查看该图像
如果您想将其制作成 BMP、JPEG 或 PNG,请使用 ImageMagick,它已安装在大多数 Linux 发行版中,可用于 macOS 和Windows:
magick image.pgm image.png
或
magick image.pgm image.jpg
如果您的 ImageMagick 版本是 v6 或更早版本,请使用 convert
代替 magick
:
convert image.pgm image.png
这是一个将 8 位灰度图像写入 Windows BMP 文件的小示例程序:
#include <stdio.h>
typedef unsigned char Byte;
int writeBMPGray8(FILE *f, int w, int h, const Byte *data)
{
unsigned bytesPerRow = (w + 3) & ~3; // align to 4 bytes (requirement)
unsigned size
= 14 // Bitmap file header size
+ 12 // DIB header size
+ 256 * 3; // palette size
unsigned gap = size;
size = (size + 3) & ~3; // align to 4 bytes (requirement)
gap = size - gap; // get size of gap between end of headers and raw data
unsigned offs = size; // store offset of raw data
size += h * bytesPerRow; // bitmap data size in file
/* write Bitmap file header (14 bytes) */
{ const Byte buffer[14] = {
'B', 'M', // magic code
size & 0xff, size >> 8 & 0xff, size >> 16 & 0xff, size >> 24 & 0xff, // size of BMP file in bytes
0, 0, // reserved
0, 0, // reserved
offs & 0xff, offs >> 8 & 0xff, offs >> 16 & 0xff, offs >> 24 & 0xff // starting offset of pixel data
};
if (fwrite(buffer, sizeof buffer, 1, f) != 1) return -1; // ERROR!
}
/* write DIB header (12 bytes) */
{ const Byte buffer[12] = {
12, 0, 0, 0, // size of this header
w & 0xff, w >> 8 & 0xff, // bitmap width in pixels
h & 0xff, h >> 8 & 0xff, // bitmap height in pixels
1, 0, // number of color planes, must be 1
8, 0 // number of bits per pixel
};
if (fwrite(buffer, sizeof buffer, 1, f) != 1) return -1; // ERROR!
}
/* write color palette (3 * 256 bytes) */
for (int i = 0; i < 256; ++i) { // make a gray level palette
Byte buffer[3] = { i, i, i };
if (fwrite(buffer, sizeof buffer, 1, f) != 1) return -1; // ERROR!
}
/* write gap (to align start address of raw data with 4 */
for (int i = 0; i < gap; ++i) {
if (fputc(0, f) < 0) return -1; // ERROR!
}
/* write raw data */
for (int y = 0; y < h; ++y) { // for all rows
int x = 0;
for (; x < w; ++x) { // for all columns
if (fputc(*data++, f) < 0) return -1; // ERROR!
}
// write row padding
for (; x < bytesPerRow; ++x) {
if (fputc(0, f) < 0) return -1; // ERROR!
}
}
/* done */
return 0;
}
int main()
{
/* a sample image 6 x 8, gray level */
enum { w = 6, h = 8 };
const Byte imgRaw[w * h] = {
0x00, 0x30, 0x60, 0x90, 0xc0, 0xf0,
0x02, 0x32, 0x62, 0x92, 0xc2, 0xf2,
0x04, 0x34, 0x64, 0x94, 0xc4, 0xf4,
0x06, 0x36, 0x66, 0x96, 0xc6, 0xf6,
0x08, 0x38, 0x68, 0x98, 0xc8, 0xf8,
0x0a, 0x3a, 0x6a, 0x9a, 0xca, 0xfa,
0x0c, 0x3c, 0x6c, 0x9c, 0xcc, 0xfc,
0x0e, 0x3e, 0x6e, 0x9e, 0xce, 0xfe
};
FILE *f = fopen("test.bmp", "wb");
if (!f) return 1; // ERROR!
if (writeBMPGray8(f, w, h, imgRaw)) return 1; // ERROR!
if (fclose(f)) return 1; // ERROR!
return 0; // success
}
示例图像提供了某种水平和垂直渐变。我特意选择了 6 的宽度,以便 check/show 正确地完成行对齐。
实现基于维基百科中的描述BMP file format。
为了简短起见,我编码了最简单的格式——Windows 2.0 的古老 BITMAPCOREHEADER
和 OS/2 1.x。 (MS Paint 可以加载它以及 Windows 10 预览版。我用 GIMP 进行了测试,它也加载了,没有任何抱怨。)
这是 GIMP 中的样子:
如果您已正确捕获 CurrentImageBuffer 中的图像,则可以使用以下代码片段将其写入原始文件:
fp = fopen(rawFileName,"wb");
fwrite (CurrentImageBuffer, sizeof (BYTE) , device_info.ImageHeight*device_info.ImageWidth , fp);
fclose(fp);
因为我使用了相同的环境,所以我从我的工作代码库中发送上面的片段。实际上,原始文件稍后会转换为模板,稍后用于匹配/识别而不是直接用于查看等。变量 rawFileName 将文件名称存储为存储此缓冲区的字符数组(字符串)。
我正在使用名为 Secugen Pro 20 的设备捕捉指纹,它有自己的 Linux SDK,我想捕捉指纹图像并将其保存为任何图像格式.
他们有这个typedef unsigned char BYTE;
我声明了我的 imageBuffer
BYTE *CurrentImageBuffer;
然后我使用设备规格为其分配内存
CurrentImageBuffer = malloc(device_info.ImageWidth*device_info.ImageHeight);
在我的代码中的某个时刻,我捕捉图像并将 CurrentImageBuffer
作为参数传递给捕捉函数:
SGFPM_GetImageEx(m_hFPM, CurrentImageBuffer, GET_IMAGE_TIMEOUT, NULL, GET_IMAGE_DESIRED_QUALITY)
这就是这行代码之后变量的样子(我可以确认它捕获了一个手指):
我只是不明白如何从这个缓冲区继续创建图像,因为它看起来不像 ByteArray
我什至不知道那是否是获取图像的正确位置,但看起来是正确的位置,因为它是一个缓冲区,对吧?
OBS:我是 C 的新手
获取图像的最简单方法是制作 NetPBM PGM 图像 - 请参阅 Wikipedia NetPBM page。
因此,如果您的图像是 640 像素宽 x 480 像素高,您将从 SDK 获得一个 307,200 字节的缓冲区,然后将其写入文件并检查其长度是否正确。称之为 image.raw
.
现在您只需要一个 PGM header,并且由于您的图像是灰度和二进制的,因此您需要 P5
header.
因此,在终端中,您可以将 header 放在:
{ printf "P5\n640 480\n255\n" ; cat image.raw ; } > image.pgm
如果您不熟悉该语法,您可以使用相同的语法:
printf "P5\n640 480\n255\n" > image.pgm
cat image.raw >> image.pgm
您可以使用 feh
、gimp
、Photoshop 等查看该图像
如果您想将其制作成 BMP、JPEG 或 PNG,请使用 ImageMagick,它已安装在大多数 Linux 发行版中,可用于 macOS 和Windows:
magick image.pgm image.png
或
magick image.pgm image.jpg
如果您的 ImageMagick 版本是 v6 或更早版本,请使用 convert
代替 magick
:
convert image.pgm image.png
这是一个将 8 位灰度图像写入 Windows BMP 文件的小示例程序:
#include <stdio.h>
typedef unsigned char Byte;
int writeBMPGray8(FILE *f, int w, int h, const Byte *data)
{
unsigned bytesPerRow = (w + 3) & ~3; // align to 4 bytes (requirement)
unsigned size
= 14 // Bitmap file header size
+ 12 // DIB header size
+ 256 * 3; // palette size
unsigned gap = size;
size = (size + 3) & ~3; // align to 4 bytes (requirement)
gap = size - gap; // get size of gap between end of headers and raw data
unsigned offs = size; // store offset of raw data
size += h * bytesPerRow; // bitmap data size in file
/* write Bitmap file header (14 bytes) */
{ const Byte buffer[14] = {
'B', 'M', // magic code
size & 0xff, size >> 8 & 0xff, size >> 16 & 0xff, size >> 24 & 0xff, // size of BMP file in bytes
0, 0, // reserved
0, 0, // reserved
offs & 0xff, offs >> 8 & 0xff, offs >> 16 & 0xff, offs >> 24 & 0xff // starting offset of pixel data
};
if (fwrite(buffer, sizeof buffer, 1, f) != 1) return -1; // ERROR!
}
/* write DIB header (12 bytes) */
{ const Byte buffer[12] = {
12, 0, 0, 0, // size of this header
w & 0xff, w >> 8 & 0xff, // bitmap width in pixels
h & 0xff, h >> 8 & 0xff, // bitmap height in pixels
1, 0, // number of color planes, must be 1
8, 0 // number of bits per pixel
};
if (fwrite(buffer, sizeof buffer, 1, f) != 1) return -1; // ERROR!
}
/* write color palette (3 * 256 bytes) */
for (int i = 0; i < 256; ++i) { // make a gray level palette
Byte buffer[3] = { i, i, i };
if (fwrite(buffer, sizeof buffer, 1, f) != 1) return -1; // ERROR!
}
/* write gap (to align start address of raw data with 4 */
for (int i = 0; i < gap; ++i) {
if (fputc(0, f) < 0) return -1; // ERROR!
}
/* write raw data */
for (int y = 0; y < h; ++y) { // for all rows
int x = 0;
for (; x < w; ++x) { // for all columns
if (fputc(*data++, f) < 0) return -1; // ERROR!
}
// write row padding
for (; x < bytesPerRow; ++x) {
if (fputc(0, f) < 0) return -1; // ERROR!
}
}
/* done */
return 0;
}
int main()
{
/* a sample image 6 x 8, gray level */
enum { w = 6, h = 8 };
const Byte imgRaw[w * h] = {
0x00, 0x30, 0x60, 0x90, 0xc0, 0xf0,
0x02, 0x32, 0x62, 0x92, 0xc2, 0xf2,
0x04, 0x34, 0x64, 0x94, 0xc4, 0xf4,
0x06, 0x36, 0x66, 0x96, 0xc6, 0xf6,
0x08, 0x38, 0x68, 0x98, 0xc8, 0xf8,
0x0a, 0x3a, 0x6a, 0x9a, 0xca, 0xfa,
0x0c, 0x3c, 0x6c, 0x9c, 0xcc, 0xfc,
0x0e, 0x3e, 0x6e, 0x9e, 0xce, 0xfe
};
FILE *f = fopen("test.bmp", "wb");
if (!f) return 1; // ERROR!
if (writeBMPGray8(f, w, h, imgRaw)) return 1; // ERROR!
if (fclose(f)) return 1; // ERROR!
return 0; // success
}
示例图像提供了某种水平和垂直渐变。我特意选择了 6 的宽度,以便 check/show 正确地完成行对齐。
实现基于维基百科中的描述BMP file format。
为了简短起见,我编码了最简单的格式——Windows 2.0 的古老 BITMAPCOREHEADER
和 OS/2 1.x。 (MS Paint 可以加载它以及 Windows 10 预览版。我用 GIMP 进行了测试,它也加载了,没有任何抱怨。)
这是 GIMP 中的样子:
如果您已正确捕获 CurrentImageBuffer 中的图像,则可以使用以下代码片段将其写入原始文件:
fp = fopen(rawFileName,"wb");
fwrite (CurrentImageBuffer, sizeof (BYTE) , device_info.ImageHeight*device_info.ImageWidth , fp);
fclose(fp);
因为我使用了相同的环境,所以我从我的工作代码库中发送上面的片段。实际上,原始文件稍后会转换为模板,稍后用于匹配/识别而不是直接用于查看等。变量 rawFileName 将文件名称存储为存储此缓冲区的字符数组(字符串)。