二维码当下很流行,想要在C/S架构中实现本机二维码,同时能列印标签还是挺不容易的。没有现成的教程和源代码供使用。下面就一步一步实现本机二维码图片做说明。
1、二维码编码公共程序模块
2、调用二维码生成图片和顺序号模块
3、程序逻辑模块,格式字符串转换为二维码图片
4、套表模板调用保存好的二维码图片列印
上面4个步骤,都是在上一步的模块上实现的,也就是说会调用上一步的函数。
1、实现两个dll,我也是网上找的,自己也不是大牛,之前在google里面看到牛人用c++实现的源码。佩服但是自己看不懂。
PtImageRW.dll
PtQREncode.dll
下载地址 http://download.csdn.net/detail/mycoolme5/8401145
2、调用上面两个dll实现,也是公共模块,代码如下
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 be encoded is too big.
PT_QRENCODE_SIZE_SMALL =$00000202; //The size of image to be pasted the symbol is too small.
PT_QRENCODE_IMAGE_INVALID =$00000203; //The image to be pasted is invalid.
//QR Code ECC level constants
PT_QR_ECCLEVEL_L =$0001; //Use ECC level L. (7% )
PT_QR_ECCLEVEL_M =$0000; //Use ECC level M. (15%)
PT_QR_ECCLEVEL_Q =$0003; //Use ECC level Q. (25%)
PT_QR_ECCLEVEL_H =$0002; //Use ECC level H. (30%)
//QR Code version constants
PT_QR_VERSION_AUTO =$0000; //Determine the version by the engine,then use 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';
红色部分为两个dll中实现的功能。
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;
下面三个功能,一个是读文件的一条信心,一个是写文件的一条信息,
最后一个根据年月日生成序列号
procedure ReadRecord(const AFileName: string; var Rec: Tlotno);
varF: 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、程序逻辑模块,调用2的相关函数实现
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; 图片显示
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'; 报表模板
CUSTPO.Text:=MCUSTPO;
for i:=0 to strtoint(printnum.text)-1 do
begin
二维码字符信息
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]);
动态自动生成图片,每个图片都有序列号
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('無效數字');
end;
end.
4、每个套表调用上一步生成的inttostr(i)+'.bmp',就生成可用的二维码图片