gpt4 book ai didi

c - 在c中生成条形码BMP

转载 作者:行者123 更新时间:2023-11-30 14:47:50 25 4
gpt4 key购买 nike

我被分配根据我随机生成的数字创建条形码图像(我已经完成了)。到目前为止,我尝试创建一个 BMP 文件并放入简单的黑白列,但我的图片被其他颜色扭曲,甚至在列中也没有。我什至没有开始编写条形码本身(这本身对我来说仍然是一个谜),我尝试创建它几乎两周了,但没有成功。我主要需要一个将黑色或白色写入条形码的程序按列或逐行编程,这样我就可以随意放置黑色或白色。

这是我的代码:

`int width, hight;
width = 141;
hight = 70;
FILE* barcode;
fopen_s(&barcode,NEWBARCODE, "wb");

int filesize = 54 + 3 * width*height; //w is your image width, h is image height, both int

char bmpfileheader[14] = { 'B','M', 0,0,0,0, 0,0, 0,0, 54,0,0,0 };
char bmpinfoheader[40] = { 0x28,0,0,0, 141,0,0,0, 70,0,0,0, 1,0, 24,0,0,0,0,0,0x8c,0x05,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
unsigned char white[3] = {255,255,255 };
unsigned char black[3] = {0,0,0 };
unsigned char pad[1] = { 0 };

bmpfileheader[2] = (unsigned char)(filesize);
bmpfileheader[3] = (unsigned char)(filesize >> 8);
bmpfileheader[4] = (unsigned char)(filesize >> 16);
bmpfileheader[5] = (unsigned char)(filesize >> 24);

fwrite(&bmpfileheader, 1, 14, barcode);
fwrite(&bmpinfoheader, 1, 40, barcode);

for (int i = 0; i < height; i++) { //columns
for (int j = 0; j < width*3+1; j++) {
if (i % 2 == 0) {
fwrite(&white, 1, 3, barcode);
fwrite(&black, 1, 3, barcode);
}
else {
fwrite(&black, 1, 3, barcode);
fwrite(&white, 1, 3, barcode);
}
}
fwrite(&pad, 1, 1, barcode);
}

输出 bmp 文件

出了什么问题?如果有任何关于创建 bmp 文件的提示,我们将不胜感激:)

最佳答案

尝试以下操作。请注意,我已将 BMP 的位深度移至 32。包装函数 setColumn 将允许您根据需要将各个列设置为黑色或白色。将 BMP 视为一个可以自由操作的数组,而不必处理大量的 fwrite 逻辑,这样更容易管理。

#include "stdafx.h"
#include <fstream>

#define NEWBARCODE "test.bmp"
#define WHITE 255
#define BLACK 0

void setColumn(unsigned char *data, const int height, const int width, const int colIndex, const int grayVal)
{
for (int r = 0; r < height; ++r)
{
data[r * width * 4 + colIndex * 4 + 0] = grayVal;
data[r * width * 4 + colIndex * 4 + 1] = grayVal;
data[r * width * 4 + colIndex * 4 + 2] = grayVal;
data[r * width * 4 + colIndex * 4 + 3] = 255;
}
}

int main()
{
int width, height;
width = 141;
height = 70;

std::ofstream filestream;

filestream.open(NEWBARCODE, std::ios::beg | std::ios::out | std::ios::binary);

int filesize = 54 + 4 * width * height;

char bmpfileheader[14] = { 'B','M', 0,0,0,0, 0,0, 0,0, 54,0,0,0 };
char bmpinfoheader[40] = { 0x28,0,0,0, 141,0,0,0, 70,0,0,0, 1,0, 32,0,0,0,0,0,0x8c,0x05,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };

bmpfileheader[2] = (unsigned char)(filesize);
bmpfileheader[3] = (unsigned char)(filesize >> 8);
bmpfileheader[4] = (unsigned char)(filesize >> 16);
bmpfileheader[5] = (unsigned char)(filesize >> 24);

filestream.write(bmpfileheader, 14);
filestream.write(bmpinfoheader, 40);

//Allocate BMP data block
unsigned char *data = new unsigned char[width * height * 4]{ 0 };

//Initialize BMP data to all black pixels
for (int i = 0; i < width * height * 4; ++i)
data[i] = 0;

//Set white
for (int i = 75; i < 100; ++i)
setColumn(data, height, width, i, WHITE);

//Set white
for (int i = 15; i < 25; ++i)
setColumn(data, height, width, i, WHITE);

//Set black
for (int i = 20; i < 23; ++i)
setColumn(data, height, width, i, BLACK);

filestream.write((const char *)data, height * width * 4);
filestream.close();

delete data;

return 0;
}

关于c - 在c中生成条形码BMP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50937988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com