qrenc.c ( File view )

  • By green75 2014-11-06
  • View(s):334
  • Download(s):8
  • Point(s): 3
			/**
 * qrencode - QR Code encoder
 *
 * QR Code encoding tool
 * Copyright (C) 2006-2012 Kentaro Fukuchi <kentaro@fukuchi.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#if HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <png.h>
#include <getopt.h>

#include "qrencode.h"

#define INCHES_PER_METER (100.0/2.54)

static int casesensitive = 1;
static int eightbit = 0;
static int version = 0;
static int size = 3;
static int margin = -1;
static int dpi = 72;
static int structured = 0;
static int micro = 0;
static QRecLevel level = QR_ECLEVEL_L;
static QRencodeMode hint = QR_MODE_8;

enum imageType {

	PNG_TYPE,
	EPS_TYPE,
	SVG_TYPE,
	ANSI_TYPE,
	ANSI256_TYPE,
	ASCII_TYPE,
	ASCIIi_TYPE

};

static enum imageType image_type = PNG_TYPE;

static const struct option options[] = {

	{
"help"         , no_argument      , NULL, 'h'
},
	{
"output"       , required_argument, NULL, 'o'
},
	{
"level"        , required_argument, NULL, 'l'
},
	{
"size"         , required_argument, NULL, 's'
},
	{
"symversion"   , required_argument, NULL, 'v'
},
	{
"margin"       , required_argument, NULL, 'm'
},
	{
"dpi"          , required_argument, NULL, 'd'
},
	{
"type"         , required_argument, NULL, 't'
},
	{
"structured"   , no_argument      , NULL, 'S'
},
	{
"kanji"        , no_argument      , NULL, 'k'
},
	{
"casesensitive", no_argument      , NULL, 'c'
},
	{
"ignorecase"   , no_argument      , NULL, 'i'
},
	{
"8bit"         , no_argument      , NULL, '8'
},
	{
"micro"        , no_argument      , NULL, 'M'
},
	{
"version"      , no_argument      , NULL, 'V'
},
	{
NULL, 0, NULL, 0
}

};

static char *optstring = "ho:l:s:v:m:d:t:Skci8MV";

static void usage(int help, int longopt)
{

	fprintf(stderr,
"qrencode version %s\n"
"Copyright (C) 2006-2012 Kentaro Fukuchi\n", QRcode_APIVersionString());
	if(help) {

		if(longopt) {

			fprintf(stderr,
"Usage: qrencode [OPTION]... [STRING]\n"
"Encode input data in a QR Code and save as a PNG or EPS image.\n\n"
"  -h, --help   display the help message. -h displays only the help of short\n"
"               options.\n\n"
"  -o FILENAME, --output=FILENAME\n"
"               write image to FILENAME. If '-' is specified, the result\n"
"               will be output to standard output. If -S is given, structured\n"
"               symbols are written to FILENAME-01.png, FILENAME-02.png, ...\n"
"               (suffix is removed from FILENAME, if specified)\n"
"  -s NUMBER, --size=NUMBER\n"
"               specify module size in dots (pixels). (default=3)\n\n"
"  -l {
LMQH
}, --level={
LMQH
}\n"
"               specify error correction level from L (lowest) to H (highest).\n"
"               (default=L)\n\n"
"  -v NUMBER, --symversion=NUMBER\n"
"               specify the version of the symbol. (default=auto)\n\n"
"  -m NUMBER, --margin=NUMBER\n"
"               specify the width of the margins. (default=4 (2 for Micro)))\n\n"
"  -d NUMBER, --dpi=NUMBER\n"
"               specify the DPI of the generated PNG. (default=72)\n\n"
"  -t {
PNG,EPS,SVG,ANSI,ANSI256,ASCII
}, --type={
PNG,EPS,SVG,ANSI,ANSI256,ASCII
}\n"
"               specify the type of the generated image. (default=PNG)\n\n"
"  -S, --structured\n"
"               make structured symbols. Version must be specified.\n\n"
"  -k, --kanji  assume that the input text contains kanji (shift-jis).\n\n"
"  -c, --casesensitive\n"
"               encode lower-case alphabet characters in 8-bit mode. (default)\n\n"
"  -i, --ignorecase\n"
"               ignore case distinctions and use only upper-case characters.\n\n"
"  -8, --8bit   encode entire data in 8-bit mode. -k, -c and -i will be ignored.\n\n"
"  -M, --micro  encode in a Micro QR Code. (experimental)\n\n"
"  -V, --version\n"
"               display the version number and copyrights of the qrencode.\n\n"
"  [STRING]     input data. If it is not specified, data will be taken from\n"
"               standard input.\n"
			);
		
} else {

			fprintf(stderr,
"Usage: qrencode [OPTION]... [STRING]\n"
"Encode input data in a QR Code and save as a PNG or EPS image.\n\n"
"  -h           display this message.\n"
"  --help       display the usage of long options.\n"
"  -o FILENAME  write image to FILENAME. If '-' is specified, the result\n"
"               will be output to standard output. If -S is given, structured\n"
"               symbols are written to FILENAME-01.png, FILENAME-02.png, ...\n"
"               (suffix is removed from FILENAME, if specified)\n"
"  -s NUMBER    specify module size in dots (pixels). (default=3)\n"
"  -l {
LMQH
}    specify error correction level from L (lowest) to H (highest).\n"
"               (default=L)\n"
"  -v NUMBER    specify the version of the symbol. (default=auto)\n"
"  -m NUMBER    specify the width of the margins. (default=4 (2 for Micro))\n"
"  -d NUMBER    specify the DPI of the generated PNG. (default=72)\n"
"  -t {
PNG,EPS,SVG,ANSI,ANSI256,ASCII
}\n"
"               specify the type of the generated image. (default=PNG)\n"
"  -S           make structured symbols. Version must be specified.\n"
"  -k           assume that the input text contains kanji (shift-jis).\n"
"  -c           encode lower-case alphabet characters in 8-bit mode. (default)\n"
"  -i           ignore case distinctions and use only upper-case characters.\n"
"  -8           encode entire data in 8-bit mode. -k, -c and -i will be ignored.\n"
"  -M           encode in a Micro QR Code.\n"
"  -V           display the version number and copyrights of the qrencode.\n"
"  [STRING]     input data. If it is not specified, data will be taken from\n"
"               standard input.\n"
			);
		
}
	
}

}

#define MAX_DATA_SIZE (7090 * 16) /* from the specification */
static unsigned char *readStdin(int *length)
{

	unsigned char *buffer;
	int ret;

	buffer = (unsigned char *)malloc(MAX_DATA_SIZE + 1);
	if(buffer == NULL) {

		fprintf(stderr, "Memory allocation failed.\n");
		exit(EXIT_FAILURE);
	
}
	ret = fread(buffer, 1, MAX_DATA_SIZE, stdin);
	if(ret == 0) {

		fprintf(stderr, "No input data.\n");
		exit(EXIT_FAILURE);
	
}
	if(feof(stdin) == 0) {

		fprintf(stderr, "Input data is too large.\n");
		exit(EXIT_FAILURE);
	
}

	buffer[ret] = '\0';
	*length = ret;

	return buffer;

}

static FILE *openFile(const char *outfile)
{

	FILE *fp;

	if(outfile == NULL || (outfile[0] == '-' && outfile[1] == '\0')) {

		fp = stdout;
	
} else {

		fp = fopen(outfile, "wb");
		if(fp == NULL) {

			fprintf(stderr, "Failed to create file: %s\n", outfile);
			perror(NULL);
			exit(EXIT_FAILURE);
		
}
	
}

	return fp;

}

static int writePNG(QRcode *qrcode, const char *outfile)
{

	static FILE *fp; // avoid clobbering by setjmp.
	png_structp png_ptr;
	png_infop info_ptr;
	unsigned char *row, *p, *q;
	int x, y, xx, yy, bit;
	int realwidth;

	realwidth = (qrcode->width + margin * 2) * size;
	row = (unsigned char *)malloc((realwidth + 7) / 8);
	if(row == NULL) {

		fprintf(stderr, "Failed to allocate memory.\n");
		exit(EXIT_FAILURE);
	
}

	if(outfile[0] == '-' && outfile[1] == '\0') {

		fp = stdout;
	
} else {

		fp = fopen(outfile, "wb");
		if(fp == NULL) {

			fprintf(stderr, "Failed to create file: %s\n", outfile);
			perror(NULL);
			exit(EXIT_FAILURE);
		
}
	
}

	png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
	if(png_ptr == NULL) {

		fprintf(stderr, "Failed to initialize PNG writer.\n");
		exit(EXIT_FAILURE);
	
}

	info_ptr = png_create_info_struct(png_ptr);
	if(info_ptr == NULL) {

		fprintf(stderr, "Failed to initialize PNG write.\n");
		exit(EXIT_FAILURE);
	
}

	if(setjmp(png_jmpbuf(png_ptr))) {

		png_destroy_write_struct(&png_ptr, &info_ptr);
		fprintf(stderr, "Failed to write PNG image.\n");
		exit(EXIT_FAILURE);
	
}

	png_init_io(png_ptr, fp);
	png_set_IHDR(png_ptr, info_ptr,
			realwidth, realwidth,
			1,
			PNG_COLOR_TYPE_GRAY,
			PNG_INTERLACE_NONE,
			PNG_COMPRESSION_TYPE_DEFAULT,
			PNG_FILTER_TYPE_DEFAULT);
	png_set_pHYs(png_ptr, info_ptr,
			dpi * INCHES_PER_METER,
			dpi * INCHES_PER_METER,
			PNG_RESOLUTION_METER);
	png_write_info(png_ptr, info_ptr);

	/* top margin */
	memset(row, 0xff, (realwidth + 7) / 8);
	for(y=0; y<margin * size; y++) {

		png_write_row(png_ptr, row);
	
}

	/* data */
	p = qrcode->data;
	for(y=0; y<qrcode->width; y++) {

		bit = 7;
		memset(row, 0xff, (realwidth + 7) / 8);
		q = row;
		q += margin * size / 8;
		bit = 7 - (margin * size % 8);
		for(x=0; x<qrcode->width; x++) {

			for(xx=0; xx<size; xx++) {

				*q ^= (*p & 1) << bit;
				bit--;
				if(bit < 0) {

					q++;
					bit = 7;
				
}
			
}
			p++;
		
}
		for(yy=0; yy<size; yy++) {

			png_write_row(png_ptr, row);
		
}
	
}
	/* bottom margin */
	memset(row, 0xff, (realwidth + 7) / 8);
	for(y=0; y<margin * size; y++) {

		png_write_row(png_ptr, row);
	
}

	png_write_end(png_ptr, info_ptr);
	png_destroy_write_struct(&png_ptr, &info_ptr);

	fclose(fp);
	free(row);

	return 0;

}

static int writeEPS(QRcode *qrcode, const char *outfile)
{

	FILE *fp;
	unsigned char *row, *p;
	int x, y, yy;
	int realwidth;

	fp = openFile(outfile);
   
	realwidth = (qrcode->width + margin * 2) * size;
	/* EPS file header */
	fprintf(fp, "%%!PS-Adobe-2.0 EPSF-1.2\n"
				"%%%%BoundingBox: 0 0 %d %d\n"
				"%%%%Pages: 1 1\n"
				"%%%%EndComments\n", realwidth, realwidth);
	/* draw point */
	fprintf(fp, "/p {
 "
				"moveto "
				"0 1 rlineto "
				"1 0 rlineto "
				"0 -1 rlineto "
				"fill "
				"
} bind def "
				"%d %d scale ", size, size);
	
	/* data */
	p = qrcode->data;
	for(y=0; y<qrcode->width; y++) {

		row = (p+(y*qrcode->width));
		yy = (margin + qrcode->width - y - 1);
		
		for(x=0; x<qrcode->width; x++) {

			if(*(row+x)&0x1) {

				fpr
...
...
(Not finished, please download and read the complete file)
			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 3

Download
0 lines left, continue to read
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
01.97 kB
01.97 kB
QRGenerator.ilk366.95 kB2013-06-11 12:21
01.97 kB
01.97 kB
BuildLog.htm15.69 kB2013-06-11 12:21
01.97 kB
bitstream.c4.21 kB2013-05-15 12:07
bitstream.h1.40 kB2013-05-15 12:07
config.h230.00 B2013-05-15 12:07
mask.c6.79 kB2013-05-15 12:07
mask.h1.52 kB2013-05-15 12:07
mmask.c4.01 kB2013-05-15 12:07
mmask.h1.37 kB2013-05-15 12:07
mqrspec.c6.92 kB2013-05-15 12:07
mqrspec.h4.66 kB2013-05-15 12:07
qrenc.c21.53 kB2013-06-05 16:59
qrencode.c19.60 kB2013-05-15 12:07
qrencode.h20.17 kB2013-05-15 12:07
qrencode_inner.h2.71 kB2013-05-15 12:07
qrinput.c38.05 kB2013-05-15 12:07
qrinput.h3.57 kB2013-05-15 12:07
qrspec.c15.35 kB2013-05-15 12:07
qrspec.h5.70 kB2013-05-15 12:07
rscode.c8.96 kB2013-05-15 12:07
rscode.h1.43 kB2013-05-15 12:07
split.c7.20 kB2013-05-15 12:07
split.h1.87 kB2013-05-15 12:07
QRGenerator.cpp6.14 kB2013-05-15 12:07
QRGenerator.vcproj5.68 kB2013-06-11 12:20
QRGenerator.vcproj.NCC1701A.TWOTM.user1.39 kB2013-06-11 12:21
QRGenerator.vcproj.PROXIMA.TWOTM.user1.38 kB2013-05-15 12:07
01.97 kB
bitstream.obj15.25 kB2013-06-11 12:21
BuildLog.htm10.58 kB2013-06-11 12:21
mask.obj19.98 kB2013-06-11 12:21
mmask.obj12.95 kB2013-06-11 12:21
mqrspec.obj16.01 kB2013-06-11 12:21
mt.dep62.00 B2013-06-11 12:21
qrencode.obj40.71 kB2013-06-11 12:21
QRGenerator.exe.intermediate.manifest616.00 B2013-06-11 12:21
QRGenerator.obj34.64 kB2013-06-11 12:21
qrinput.obj62.77 kB2013-06-11 12:21
qrspec.obj26.07 kB2013-06-11 12:21
rscode.obj14.36 kB2013-06-11 12:21
split.obj16.79 kB2013-06-11 12:21
stdafx.obj25.93 kB2013-06-11 12:21
vc90.idb83.00 kB2013-06-11 12:21
vc90.pdb76.00 kB2013-06-11 12:21
stdafx.cpp298.00 B2013-05-15 12:07
stdafx.h320.00 B2013-05-15 12:07
targetver.h765.00 B2013-05-15 12:07
QRGenerator.ncb683.00 kB2013-06-11 12:21
QRGenerator.sln899.00 B2013-05-15 12:07
QRGenerator.suo23.00 kB2013-06-11 12:21
01.97 kB
QRGenerator.exe28.00 kB2013-06-11 12:21
QRGenerator.pdb267.00 kB2013-06-11 12:21
...
Sponsored links

qrenc.c (497.86 kB)

Need 3 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D