C语言 EAN13 条形码生成【bmp生成】
时间:2010-10-14 来源:SnowBee3012
废话不多说
直接上代码
1 #ifndef _BMP_H_
2 #define _BMP_H_
3
4 #include<stdio.h>
5
6 #define max_height 4096
7 #define max_width 4096
8
9 unsigned char bitmap[max_height*max_width*3+1];
10
11 /*2 Byte*/
12 struct bmp_magic
13 {
14 unsigned char magic[2]; //BM
15 };
16 /*3 * 4 = 12 Byte*/
17 struct bmp_header
18 {
19 unsigned file_size; //file size in Byte, w * h * 3 + 54D
20 unsigned short creater1; //0
21 unsigned short creater2; //0
22 unsigned offset; //offset to image data: 54D, 36H
23 };
24 /*10 * 4 = 40 Byte*/
25 struct bmp_info
26 {
27 unsigned header_size; //info size in bytes, equals 40D, or 28H
28 unsigned width; //width in pixel
29 unsigned height; //height in pixel
30 unsigned short nplanes; //number of color planes, 1
31 unsigned short bitspp; //bits per pixel, 24D, 18H
32 unsigned compress_type; //compress type, default 0, (0-no, 1-, 2-, 3-)
33 unsigned image_size; //image size in Byte, w * h * 3
34 unsigned hres; //pixels per meter, 0
35 unsigned vres; //pixels per meter, 0
36 unsigned ncolors; //number of colors, 0
37 unsigned nimpcolors; //important colors, 0
38 };
39
40 struct bmp_magic magic;
41 struct bmp_header header;
42 struct bmp_info info;
43
44 /*2 + 12 + 40 = 54 Byte*/
45
46 void init_bmp_header(unsigned w, unsigned h)
47 {
48 magic.magic[0] = 'B';
49 magic.magic[1] = 'M';
50 header.file_size = w * h * 3 + 54;
51 header.creater1 = 0;
52 header.creater2 = 0;
53 header.offset = 54;
54 info.header_size = 40;
55 info.width = w;
56 info.height = h;
57 info.nplanes = 1;
58 info.bitspp = 3 * 8;
59 info.compress_type = 0;
60 info.image_size = w * h * 3;
61 info.hres = 0;
62 info.vres = 0;
63 info.ncolors = 0;
64 info.nimpcolors = 0;
65 }
66
67 void write_bmp_header(void)
68 {
69 fwrite(&magic,1,2,stdout);
70 fwrite(&header,3,4,stdout);
71 fwrite(&info,10,4,stdout);
72 }
73 void write_bmp(void)
74 {
75 write_bmp_header();
76 fwrite(&bitmap,1,info.image_size,stdout);
77 }
78 #endif
79
1 #ifndef _USE_H_
2 #define _USE_H_
3 #include<stdio.h>
4 #include"bmp.h"
5 void set_color_bk(unsigned char r,unsigned char g,unsigned char b)
6 {
7 unsigned k =0;
8 while(k<info.image_size)
9 {
10 bitmap[k++]=b;
11 bitmap[k++]=g;
12 bitmap[k++]=r;
13 }
14 }
15 void set_pixel(unsigned x, unsigned y, unsigned char r, unsigned g, unsigned b)
16 {
17 unsigned k = y * info.width + x;
18 bitmap[k * 3 + 0] = b;
19 bitmap[k * 3 + 1] = g;
20 bitmap[k * 3 + 2] = r;
21 }
22
23 void draw_rect(unsigned x, unsigned y,unsigned width, unsigned height,
24 unsigned char r, unsigned char g, unsigned char b)
25 {
26 unsigned i,j,k;
27 y=info.height-y;
28
29 for(i=y-height;i<=y;i++)
30 {
31 for(j=x;j<=(x+width);j++)
32 {
33 k=3*(i*info.width+j);
34 bitmap[k+0] = b;
35 bitmap[k+1] = g;
36 bitmap[k+2] = r;
37 }
38 }
39 }
40 #endif
1 #ifndef _ISBN_H_
2 #define _ISBN_H_
3
4 #include"use.h"
5 unsigned char left_odd[10]={13,25,19,61,35,49,47,59,55,11};
6 unsigned char left_even[10]={39,51,27,33,29,57,5,17,9,23};
7 unsigned char right_even[10]={114,102,108,66,92,78,80,68,72,116};
8 unsigned char flag13[10]={63,52,50,49,44,38,35,42,41,37};
9 unsigned char num13[14];
10 struct code_isbn
11 {
12 unsigned eprefix; //3 prefix_element
13 unsigned eregroup; //1 registration_group_element
14 unsigned eregistrant; //3 registrant_element
15 unsigned epublication; //5 publication_element
16 unsigned check_digit; //1 check_digit
17 };
18
19 struct code_isbn isbn;
20
21 unsigned char get_digit(unsigned key,unsigned n)
22 {
23 unsigned i;
24 for(i=1;i<n;i++)
25 {
26 key/=10;
27 }
28 return (key%10);
29 }
30 void cal_check_digit(void)
31 {
32 unsigned key=0,i,p=13;
33 for(i=3;i>=1;i--)
34 {
35 num13[p--]=get_digit(isbn.eprefix,i);
36 }
37 num13[p--]=get_digit(isbn.eregroup,1);
38 for(i=3;i>=1;i--)
39 {
40 num13[p--]=get_digit(isbn.eregistrant,i);
41 }
42 for(i=5;i>=1;i--)
43 {
44 num13[p--]=get_digit(isbn.epublication,i);
45 }
46 for(i=13;i>=2;i--)
47 {
48 if(i%2)key+=num13[i];
49 else key+=3*num13[i];
50 }
51 key%=10;
52 if(key)
53 {
54 isbn.check_digit=10-key;
55 }
56 else
57 {
58 isbn.check_digit=key;
59 }
60 num13[1]=isbn.check_digit;
61 }
62 void draw_isbn(unsigned a,unsigned b,unsigned c,unsigned d)
63 {
64 unsigned char key,m,n,show; //show=1为寄 0为偶
65 unsigned i;
66 isbn.eprefix=a;
67 isbn.eregroup=b;
68 isbn.eregistrant=c;
69 isbn.epublication=d;
70 cal_check_digit();
71 key=flag13[num13[13]];
72 //设置大小及背景
73 init_bmp_header(500,300);
74 set_color_bk(0xFF,0xFF,0xFF);
75 //画起始符
76 draw_rect(60,0,4,300,0x00,0x00,0x00);
77 draw_rect(68,0,4,300,0x00,0x00,0x00);
78 i=72;
79 //画左边区域
80 for(m=1;m<=6;m++)
81 {
82 show=((key>>(6-m))&1)?left_odd[num13[13-m]]:left_even[num13[13-m]];
83 for(n=1;n<=7;n++)
84 {
85 if((show>>(7-n))&1)draw_rect(i,0,4*1,280,0x00,0x00,0x00);
86 i+=4;
87 }
88 }
89 //画中间分隔符
90 i+=4; //白
91 draw_rect(i,0,4*1,300,0x00,0x00,0x00);
92 i+=4;
93 i+=4; //白
94 draw_rect(i,0,4*1,300,0x00,0x00,0x00);
95 i+=4;
96 i+=4; //白
97 //画右侧
98 for(m=1;m<=6;m++)
99 {
100 show=right_even[num13[7-m]];
101 for(n=1;n<=7;n++)
102 {
103 if((show>>(7-n))&1)draw_rect(i,0,4*1,280,0x00,0x00,0x00);
104 i+=4;
105 }
106 }
107 //画终止符
108 draw_rect(i,0,4*1,300,0x00,0x00,0x00);
109 i+=4;
110 i+=4; //白
111 draw_rect(i,0,4*1,300,0x00,0x00,0x00);
112 i+=4;
113 write_bmp();
114 }
115
116 #endif
相关阅读 更多 +