Código Fonte - O melhor conteúdo para programadores

ASP / Imagens

Gerador de Código de Barras


enviado por Gabriel Fróes
Este código possui uma rotina completa para a geração Código de Barra.

Com este código é possível gerar código de barra para diversos padrões como EAN-13 e EAN-8.

É necessário baixar os arquivos anexados.
<%
'-----------------------------------------------------
'Funcao: getCodigoBarras(ByVal Numeros)
'Sinopse: Rotina para gerar códigos de barra padrão 2of5 ou 25.
'Parametro:
'       Numeros: Números para a geração do código de barras
'Retorno: String (HTML com o código gerado)
'-----------------------------------------------------
Function getCodigoBarras(ByVal Numeros)
   Dim F, F1, F2, i, Texto
   Dim arrCodigoBarra(99)
   Dim htmlCodigoBarra
   htmlCodigoBarra = ""
   Const Fino      = 1
   Const Largo      = 3
   Const Altura   = 50
   
   if isempty(arrCodigoBarra(0)) Then
      arrCodigoBarra(0) = "00110"
      arrCodigoBarra(1) = "10001"
      arrCodigoBarra(2) = "01001"
      arrCodigoBarra(3) = "11000"
      arrCodigoBarra(4) = "00101"
      arrCodigoBarra(5) = "10100"
      arrCodigoBarra(6) = "01100"
      arrCodigoBarra(7) = "00011"
      arrCodigoBarra(8) = "10010"
      arrCodigoBarra(9) = "01010"
      For F1 = 9 To 0 Step -1
         For F2 = 9 To 0 Step -1
            F = F1 * 10 + F2
            Texto = ""
            For i = 1 To 5
               Texto = Texto & Mid(arrCodigoBarra(F1), i, 1) + Mid(arrCodigoBarra(F2), i, 1)
            Next
            arrCodigoBarra(f) = Texto
         Next
      Next
   End if

   'Construindo o código HTML do código de barras
   'Guarda inicial
   htmlCodigoBarra = htmlCodigoBarra & "<img src=p.jpg width=" & Fino & " height=" & Altura & " border=0>"
   htmlCodigoBarra = htmlCodigoBarra & "<img src=b.jpg width=" & Fino & " height=" & Altura & " border=0>"
   htmlCodigoBarra = htmlCodigoBarra & "<img src=p.jpg width=" & Fino & " height=" & Altura & " border=0>"
   htmlCodigoBarra = htmlCodigoBarra & "<img src=b.jpg width=" & Fino & " height=" & Altura & " border=0>"
   htmlCodigoBarra = htmlCodigoBarra & "<img"
'Response.Write htmlCodigoBarra
   Texto = Numeros
   if Len(Texto) Mod 2 <> 0 Then Texto = "0" & Texto End if
   'HTML dos dados
   Do While Len(Texto) > 0
      i      = Cint(Left(Texto,2))
      Texto   = Right(Texto, Len(Texto)- 2)
      F      = arrCodigoBarra(i)
      For i = 1 To 10 Step 2
         If Mid(F, i, 1) = "0" Then
            F1 = Fino
         Else
            F1 = Largo
         End If
         
         htmlCodigoBarra = htmlCodigoBarra & " src=p.jpg width=" & F1 & " height=" & Altura & " border=0><img"
         
         If mid(F, i + 1, 1) = "0" Then
            F2 = Fino
         Else
            F2 = Largo
         End If

         htmlCodigoBarra = htmlCodigoBarra & " src=b.jpg width=" & F2 & " height=" & Altura & " border=0><img"
   
      Next
   Loop
   
   'Guarda final
   htmlCodigoBarra = htmlCodigoBarra & " src=p.jpg width=" & Largo & " height=" & Altura & " border=0>"
   htmlCodigoBarra = htmlCodigoBarra & "<img src=b.jpg width=" & Fino & " height=" & Altura & " border=0>"
   htmlCodigoBarra = htmlCodigoBarra & "<img src=p.jpg width=1 height=" & Altura & " border=0>"
   
   'Retornando a função
   getCodigoBarras   = htmlCodigoBarra
End Function

'Exemplo de geração do código de barras
'Substitua o valor do parâmetro abaixo pelo número do código de barras.
Response.Write getCodigoBarras("7890552007601")
%>


Versão impressa gerada em: quarta-feira, 16 de maio de 2012

© Copyright 2006 - R&W Consulting.