QR code is very popular at the moment, want to implement the two-dimensional code in the C/S architecture, at the same time can print the label is not easy. There are no ready-made tutorials and source code for use. The following is a step-by-step implementation of the two-dimensional code picture to do a description.
1, QR Code code common program module
2, call the two-dimensional code generation picture and Sequence number module
3, Program logic module, format string converted to two-dimensional code picture
4. Set Table template call Save good QR code picture printing
The above 4 steps are implemented on the module in the previous step, which means that the function of the previous step is called.
1, the implementation of two DLLs, I also find on-line, I am not Daniel, before Google inside see cattle people with C + + implementation of the source code. Admire but I do not understand.
PtImageRW.dll
PtQREncode.dll
http://download.csdn.net/detail/mycoolme5/8401145
2, call the above two DLL implementation, is also a public module, the code is as follows
Unit Twodbarcode;
Interface
Uses
Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
Dialogs,extctrls;
Const
Pt_qrencode_fail =$00000000; An operation is Failed.
Pt_qrencode_success =$00000001; An operation is successful.
Pt_qrencode_alloc_error =$00000200; Error while allocating the memory.
Pt_qrencode_data_big =$00000201; Data to being encoded is too big.
Pt_qrencode_size_small =$00000202; The size of image to being pasted the symbol is too small.
Pt_qrencode_image_invalid =$00000203; The image to being pasted is invalid.
QR Code ECC Level constants
pt_qr_ecclevel_l =$0001; Use of ECC level L. (7%)
Pt_qr_ecclevel_m =$0000; Use of ECC level M. (15%)
Pt_qr_ecclevel_q =$0003; Use ECC level Q. (25%)
Pt_qr_ecclevel_h =$0002; Use of ECC level H. (30%)
QR Code Version Constants
Pt_qr_version_auto =$0000; Determine the version by the Engine,then with the smallest version that can contain the data.
QR Code Mask Number Constants
Pt_qr_masknumber_auto =$0008; Determine the mask number by the engine.
Type
Pptqrencodestruct=^ptqrencodestruct;
Ptqrencodestruct = Record
Pdata:pchar; Pointer to the data to be encoded.
Ndatalength:integer; Length of the data in bytes.
Wversion:smallint; The version of the QR Code.
Wmasknumber:smallint; The mask number of the QR Code.
Wecclevel:smallint ; Determines the ECC level for encoding a QR Code symbol.
Wmodule:smallint ; The smallest element size in pixels.
Wgrouptotal:smallint ; The number of symbols that belong to the group.
Wgroupindex:smallint ; The index of the symbol in the group
Wleftspace:smallint ; The left space of the symbol in pixels while generating Image.
Wrightspace:smallint ; The right space of the symbol in pixels while generating Image.
Wtopspace:smallint ; The top space of the symbol in pixels while generating Image.
Wbottomspace:smallint ; The bottom space of the symbol in pixels while generating Image.
End;
Const
Pt_imagerw_fail =$00000000; An error occured in an operation.
Pt_imagerw_success =$00000001; An operation is successful.
Pt_imagerw_alloc_error =$00000100; Error while allocating memory.
Pt_imagerw_format_unsupported =$00000101; The format of image is unsupported.
Type
Pptimagestruct=^ptimagestruct;
Ptimagestruct = Record
Dwwidth:dword; The width of the image in pixels.
Dwheight:dword; The height of the image in pixels.
Pbits:pbyte; Pointer to the image data.
Ppalette:pbyte; Pointer to the Palette data (Rgbquad) for 1,4,8 bit image.
Wbitsperpixel:smallint//number of bits per pixel.
End;
Tlotno = Record
PCODE:STRING[1];
YY:STRING[1];
DD:STRING[2];
MM:STRING[2];
FLUT:STRING[3];
End
Tlotnofile = file of Tlotno;
Procedure Readrecord (const afilename:string; var rec:tlotno);
Procedure Writerecord (const afilename:string; var rec:tlotno);
function Getlotno (frtcod:string): string;
Procedure Ptqrencodeinit (pencode:pptqrencodestruct);
stdcall; Far External ' PtQREncode.dll ' name ' Ptqrencodeinit ';
Function Ptqrencode (pencode:pptqrencodestruct; pimage:pptimagestruct): Integer;
stdcall; Far External ' PtQREncode.dll ' name ' Ptqrencode ';
Function ptqrencodetoimage (pencode:pptqrencodestruct; pimage:pptimagestruct;
Startx:integer; Starty:integer): Integer;
stdcall; Far External ' PtQREncode.dll ' name ' Ptqrencodetoimage ';
The Red section is a feature implemented in two DLLs.
Procedure ptinitimage (pimage:pptimagestruct);
stdcall; Far External ' PtImageRW.dll ' name ' Ptinitimage ';
Function ptloadimage (filename:string; pimage:pptimagestruct; Frameindex:dword): Integer;
stdcall; Far External ' PtImageRW.dll ' name ' Ptloadimage ';
Function ptsaveimage (filename:string; pimage:pptimagestruct): Integer;
stdcall; Far External ' PtImageRW.dll ' name ' Ptsaveimage ';
Function Ptcreateimage (pimage:pptimagestruct; Imagesize:dword; Palettesize:dword): Integer;
stdcall; Far External ' PtImageRW.dll ' name ' Ptcreateimage ';
Procedure ptfreeimage (pimage:pptimagestruct);
stdcall; Far External ' PtImageRW.dll ' name ' Ptfreeimage ';
Procedure GetCode (codestr:string;m_image:ptimagestruct;tempname:string);
Implementation
Procedure GetCode (codestr:string;m_image:ptimagestruct;tempname:string);
Var
Ret:integer;
M_encode:ptqrencodestruct;
Begin
Ptqrencodeinit (@m_encode);
M_encode.pdata: = PChar (CODESTR);
M_encode.ndatalength: =lstrlen (M_encode.pdata);
RET: = Ptqrencode (@m_encode, @m_image);
IF ret <> Pt_qrencode_success Then
Begin
ShowMessage (' Encode Error ');
Exit;
End;
RET: = Ptsaveimage (Tempname, @m_image);
IF ret <> Pt_imagerw_success Then
ShowMessage (' Save BMP File Error ');
Ptfreeimage (@m_image);
End
The following three functions, one is to read a file of confidence, one is to write a file, a message,
The last one builds the serial number according to the date
Procedure Readrecord (const afilename:string; var rec:tlotno);
Var
F:tlotnofile;
Begin
AssignFile (F, afilename);
{$I-}
Reset (F);
{$I +}
If IOResult = 0 Then
Begin
Read (F, REC);
CloseFile (F);
End If IOResult
End
Procedure Writerecord (const afilename:string; var rec:tlotno);
Var
F:tlotnofile;
Begin
AssignFile (F, afilename);
Rewrite (F);
Write (F, REC);
CloseFile (F);
End
function Getlotno (frtcod:string): string;
Var
Testrec:tlotno;
Readrec:tlotno;
tmpfmt:string;
yy,mm,dd:string;
Begin
TMPFMT: =formatdatetime (' YYYYMMDD ', now);
YY: = Copy (tmpfmt,4,1);
MM: = Copy (tmpfmt,5,2);
DD: = Copy (tmpfmt,7,2);
Testrec.pcode: = Frtcod;
Testrec.yy: = yy;
TESTREC.DD: = DD;
TESTREC.MM: = mm;
Readrecord ('. \lot.dat ', Readrec);
if (Readrec.pcode = Testrec.pcode) and (readrec.yy = testrec.yy)
and (readrec.dd = testrec.dd) and (readrec.mm = testrec.mm) Then
Begin
Testrec.flut: = IntToStr (Strtoint (Readrec.flut) + 1);
End
Else
Testrec.flut: = ' 100 ';
Writerecord ('. \lot.dat ', Testrec);
Readrecord ('. \lot.dat ', Readrec);
Result: =readrec.pcode + readrec.yy + readrec.dd
+ readrec.mm + readrec.flut;
End
End.
3, Program logic module, call 2 of the correlation function implementation
Unit Zhizhuo;
Interface
Uses
Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
Dialogs, BASE_CCL, DB, ADODB, Extctrls, Stdctrls, Buttons,Twodbarcode;
Type
TFRM_ZHIZHUOCCL = Class (TFRM_BASECCL)
Label5:tlabel;
Custpo:tedit;
Codeimage:timage; Picture Display
Lbl1:tlabel;
Printnum:tedit;
Procedure Bitbtn3click (Sender:tobject);
Procedure Speedbutton1click (Sender:tobject);
Procedure Formcreate (Sender:tobject);
Procedure Printnumchange (Sender:tobject);
Private
{Private declarations}
Public
{Public declarations}
End
Var
FRM_ZHIZHUOCCL:TFRM_ZHIZHUOCCL;
M_image:ptimagestruct;
Implementation
{$R *.DFM}
Uses Dm;
Procedure TFRM_ZHIZHUOCCL. Bitbtn3click (Sender:tobject);
Var
codestr:string;
I:integer;
Begin
tablename:= ' ZHIZHUOCCL ';
reportname:= ' ZHIZHUOCCL.FR3 '; Report templates
Custpo. TEXT:=MCUSTPO;
For i:=0 to Strtoint (printnum.text)-1 do
Begin
Two-dimensional Code character information
codestr:=getlotno (' 2 ') + ' # ' +trim (CUSTPO. Text) + ' # ' + ' P ' + ' # ' +trim (custcode.text) + ' # '
+trim (quantity.text) + ' # ' +trim (Lotno. Text) + ' # ' + ' HI12 ' + ' # ' +trim (partnumber. Text)
+ ' # ' +stringreplace (Trim (mfgdate.text), '/', '-', [ Rfreplaceall]);
dynamically generate images automatically, each with a serial number
GetCode (Codestr,m_image,inttostr (i) + '. bmp ');
End
inherited;
End
Procedure TFRM_ZHIZHUOCCL. Speedbutton1click (Sender:tobject);
Begin
tablename:= ' ZHIZHUOCCL ';
reportname:= ' ZHIZHUOCCL.FR3 ';
inherited;
End
Procedure TFRM_ZHIZHUOCCL. Formcreate (Sender:tobject);
Begin
inherited;
Ptinitimage (@m_image);
End
Procedure Tfrm_zhizhuoccl.printnumchange (Sender:tobject);
Begin
inherited;
if (Strtoint (printnum. Text)) <=0 Then
ShowMessage (' No effect numbers ');
End
End.
4. Each set of tables calls the IntToStr (i) + '. bmp ' generated in the previous step to generate a usable QR code image
Two-dimensional code development steps