本人所在的单位是一家零售公司,因此对于条码的印制是经常遇上的。公司一共有4家店,每家店使用的条码打印机均为Zebra S4M。使用的条码打印程序是用VF6编写的,而大家对VF6不熟悉因此给后续的客制化开发带来很大的困难,因此本人想用自己比较熟悉的编程语言进行改写(后面的测试代码均使用VB6或VB.NET2005语言)。
首先对用VF6编写的打印程序进行了简单的分析,其实现原理大致如下。每个价签格式均使用ZPL语言编写好,然后再根据打印价签格式的不同调用不同的配置文件。配置文件内容如下:
^XA
^LH2
%BARCODE ^FO28,8^BEN,65,Y,N^FD@^FS
%BARCODE ^FO304,8^BEN,65,Y,N^FD@^FS
%ITEMDESP ^FO10,99^WINFONT(宋体,16,0,0,Y,N,Y),1,2^FD @^FS
%ITEMDESP ^FO280,99^WINFONT(宋体,16,0,0,Y,N,Y),1,2^FD @^FS
%ITEMNO ^FO35,133^ADN,28,14^FD @^FS
%ITEMNO ^FO320,133^ADN,28,14^FD @^FS
%QUANTITY ^PQ@^FS
^XZ
如果你接触过ZPL语言的话,对上面的代码你会比较熟悉,但是对于其中%BARCODE,^WINFONT(宋体,16,0,0,Y,N,Y),@的代码你可能觉得比较难理解,不要紧我会分析给大家看的。
^XA 发送给打印机的开始信号
^LH2,1 定义价签的原点位置
%BARCODE ^FO28,8^BEN,65,Y,N^FD@^FS %BARCODE这个是代表VF6数据库中的一张数据表中有叫BARCODE 的列,^FO28,8^BEN,65,Y,N代表的是,在价签的28,8的坐标位置打印EAN条码,条码高度为65个点。这里的@代表配置文件中的变量,它被数据库中BARCODE字段的数据替换。
%BARCODE ^FO304,8^BEN,65,Y,N^FD@^FS
%ITEMDESP ^FO10,99^WINFONT(宋体,16,0,0,Y,N,Y),1,2^FD @^FS ^WINTFONT是自己创造的一个函数,说白了这个函数就是为了解决不能打印汉字而设置的,如果你直接发送这个函数给打印机,打印机是不能够识别的,对于这个函数的转换,其实就是本篇文章的重点,会在后面做详细说明。
%ITEMDESP ^FO280,99^WINFONT(宋体,16,0,0,Y,N,Y),1,2^FD @^FS
%ITEMNO ^FO35,133^ADN,28,14^FD @^FS
%ITEMNO ^FO320,133^ADN,28,14^FD @^FS
%QUANTITY ^PQ@^FS
^XZ 发送结束信号
通过以上的分析你可能明白了配置文件的内容。
下面再说下打印原理,如果你使用WINDOWS的SPOOLER打印服务那么你是很难进行打印控制的。斑马打印机将打印指令集成在打印芯片中,你发送打印指令给打印机,打印机接受指令后才进行打印操作,而发送的打印指令就是ZPL打印语言,也就是上面价签配置文件的内容。通过上面的分析你可能明白了整个程序的机制。就是,程序读取价签配置数据,然后将数据库中的内容替换价签配置文件中需要替换的数据,最后形成一个ZPL指令流,最后发送给打印机,打印机在接受这些指令流后进行打印。
接下来我们说下汉字打印。Zebra S4M打印机是不能识别汉字的,那么怎么解决这个问题呢,我在网上搜索了下,大致有两种解决方案:
(一)使用一个名字叫Fnthex32.dll的连接库文件。这个文件有一个名为GETFONTHEX的导出函数,使用方式如下(VB6)
Declare Function GETFONTHEX Lib "./Fnthex32.dll" (ByVal chnstr As String, ByVal fontname As String, ByVal orient As Integer, ByVal height As Integer, ByVal width As Integer, ByVal bold As Integer, ByVal italic As Integer, ByVal hexbuf As String) As Integer
通过以下的测试
Private Sub Form_Click()
Dim cBuf As String * 1024
nCount = GETFONTHEX("一", "宋体", 0, 200, 200, 1, 0, cBuf)
Debug.Print cBuf
'Text1.Text = cBuf
End Sub
会得到这样的结果
~DGOUTSTR01,10192,052,z0z0p0z0z0p0z0z0p0z0z0p0z0z0p0z0z0p0z0z0p0z0z0p0jT07FkG01HFCk07IFjY01JFCjX07KFjW01LF8jV0MFEjU03NF8jT0OFEiN0gK03PF8jR0QFCV07jMFV07jMFCU07jNFU07jNFCT01jNFCU07jMFCU01jMFCV07KF8jW01HFCK0z0z0p0z0z0p0z0z0p0z0z0p0z0z0p0z0z0p0z0z0p0z0z0p0z0z0p0z0z0p0yJ0
这样的结果是不是有点眼熟啊,其实他就是将“一”字转换成图象再转换成十六进制编码最后进行ZPL的压缩,利用“~DG”命令将编码发送给打印机最后利用“^XG”命令进行图象打印。整个结果中~DGOUTSTR01这个命令是将图象编码发送给打印机并存贮在打印机存储卡上同时编码文件被命名为OUTSTR01。10192,052是指打印图像的总字节和每行的字节数,最后的东西就是图象编码。
(二)根据一的思路自己进行开发转换库,这种方式最灵活但难度比较大。下面我会重点对自己开发转换库进行讲解,后面的代码全部采用VB.NET2005开发。
对于自己开发首先你要解决下面几个问题。
(1)如何编码
一般来说我们的打印机都是进行点阵打印,那么对于一个点来说如果打印的话则编码为“1”否则为“0”,那么一个十六进制的数来说它可以对应4个点,比如我们对“....”可以编码成“1111”转换成十六进制数来说就是F,同理对“. ..”来说十六进制数就是B了。
(2)字节数的计算
总字节数=行宽*列高/8
每行字节数=行宽/8
(3)ZPL压缩代码
什么是ZPL压缩代码呢,比如我们有这么一个图形“…………”一共12个点,进行编码后会是“FFF”根据ZPL压缩定义的话,我们可以写成“IF”,你看是不是比原来的“FFF”少了一个数据啊,这样的话是有利于减少数据量,提高与打印机的通信速度。那么ZPL压缩是怎么定义的呢。
G H I J K L M N O P Q R S T U V W X Y 对应1,2,3,4……18,19。
g h i j k l m n o p q r s t u v w x y z 对应20,40,60,80……340,360,380,400。
由于十六进制数占用了A,B,C,D,E,F所以要从G或g开始。还有就是压缩代码不分先后顺序的,比如Hi8和iH8实质是一样的。
解决了上述问题后就看下原代码吧。
- Imports System.Text
- Imports System.Drawing
- Imports System.Drawing.Imaging
- Public Class FontConvertBmp
- Private m_FontName As String
- Private m_FontSize As Integer
- Private m_String As String
- Private m_FontBold As Boolean
- Private m_X As Integer, m_Y As Integer
- Private m_XZoom As Integer, m_YZoom As Integer
- Private List1 As List(Of Char), List2 As List(Of Integer)
- Private S As String
- Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As IntPtr) As Integer
- Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As IntPtr) As IntPtr
- Public Property XZoom() As Integer
- Get
- Return m_XZoom
- End Get
- Set(ByVal value As Integer)
- If value > 0 AndAlso value <= 10 Then
- m_XZoom = value
- End If
- End Set
- End Property
- Public Property YZoom() As Integer
- Get
- Return m_YZoom
- End Get
- Set(ByVal value As Integer)
- If value > 0 AndAlso value <= 10 Then
- m_YZoom = value
- End If
- End Set
- End Property
- Public Property X() As Integer
- Get
- Return m_X
- End Get
- Set(ByVal value As Integer)
- If value >= 0 Then
- m_X = value
- End If
- End Set
- End Property
- Public Property Y() As Integer
- Get
- Return m_Y
- End Get
- Set(ByVal value As Integer)
- If value >= 0 Then
- m_Y = value
- End If
- End Set
- End Property
- Public Property FontName() As String
- Get
- Return m_FontName
- End Get
- Set(ByVal value As String)
- m_FontName = value
- End Set
- End Property
- Public Property FontSize() As Integer
- Get
- Return m_FontSize
- End Get
- Set(ByVal value As Integer)
- If value > 7 AndAlso value <= 500 Then
- m_FontSize = value
- End If
- End Set
- End Property
- Public Property Text() As String
- Get
- Return m_String
- End Get
- Set(ByVal value As String)
- m_String = value
- End Set
- End Property
- Public Property FontBold() As Boolean
- Get
- Return m_FontBold
- End Get
- Set(ByVal value As Boolean)
- m_FontBold = value
- End Set
- End Property
- Public Sub New()
- m_FontName = "宋体"
- m_FontSize = 12
- m_FontBold = False
- m_X = 1
- m_Y = 1
- m_XZoom = 1
- m_YZoom = 1
- InitDictionary()
- End Sub
- Public Function Convert() As String
- Dim Hdc As IntPtr
- Hdc = CreateCompatibleDC(IntPtr.Zero)
- Dim Graphic As Graphics = Graphics.FromHdc(Hdc)
- Dim Width As Integer, Height As Integer
- Dim G As Graphics, Image As Bitmap
- Dim Font As Font
- If m_FontBold = True Then
- Font = New Font(m_FontName, m_FontSize, Drawing.FontStyle.Bold, GraphicsUnit.Pixel)
- Else
- Font = New Font(m_FontName, m_FontSize, Drawing.FontStyle.Regular, GraphicsUnit.Pixel)
- End If
- Width = (CInt(Graphic.MeasureString(m_String, Font).Width) / 8 + 1) * 8
- Height = Font.Height
- Image = New Bitmap(Width, Height, PixelFormat.Format32bppArgb)
- G = Graphics.FromImage(Image)
- G.Clear(Color.White)
- G.DrawString(m_String, Font, Brushes.Black, 0, 2)
- Dim Value As Integer = Width * Height
- Dim TempString As New StringBuilder(Value)
- Dim DesString As New StringBuilder(Value)
- DesString.Append("~DGOUTSTR01," & Width * Height / 8 & "," & Width / 8 & ",")
- Dim i As Integer, j As Integer
- Dim Sum As Integer
- For j = 0 To Height - 1
- For i = 0 To Width / 4 - 1
- Sum = 0
- For m As Integer = 0 To 3
- If Image.GetPixel(i * 4 + m, j).B = 0 Then
- Sum += 1 << (3 - m)
- End If
- Next
- TempString.Append(Hex(Sum))
- Next
- Next
- Dim Count As Integer = 1
- For i = 1 To TempString.Length - 1
- If TempString.Chars(i - 1) = TempString.Chars(i) Then
- Count += 1
- If i = TempString.Length - 1 Then
- DesString.Append(CompressCode(Count) & TempString.Chars(i))
- End If
- Else
- DesString.Append(CompressCode(Count) & TempString.Chars(i - 1))
- S = String.Empty
- Count = 1
- End If
- Next
- DesString.Append(vbCrLf & "^FO" & m_X.ToString & "," & m_Y.ToString & "^XGOUTSTR01," & m_XZoom & "," & m_YZoom & ",^FS")
- DeleteDC(Hdc)
- G.Dispose()
- Image.Dispose()
- Graphic.Dispose()
- Return DesString.ToString
- End Function
- Private Function CompressCode(ByRef Input As Integer) As String
- If Input > 1 Then
- For i As Integer = List1.Count - 1 To 0 Step -1
- If Input >= List2.Item(i) Then
- S &= List1.Item(i)
- Input -= List2.Item(i)
- CompressCode(Input)
- End If
- Next
- End If
- Return S
- End Function
- Private Sub InitDictionary()
-
- List1 = New List(Of Char)
- List2 = New List(Of Integer)
- For i As Integer = 0 To 18
- List1.Add(ChrW(71 + i))
- List2.Add(i + 1)
- Next
- For i As Integer = 0 To 19
- List1.Add(ChrW(103 + i))
- List2.Add(20 * (i + 1))
- Next
- End Sub
- End Class
接下来我们开始分析价签配置文件了,对于%开头的词我们仍然认定为数据库中列,@定义为变量,定义^WINFONT(变量1,变量2,变量3,变量4,变量5,变量6)函数,其中变量分别对应 字体名,字号,X坐标,Y坐标,是否为粗体,X方向放大倍数,Y方向放大倍数。程序的实现原理就是将“%”,“^WINFONT”,“@”通过正则表达式将其从文件中提取出来来,然后根据各自的功能进行相应的拆分,替换转换成ZPL指令流,最后再将其发送到打印机。
在写分析价签配置文件分析库和打印接口的时候应该有哪些重要地方呢
(一) 正则表达式
有四个表达式分别为"^%/w*", "/^WINFONT/S*/)", "/^FD.*FS$", "@",通过对价签配置文件每行的分析来获取相应的数据比如有这么一行数据
%产地 ^WINFONT(黑体,40,100,200,Y,1,2)^FD@^FS
通过正则分析后会得到如下数据“%产地”,“^WINFONT(黑体,40,100,200,Y,1,2)”,“^FD@^FS”,“@”,对“%产地”的值去调“%”后就是数据表中名为“产地”的列。
将“^WINFONT(黑体,40,100,200,Y,1,2)”拆份后会得到“黑体”,“40”,“100”,“200”,“Y”,“1”,“2”。将这些变量赋给上面的汉字转换类中的属性“FontName”,“FontSize”,“X”,“Y”,“FontBold”,“Xzoom”,“Yzoom”。
将“@”替换成“产地”中的数据然后赋给上面的汉字转换类中的属性“Text”
通过上面的操作后就将这些数据转换成ZPL指令流了,比如我们的数据库中有个字段为“产地”,有一数据值为“四川1”,价签配置文件内容为“^XA %产地^WINFONT(黑体,40,100,200,Y,1,2)^FD@^FS ^XZ ”那么通过分析转换后就会得到这样的结果^XA~DGOUTSTR01,690,15,nJ07CI01EgI078I01ER0MFEI0781F01ER0MFEI0781F01EI0FCM0MFEI0781F01EH03HFM0F03C781EI0780F01EH07HF8L0F03C781EI0780F01EH0F87CL0F03C781EI0780F01EH0F03CL0F03C781EI0780F01E01F01EL0F038781EI0780F01E01E01EL0F038781EI0780F01E01E01EL0F038781EI0780F01E03CH0FL0F078781EI0780F01E03CH0FL0F078781EI0780F01E03CH0FL0F078781EI0780F01E03CH0FL0F078781EI0F80F01E03CH0FL0F0F8781EI0F80F01E03CH0FL0F0F0781EI0FH0F01E03CH0FL0F1F0781EI0FH0F01E03CH0FL0F1E07HDEI0FH0F01E03CH0FL0F3E07FDEI0FH0F01E03CH0FL0F7C07FDEI0FH0F01E03CH0FL0HF8H07DEH01FH0F01E01E01EL0F38I01EH01EH0F01E01E01EL0FK01EH01EH0F01E01F01EL0FK01EH01EH0F01EH0F03CL0FK01EH03CH0F01EH0F87CL0MFEH03CH0F01EH07HF8L0MFEH07C01F01EH03HFM0MFEH07801F01EI0FCM0FK01EH0F801F01ER0FK01E01FK01ER0FK01EH0FK01EgN01EpY0
^FO100,200^XGOUTSTR01,1,2,^FS^XZ
(二) 如何打印
指令流生成了如何发送呢,我采取的方式是通过CreateFile这个API函数直接发送数据到打印端口。注意为了提高效率,我们会采用边生成指令流边打印的方式操作,这样会提高使用效率。对于打印连续打印价签会节约不少的时间。
原代码如下:
- Imports System.Text.RegularExpressions
- Imports System.Windows.Forms
- Imports System.Text
- Imports System.Runtime.InteropServices
- Imports System.IO
- Imports Microsoft.Win32.SafeHandles
- Public Class FormatText
- Private List As String() = New String(3) {"^%/w*", "/^WINFONT/S*/)", "/^FD.*FS$", "@"}
- Private m_ColnameString As String
- Private m_IntputString As String
- Private m_FileName As String
- Private m_Device As String
- Private TempArrary(3) As String
- Private m_Table As DataTable
- Public Event ValueChaged(ByVal Precent As Integer)
- "kernel32.dll", SetLastError:=True)> _
- Private Shared Function CreateFile(ByVal lpFileName As String, ByVal dwDesiredAccess As System.UInt32, ByVal dwShareMode As System.UInt32, ByVal lpSecurityAttributes As IntPtr, ByVal dwCreationDisposition As System.UInt32, ByVal dwFlagsAndAttributes As System.UInt32, ByVal hTemplateFile As IntPtr) As SafeFileHandle
- End Function
- Private Const GENERIC_WRITE =
- Private Const CREATE_ALWAYS = 2
- Private Const FILE_SHARE_WRITE =
- Private Const OPEN_EXISTING = 3
- Public Property Device() As String
- Get
- Return m_Device
- End Get
- Set(ByVal value As String)
- m_Device = value
- End Set
- End Property
- Public Sub New()
- MyBase.new()
- End Sub
- Public Sub New(ByVal FileNameValue As String)
- m_FileName = FileNameValue
- End Sub
- Public Property Table() As DataTable
- Get
- Return m_Table
- End Get
- Set(ByVal value As DataTable)
- m_Table = value
- End Set
- End Property
- Public Property FileName() As String
- Get
- Return m_FileName
- End Get
- Set(ByVal value As String)
- m_FileName = value
- End Set
- End Property
- Private Sub InitList()
- For i As Integer = 0 To List.Length - 1
- TempArrary(i) = Regex.Match(m_IntputString, List(i)).Value
- Next
- If TempArrary(0) <> String.Empty Then
- m_ColnameString = Trim(TempArrary(0).Substring(1))
- End If
- End Sub
- Private Function RePlace(ByVal RePlaceString As String) As String
- Dim ZPL As New ZPL.FontConvertBmp, DesString() As String
- If TempArrary(3) = "@" AndAlso TempArrary(0) <> "" AndAlso TempArrary(1) = "" Then
- Return m_IntputString.Substring(TempArrary(0).Length).Replace("@", RePlaceString)
- ElseIf TempArrary(3) = "@" AndAlso TempArrary(0) <> "" AndAlso TempArrary(1) <> "" Then
- ZPL.Text = RePlaceString
- ElseIf TempArrary(2) <> "" AndAlso TempArrary(1) <> "" Then
- ZPL.Text = Trim(TempArrary(2).Substring(3, TempArrary(2).Length - 6))
- ElseIf TempArrary(0) = "" AndAlso TempArrary(1) = "" AndAlso TempArrary(2) = "" AndAlso TempArrary(3) = "" Then
- Return m_IntputString
- ElseIf TempArrary(2) <> "" AndAlso TempArrary(1) = "" Then
- Return m_IntputString
- End If
- If TempArrary(1) <> String.Empty Then
- DesString = TempArrary(1).Substring(9, TempArrary(1).Length - 10).Split(",")
- With ZPL
- .FontName = DesString(0)
- .FontSize = DesString(1)
- Integer.TryParse(DesString(2), .X)
- Integer.TryParse(DesString(3), .Y)
- If DesString(4) = "Y" Then
- .FontBold = True
- ElseIf DesString(4) = "N" Then
- .FontBold = False
- End If
- Integer.TryParse(DesString(5), .XZoom)
- Integer.TryParse(DesString(6), .YZoom)
- End With
- End If
- Return ZPL.Convert
- End Function
- Public Sub OutPut()
- Dim Count As Integer = m_Table.Rows.Count
- Dim SR As IO.StreamReader, Str As String
- Dim Stream As FileStream, EnCoding As New System.Text.ASCIIEncoding
- Dim Hdc As SafeHandle = Nothing
- Hdc = CreateFile(m_Device, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, 0)
- If Not Hdc.IsInvalid Then
- Stream = New FileStream(Hdc, FileAccess.Write)
- If Not IO.File.Exists(m_FileName) Then
- MessageBox.Show(m_FileName & "文件不存在", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
- Exit Sub
- End If
- Dim i As Integer
- While i < Count
- SR = New IO.StreamReader(m_FileName, System.Text.Encoding.Default)
- Try
- While Not SR.EndOfStream
- m_IntputString = SR.ReadLine
- InitList()
- If m_ColnameString <> "" Then
- Str = RePlace(Table.Rows(i)(m_ColnameString))
- Else
- Str = RePlace(String.Empty)
- End If
- Stream.Write(EnCoding.GetBytes(Str), 0, EnCoding.GetBytes(Str).Length)
- End While
- Catch ex As Exception
- MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
- Hdc.Close()
- Exit Sub
- Finally
- SR.Close()
- End Try
- RaiseEvent ValueChaged(CInt((i + 1) / (Count) * 100))
- i += 1
- End While
- Stream.Close()
- Else
- MessageBox.Show("设备不存在或该设备没有打开", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
- End If
- Hdc.Close()
- Hdc.Dispose()
- End Sub
- End Class
至此,程序设计和分析就介绍完了,以上的代码均在VB6或VS2005中进行过测试。