⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qrspec.c

📁 二维码QR的linux平台下的编码源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
	if(version < 2) return NULL;	al = (QRspec_Alignment *)malloc(sizeof(QRspec_Alignment));	width = qrspecCapacity[version].width;	d = alignmentPattern[version][1] - alignmentPattern[version][0];	if(d < 0) {		w = 2;	} else {		w = (width - alignmentPattern[version][0]) / d + 2;	}	al->n = w * w - 3;	al->pos = (int *)malloc(sizeof(int) * al->n * 2);	if(al->n == 1) {		al->pos[0] = alignmentPattern[version][0];		al->pos[1] = alignmentPattern[version][0];		return al;	}#if 0	/* Just for debug purpose */	printf("%d ", version);	cx = alignmentPattern[version][0];	for(x=0; x<w-1; x++) {		printf(" %3d", cx);		cx += d;	}	printf("\n");#endif	p = al->pos;	cx = alignmentPattern[version][0];	for(x=1; x<w - 1; x++) {		p[0] = 6;		p[1] = cx;		p[2] = cx;		p[3] = 6;		cx += d;		p += 4;	}	cy = alignmentPattern[version][0];	for(y=0; y<w-1; y++) {		cx = alignmentPattern[version][0];		for(x=0; x<w-1; x++) {			p[0] = cx;			p[1] = cy;			cx += d;			p += 2;		}		cy += d;	}	return al;}void QRspec_freeAlignment(QRspec_Alignment *al){	if(al != NULL) {		if(al->pos != NULL) {			free(al->pos);		}		free(al);	}}/****************************************************************************** * Version information pattern *****************************************************************************//** * Version information pattern (BCH coded). * See Table 1 in Appendix D (pp.68) of JIS X0510:2004. */static const unsigned int versionPattern[QRSPEC_VERSION_MAX - 6] = {	0x07c94, 0x085bc, 0x09a99, 0x0a4d3, 0x0bbf6, 0x0c762, 0x0d847, 0x0e60d,	0x0f928, 0x10b78, 0x1145d, 0x12a17, 0x13532, 0x149a6, 0x15683, 0x168c9,	0x177ec, 0x18ec4, 0x191e1, 0x1afab, 0x1b08e, 0x1cc1a, 0x1d33f, 0x1ed75,	0x1f250, 0x209d5, 0x216f0, 0x228ba, 0x2379f, 0x24b0b, 0x2542e, 0x26a64,	0x27541, 0x28c69};unsigned int QRspec_getVersionPattern(int version){	if(version < 7 || version > QRSPEC_VERSION_MAX) return 0;	return versionPattern[version -7];}/****************************************************************************** * Format information *****************************************************************************//* See calcFormatInfo in tests/test_qrspec.c */static const unsigned int formatInfo[4][8] = {	{0x77c4, 0x72f3, 0x7daa, 0x789d, 0x662f, 0x6318, 0x6c41, 0x6976},	{0x5412, 0x5125, 0x5e7c, 0x5b4b, 0x45f9, 0x40ce, 0x4f97, 0x4aa0},	{0x355f, 0x3068, 0x3f31, 0x3a06, 0x24b4, 0x2183, 0x2eda, 0x2bed},	{0x1689, 0x13be, 0x1ce7, 0x19d0, 0x0762, 0x0255, 0x0d0c, 0x083b}};unsigned int QRspec_getFormatInfo(int mask, QRecLevel level){	if(mask < 0 || mask > 7) return 0;	return formatInfo[level][mask];}/****************************************************************************** * Frame *****************************************************************************//** * Cache of initial frames. *//* C99 says that static storage shall be initialized to a null pointer * by compiler. */static unsigned char *frames[QRSPEC_VERSION_MAX + 1];/** * Put a finder pattern. * @param frame * @param width * @param ox,oy upper-left coordinate of the pattern */static void putFinderPattern(unsigned char *frame, int width, int ox, int oy){	static const unsigned char finder[] = {		0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1,		0xc1, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc1,		0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1,		0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1,		0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1,		0xc1, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc1,		0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1,	};	int x, y;	const unsigned char *s;	frame += oy * width + ox;	s = finder;	for(y=0; y<7; y++) {		for(x=0; x<7; x++) {			frame[x] = s[x];		}		frame += width;		s += 7;	}}/** * Put an alignment pattern. * @param frame * @param width * @param ox,oy center coordinate of the pattern */static void putAlignmentPattern(unsigned char *frame, int width, int ox, int oy){	static const unsigned char finder[] = {		0xa1, 0xa1, 0xa1, 0xa1, 0xa1,		0xa1, 0xa0, 0xa0, 0xa0, 0xa1,		0xa1, 0xa0, 0xa1, 0xa0, 0xa1,		0xa1, 0xa0, 0xa0, 0xa0, 0xa1,		0xa1, 0xa1, 0xa1, 0xa1, 0xa1,	};	int x, y;	const unsigned char *s;	frame += (oy - 2) * width + ox - 2;	s = finder;	for(y=0; y<5; y++) {		for(x=0; x<5; x++) {			frame[x] = s[x];		}		frame += width;		s += 5;	}}static unsigned char *QRspec_createFrame(int version){	unsigned char *frame, *p, *q;	int width;	int x, y;	unsigned int verinfo, mask;	QRspec_Alignment *alignment;	width = qrspecCapacity[version].width;	frame = (unsigned char *)malloc(width * width);	memset(frame, 0, width * width);	/* Finder pattern */	putFinderPattern(frame, width, 0, 0);	putFinderPattern(frame, width, width - 7, 0);	putFinderPattern(frame, width, 0, width - 7);	/* Separator */	p = frame;	q = frame + width * (width - 7);	for(y=0; y<7; y++) {		p[7] = 0xc0;		p[width - 8] = 0xc0;		q[7] = 0xc0;		p += width;		q += width;	}	memset(frame + width * 7, 0xc0, 8);	memset(frame + width * 8 - 8, 0xc0, 8);	memset(frame + width * (width - 8), 0xc0, 8);	/* Mask format information area */	memset(frame + width * 8, 0x84, 9);	memset(frame + width * 9 - 8, 0x84, 8);	p = frame + 8;	for(y=0; y<8; y++) {		*p = 0x84;		p += width;	}	p = frame + width * (width - 7) + 8;	for(y=0; y<7; y++) {		*p = 0x84;		p += width;	}	/* Timing pattern */	p = frame + width * 6 + 8;	q = frame + width * 8 + 6;	for(x=1; x<width-15; x++) {		*p =  0x90 | (x & 1);		*q =  0x90 | (x & 1);		p++;		q += width;	}	/* Alignment pattern */	alignment = QRspec_getAlignmentPattern(version);	if(alignment != NULL) {		for(x=0; x<alignment->n; x++) {			putAlignmentPattern(frame, width,					alignment->pos[x*2], alignment->pos[x*2+1]);		}		QRspec_freeAlignment(alignment);	}	/* Version information */	if(version >= 7) {		verinfo = QRspec_getVersionPattern(version);		p = frame + width * (width - 11);		mask = 0x20000;		for(x=0; x<6; x++) {			for(y=0; y<3; y++) {				p[width * y + x] = 0x88 | ((mask & verinfo) != 0);				mask = mask >> 1;			}		}		p = frame + width - 11;		mask = 0x20000;		for(y=0; y<6; y++) {			for(x=0; x<3; x++) {				p[x] = 0x88 | ((mask & verinfo) != 0);				mask = mask >> 1;			}			p += width;		}	}	/* and a little bit... */	frame[width * (width - 8) + 8] = 0x81;	return frame;}unsigned char *QRspec_newFrame(int version){	unsigned char *frame;	int width;	if(version < 1 || version > QRSPEC_VERSION_MAX) return NULL;	if(frames[version] == NULL) {		frames[version] = QRspec_createFrame(version);	}	width = qrspecCapacity[version].width;	frame = (unsigned char *)malloc(width * width);	memcpy(frame, frames[version], width * width);	return frame;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -