二维码开发步骤

二维码当下很流行,想要在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);

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、程序逻辑模块,调用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‘,就生成可用的二维码图片

时间: 2024-07-30 11:50:42

二维码开发步骤的相关文章

【转】Android平台下利用zxing实现二维码开发

http://www.cnblogs.com/dolphin0520/p/3355728.html 现在走在大街小巷都能看到二维码,而且最近由于项目需要,所以研究了下二维码开发的东西,开源的二维码扫描库主要有zxing和zbar,zbar在iPos平台上应用比较成熟,而在Android平台上主流还是用zxing库,因此这里主要讲述如何利用zxing进行二维码开发. 1.如何将zxing的Android源码导入工程. 在导入zxing的android源码之前,先去官方下载zxing的源码:http

Android平台下利用zxing实现二维码开发

http://www.cnblogs.com/dolphin0520/p/3355728.html 现在走在大街小巷都能看到二维码,而且最近由于项目需要,所以研究了下二维码开发的东西,开源的二维码扫描库主要有zxing和zbar,zbar在iPos平台上应用比较成熟,而在Android平台上主流还是用zxing库,因此这里主要讲述如何利用zxing进行二维码开发. 1.如何将zxing的Android源码导入工程. 在导入zxing的android源码之前,先去官方下载zxing的源码:http

Android开发之扫描二维码开发

原贴地址:http://www.cnblogs.com/Fndroid/p/5540688.html 二维码其实有很多种,但是我们常见的微信使用的是一种叫做QRCode的二维码,像下面这样的,可以放心的扫,这只是我的博客主页链接啦: 关于QR码编码的二维码,我们要知道几个特点: 1. 扫描时可以从各个角度去扫,也就是旋转多少度都没关系,不信吗?下次去肯德基买单试试咯. 2. 二维码有容错率,容错率越大,生成的二维码也就越复杂,但是越不容易出错,并且,当二维码被遮挡的时候也越容易扫描出来.这里我上

python Qrcode二维码开发环境搭建

开发环境环境 系      统:windows10 x64 开发工具:python2.7  +qrcode 最近发现一个挺好用的工具,思维导图,可以让我们建立一个清晰思路,我个人比较喜欢用xmind6,所以环境搭建方法是使用思维导图工具编写 . python qrcode 作用: 现在最流行事就是,在外面经常有人让我们扫二维码,而这个插件包的作用就是生成二维码. 以下就是在windows环境下搭建Qrcode环境的具体过程,详见下图.所需要的资料详见附件.

java二维码开发

之前就写过很多关于二维码的东西,一直没有时间整理一下,所以呢今天就先来介绍一下如何利用java开发二维码.生成二维码有很多jar包可以实现,例如Zxing,QRcode,前者是谷歌的,后者日本的,这里我将对这两种方式的具体实现方法做简单介绍. 一.二维码的原理 二维条形码最早发明于日本,它是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的,在代码编制上巧妙地利用构成计算机内部逻辑基础的“0”.“1”比特流的概念,使用若干个与二进制相对应的几何形体来表示文字

Halcon的二维码解码步骤和解码技巧

一.二维码简介 1 . 类型多样,常见的有QR Code二维码. Data Matrix二维码等. 2.高密度编码,信息容量大. 3.容错能力强,具有纠错功能:二维码因穿孔.污损等引起局部损坏时,照样可以正确得到识读,损毁面积达50%仍可恢复信息. 4.译码可靠性高:它比普通条码译码错误率百万分之二要低得多,误码率不超过千万分之一. 5.可引入加密措施:保密性.防伪性好. 二.图像预处理和二维码增强 对比度太低:scale_image(或使用外部程序scale_image_range),增强图像

iOS开发-二维码

二维码 从ios7开始集成了二维码的生成和读取功能 此前被广泛使用的zbarsdk目前不支持64位处理器 生成二维码的步骤: 倒入CoreImage框架 通过滤镜CIFilter生成二维码 二维码的内容(传统的条形码职能放数字) 纯文本 名片 URL 二维码的生成 // 1.创建过滤器 CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"]; // 2.恢复默认 [filter setDefaults]; //

超实用python小项目--基于python的手机通讯录二维码生成网站--1、项目介绍和开发环境

这个项目是我做完整的第一个python web项目,对于新手来说,这个项目绝对是一个特别好的练手项目. 起名还是困难,但是自己确实比较烦输入这么长的名字(手机通讯录二维码生成网站)去定义这个网站,所以还是给这个项目起个名字吧,叫什么呢?就叫 "鹅日通讯录"吧(Earth address list). --------------------------------------------------------------------------------------------我是

iOS开发——高级篇——二维码的生产和读取

一.二维码的生成 从iOS7开始集成了二维码的生成和读取功能此前被广泛使用的zbarsdk目前不支持64位处理器 生成二维码的步骤:导入CoreImage框架通过滤镜CIFilter生成二维码 二维码的内容(传统的条形码只能放数字):纯文本名片URL // 1. 实例化二维码滤镜 CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"]; // 2. 恢复滤镜的默认属性 [filter setDefaults];