QRGenerator.cpp ( File view )

  • By green75 2014-11-06
  • View(s):334
  • Download(s):8
  • Point(s): 3
			//	---------------------------------------------------------------------------
//		
//		QRGenerator
//
//		Create:			15/05/2013
//		Last update:	15/05/2013
//
//		Author:	TWOTM
//
//
//		Note:
//
//		/o ULTRAMUNDUM FOUNDATION - all rights reserved
//	---------------------------------------------------------------------------


//	-------------------------------------------------------
//	Includes
//	-------------------------------------------------------

#include "stdafx.h"
#include <string.h>
#include <errno.h>
#include <conio.h>
#include <ctype.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <wchar.h>

#include "qrencode.h"
//	-------------------------------------------------------


//	-------------------------------------------------------
//	DEFines
//	-------------------------------------------------------

#define QRCODE_TEXT					"http://www.ultramundum.org/index.htm";		// Text to encode into QRCode
#define OUT_FILE					"C:/test.bmp"								// Output file name
#define OUT_FILE_PIXEL_PRESCALER	8											// Prescaler (number of pixels in bmp file for each QRCode pixel, on each dimension)

#define PIXEL_COLOR_R				0											// Color of bmp pixels
#define PIXEL_COLOR_G				0
#define PIXEL_COLOR_B				0xff

	// BMP defines

typedef unsigned short	WORD;
typedef unsigned long	DWORD;
typedef signed long		LONG;

#define BI_RGB			0L

#pragma pack(push, 2)

typedef struct  
	{

	WORD    bfType;
	DWORD   bfSize;
	WORD    bfReserved1;
	WORD    bfReserved2;
	DWORD   bfOffBits;
	
} BITMAPFILEHEADER;

typedef struct 
	{

	DWORD      biSize;
	LONG       biWidth;
	LONG       biHeight;
	WORD       biPlanes;
	WORD       biBitCount;
	DWORD      biCompression;
	DWORD      biSizeImage;
	LONG       biXPelsPerMeter;
	LONG       biYPelsPerMeter;
	DWORD      biClrUsed;
	DWORD      biClrImportant;
	
} BITMAPINFOHEADER;

#pragma pack(pop)
//	-------------------------------------------------------


//	-------------------------------------------------------
//	Main
//	-------------------------------------------------------

int _tmain(int argc, _TCHAR* argv[])
{

char*			szSourceSring = QRCODE_TEXT;
unsigned int	unWidth, x, y, l, n, unWidthAdjusted, unDataBytes;
unsigned char*	pRGBData, *pSourceData, *pDestData;
QRcode*			pQRC;
FILE*			f;

/*
 * Create a symbol from the string. The library automatically parses the input
 * string and encodes in a QR Code symbol.
 * @warning This function is THREAD UNSAFE when pthread is disabled.
 * @param string input string. It must be NUL terminated.
 * @param version version of the symbol. If 0, the library chooses the minimum
 *                version for the given input data.
 * @param level error correction level.
 * @param hint tell the library how non-alphanumerical characters should be
 *             encoded. If QR_MODE_KANJI is given, kanji characters will be
 *             encoded as Shif-JIS characters. If QR_MODE_8 is given, all of
 *             non-alphanumerical characters will be encoded as is. If you want
 *             to embed UTF-8 string, choose this.
 * @param casesensitive case-sensitive(1) or not(0).
 * @return an instance of QRcode class. The version of the result QRcode may
 *         be larger than the designated version. On error, NULL is returned,
 *         and errno is set to indicate the error. See Exceptions for the
 *         details.
 * @throw EINVAL invalid input object.
 * @throw ENOMEM unable to allocate memory for input objects.
 * @throw ERANGE input data is too large.
 */

		// Compute QRCode

	if (pQRC = QRcode_encodeString(szSourceSring, 0, QR_ECLEVEL_H, QR_MODE_8, 1))
		{

		unWidth = pQRC->width;
		unWidthAdjusted = unWidth * OUT_FILE_PIXEL_PRESCALER * 3;
		if (unWidthAdjusted % 4)
			unWidthAdjusted = (unWidthAdjusted / 4 + 1) * 4;
		unDataBytes = unWidthAdjusted * unWidth * OUT_FILE_PIXEL_PRESCALER;

			// Allocate pixels buffer

		if (!(pRGBData = (unsigned char*)malloc(unDataBytes)))
			{

			printf("Out of memory");
			exit(-1);
			
}
			
			// Preset to white

		memset(pRGBData, 0xff, unDataBytes);


			// Prepare bmp headers

		BITMAPFILEHEADER kFileHeader;
		kFileHeader.bfType = 0x4d42;  // "BM"
		kFileHeader.bfSize =	sizeof(BITMAPFILEHEADER) +
								sizeof(BITMAPINFOHEADER) +
								unDataBytes;
		kFileHeader.bfReserved1 = 0;
		kFileHeader.bfReserved2 = 0;
		kFileHeader.bfOffBits =	sizeof(BITMAPFILEHEADER) +
								sizeof(BITMAPINFOHEADER);

		BITMAPINFOHEADER kInfoHeader;
		kInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
		kInfoHeader.biWidth = unWidth * OUT_FILE_PIXEL_PRESCALER;
		kInfoHeader.biHeight = -((int)unWidth * OUT_FILE_PIXEL_PRESCALER);
		kInfoHeader.biPlanes = 1;
		kInfoHeader.biBitCount = 24;
		kInfoHeader.biCompression = BI_RGB;
		kInfoHeader.biSizeImage = 0;
		kInfoHeader.biXPelsPerMeter = 0;
		kInfoHeader.biYPelsPerMeter = 0;
		kInfoHeader.biClrUsed = 0;
		kInfoHeader.biClrImportant = 0;


			// Convert QrCode bits to bmp pixels

		pSourceData = pQRC->data;
		for(y = 0; y < unWidth; y++)
			{

			pDestData = pRGBData + unWidthAdjusted * y * OUT_FILE_PIXEL_PRESCALER;
			for(x = 0; x < unWidth; x++)
				{

				if (*pSourceData & 1)
					{

					for(l = 0; l < OUT_FILE_PIXEL_PRESCALER; l++)
						{

						for(n = 0; n < OUT_FILE_PIXEL_PRESCALER; n++)
							{

							*(pDestData +		n * 3 + unWidthAdjusted * l) =	PIXEL_COLOR_B;
							*(pDestData + 1 +	n * 3 + unWidthAdjusted * l) =	PIXEL_COLOR_G;
							*(pDestData + 2 +	n * 3 + unWidthAdjusted * l) =	PIXEL_COLOR_R;
							
}
						
}
					
}
				pDestData += 3 * OUT_FILE_PIXEL_PRESCALER;
				pSourceData++;
				
}
			
}


			// Output the bmp file

		if (!(fopen_s(&f, OUT_FILE, "wb")))
			{

			fwrite(&kFileHeader, sizeof(BITMAPFILEHEADER), 1, f);
			fwrite(&kInfoHeader, sizeof(BITMAPINFOHEADER), 1, f);
			fwrite(pRGBData, sizeof(unsigned char), unDataBytes, f);
			
			fclose(f);
			
}
		else
			{

			printf("Unable to open file");
			exit(-1);
			
}

			// Free data

		free(pRGBData);
		QRcode_free(pQRC);
		
}
	else
		{

		printf("NULL returned");
		exit(-1);
		
}

	return 0;

}
//	-------------------------------------------------------

			
...
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

QRGenerator.cpp (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