PB中利用字体实现EAN13条形码的打印

  在改造图书管理系统中,碰到一个新需求,图书Isbn的条码打印,需要用到EAN13 条形码,上网搜了一下资料,大多都是需要控件的方式来实现,感觉不是特别爽。研究了一下EAN13的背景资料,编码方式,规则,采用ean13.ttf字体轻松实现了EAN13条码打印。

  我自写了两个函数,一个计算EAN13的检验位函数clefean,一个是ISBN的转换函数。分别如下:

 

public function integer clefean (string code);

/*检验码计算的步聚如下:
  以2342355654652为例:
  1、从序号2开始,将所有偶数位的数字代码求和,得出S1
  S1 = 3 + 2 + 5 + 5 + 6 + 2 = 23
  2、从序号3开始,将所有奇数位的数字求和,得出S2
  S2 = 2 + 4 + 3 + 6 + 4 + 5 = 24
  3、S3 = S1 * 3 + S2
  S3 = 23*3 + 24 = 93
  4、C = 10 -(S3的个位数),得到校验码C的值,并且当S3的个位数为0时,C=0。
  C=10 - 3 = 7
*/

string tabcode[13]
integer ope1
integer ope2
integer ope3
integer ope4
tabcode[1] = mid(code,1,1)
tabcode[2] = mid(code,2,1)
tabcode[3] = mid(code,3,1)
tabcode[4] = mid(code,4,1)
tabcode[5] = mid(code,5,1)
tabcode[6] = mid(code,6,1)
tabcode[7] = mid(code,7,1)
tabcode[8] = mid(code,8,1)
tabcode[9] = mid(code,9,1)
tabcode[10] = mid(code,10,1)
tabcode[11] = mid(code,11,1)
tabcode[12] = mid(code,12,1)

ope1 = integer(tabcode[1]) + integer(tabcode[3]) + integer(tabcode[5])
ope1 = ope1 + integer(tabcode[7]) + integer(tabcode[9]) + integer(tabcode[11])

ope2 = integer(tabcode[2]) + integer(tabcode[4]) + integer(tabcode[6])
ope2 = ope2 + integer(tabcode[8]) + integer(tabcode[10]) + integer(tabcode[12])

ope2 = ope2 * 3

ope3 =ope2 + ope1
ope4 = integer(right(string(ope3),1))
if ope4 = 0 then
 return 0
else
 return 10 - ope4
end if

end function

 

 

public function string ean13ekim (string s);

string   chaine, codebarre
long     checksum
integer  li_first,i
boolean  tableA
string   ls_result
ls_result = ''
checksum = 0
if len(s) = 13 then
 i = 0
 do
  i = i + 1
  if (Asc(Mid(s,i,1)) < 48) or (Asc(Mid(s,i,1)) > 57) then
   i = 0
   messagebox('提示','转换EAN13码时发现有非数字字符,无法正常显示。')
   return ''
  end if
 loop until (i=13)

   checksum = clefean(s)
   s = s + string(checksum) //算出检验和
   codebarre = Left(s,1) + Char( 65 + long(Mid(s,2,1)) )

   li_first = long( Left(s,1) )
   For i = 3 to 7
    tableA = FALSE
      if i=3 then
   if li_first=0 or li_first=1 or li_first=2 or li_first=3 then tableA = TRUE
  end if
    if i=4 then
   if li_first=0 or li_first=4 or li_first=7 or li_first=8 then tableA = TRUE
  end if
      if i=5 then
   if li_first=0 or li_first=1 or li_first=4 or li_first=5 or li_first=9 then tableA = TRUE
  end if
    if i=6 then
   if li_first=0 or li_first=2 or li_first=5 or li_first=6 or li_first=7 then tableA = TRUE   
  end if
    if i=7 then
   if li_first=0 or li_first=3 or li_first=6 or li_first=8 or li_first=9 then tableA = TRUE
  end if

  if tableA then
   codebarre = codebarre + char( 65 + long(Mid(s,i,1)) )
  else
         codebarre = codebarre + char( 75 + long(Mid(s,i,1)) )
  end if
 next
   codebarre = codebarre + '*'
   for i = 8 to 13
    codebarre = codebarre + char(97 + long(Mid(s,i,1)) );
      codebarre = codebarre + '+'
      ls_result = codebarre
   next
else
 messagebox('提示','转换EAN13码时传入参数不是13位,无法正常显示条码。')
 return ''
end if
return ls_result
 

end function

 

 

可以到我的资源下载本示例:http://download.csdn.net/source/1803052

你可能感兴趣的:(c,function,String,Integer)