我想从我的应用程序中创建Code39编码的条形码。
我知道我可以使用字体,但我不想这样做,因为我必须在服务器上注册字体,而且我有一些非常糟糕的经历。
在提出这个问题之后我提出的一个例子是答案
答案 0 :(得分:11)
这是我目前的代码隐藏,有很多评论:
Option Explicit On
Option Strict On
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Bitmap
Imports System.Drawing.Graphics
Imports System.IO
Partial Public Class Barcode
Inherits System.Web.UI.Page
'Sebastiaan Janssen - 20081001 - TINT-30584
'Most of the code is based on this example:
'http://www.atalasoft.com/cs/blogs/loufranco/archive/2008/04/25/writing-code-39-barcodes-with-javascript.aspx-generation.aspx
'With a bit of this thrown in:
'http://www.atalasoft.com/cs/blogs/loufranco/archive/2008/03/24/code-39-barcode
Private _encoding As Hashtable = New Hashtable
Private Const _wideBarWidth As Short = 8
Private Const _narrowBarWidth As Short = 2
Private Const _barHeight As Short = 100
Sub BarcodeCode39()
_encoding.Add("*", "bWbwBwBwb")
_encoding.Add("-", "bWbwbwBwB")
_encoding.Add("$", "bWbWbWbwb")
_encoding.Add("%", "bwbWbWbWb")
_encoding.Add(" ", "bWBwbwBwb")
_encoding.Add(".", "BWbwbwBwb")
_encoding.Add("/", "bWbWbwbWb")
_encoding.Add("+", "bWbwbWbWb")
_encoding.Add("0", "bwbWBwBwb")
_encoding.Add("1", "BwbWbwbwB")
_encoding.Add("2", "bwBWbwbwB")
_encoding.Add("3", "BwBWbwbwb")
_encoding.Add("4", "bwbWBwbwB")
_encoding.Add("5", "BwbWBwbwb")
_encoding.Add("6", "bwBWBwbwb")
_encoding.Add("7", "bwbWbwBwB")
_encoding.Add("8", "BwbWbwBwb")
_encoding.Add("9", "bwBWbwBwb")
_encoding.Add("A", "BwbwbWbwB")
_encoding.Add("B", "bwBwbWbwB")
_encoding.Add("C", "BwBwbWbwb")
_encoding.Add("D", "bwbwBWbwB")
_encoding.Add("E", "BwbwBWbwb")
_encoding.Add("F", "bwBwBWbwb")
_encoding.Add("G", "bwbwbWBwB")
_encoding.Add("H", "BwbwbWBwb")
_encoding.Add("I", "bwBwbWBwb")
_encoding.Add("J", "bwbwBWBwb")
_encoding.Add("K", "BwbwbwbWB")
_encoding.Add("L", "bwBwbwbWB")
_encoding.Add("M", "BwBwbwbWb")
_encoding.Add("N", "bwbwBwbWB")
_encoding.Add("O", "BwbwBwbWb")
_encoding.Add("P", "bwBwBwbWb")
_encoding.Add("Q", "bwbwbwBWB")
_encoding.Add("R", "BwbwbwBWb")
_encoding.Add("S", "bwBwbwBWb")
_encoding.Add("T", "bwbwBwBWb")
_encoding.Add("U", "BWbwbwbwB")
_encoding.Add("V", "bWBwbwbwB")
_encoding.Add("W", "BWBwbwbwb")
_encoding.Add("X", "bWbwBwbwB")
_encoding.Add("Y", "BWbwBwbwb")
_encoding.Add("Z", "bWBwBwbwb")
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
BarcodeCode39()
Dim barcode As String = String.Empty
If Not IsNothing(Request("barcode")) AndAlso Not (Request("barcode").Length = 0) Then
barcode = Request("barcode")
Response.ContentType = "image/png"
Response.AddHeader("Content-Disposition", String.Format("attachment; filename=barcode_{0}.png", barcode))
'TODO: Depending on the length of the string, determine how wide the image will be
GenerateBarcodeImage(250, 140, barcode).WriteTo(Response.OutputStream)
End If
End Sub
Protected Function getBCSymbolColor(ByVal symbol As String) As System.Drawing.Brush
getBCSymbolColor = Brushes.Black
If symbol = "W" Or symbol = "w" Then
getBCSymbolColor = Brushes.White
End If
End Function
Protected Function getBCSymbolWidth(ByVal symbol As String) As Short
getBCSymbolWidth = _narrowBarWidth
If symbol = "B" Or symbol = "W" Then
getBCSymbolWidth = _wideBarWidth
End If
End Function
Protected Overridable Function GenerateBarcodeImage(ByVal imageWidth As Short, ByVal imageHeight As Short, ByVal Code As String) As MemoryStream
'create a new bitmap
Dim b As New Bitmap(imageWidth, imageHeight, Imaging.PixelFormat.Format32bppArgb)
'create a canvas to paint on
Dim canvas As New Rectangle(0, 0, imageWidth, imageHeight)
'draw a white background
Dim g As Graphics = Graphics.FromImage(b)
g.FillRectangle(Brushes.White, 0, 0, imageWidth, imageHeight)
'write the unaltered code at the bottom
'TODO: truely center this text
Dim textBrush As New SolidBrush(Color.Black)
g.DrawString(Code, New Font("Courier New", 12), textBrush, 100, 110)
'Code has to be surrounded by asterisks to make it a valid Code39 barcode
Dim UseCode As String = String.Format("{0}{1}{0}", "*", Code)
'Start drawing at 10, 10
Dim XPosition As Short = 10
Dim YPosition As Short = 10
Dim invalidCharacter As Boolean = False
Dim CurrentSymbol As String = String.Empty
For j As Short = 0 To CShort(UseCode.Length - 1)
CurrentSymbol = UseCode.Substring(j, 1)
'check if symbol can be used
If Not IsNothing(_encoding(CurrentSymbol)) Then
Dim EncodedSymbol As String = _encoding(CurrentSymbol).ToString
For i As Short = 0 To CShort(EncodedSymbol.Length - 1)
Dim CurrentCode As String = EncodedSymbol.Substring(i, 1)
g.FillRectangle(getBCSymbolColor(CurrentCode), XPosition, YPosition, getBCSymbolWidth(CurrentCode), _barHeight)
XPosition = XPosition + getBCSymbolWidth(CurrentCode)
Next
'After each written full symbol we need a whitespace (narrow width)
g.FillRectangle(getBCSymbolColor("w"), XPosition, YPosition, getBCSymbolWidth("w"), _barHeight)
XPosition = XPosition + getBCSymbolWidth("w")
Else
invalidCharacter = True
End If
Next
'errorhandling when an invalidcharacter is found
If invalidCharacter Then
g.FillRectangle(Brushes.White, 0, 0, imageWidth, imageHeight)
g.DrawString("Invalid characters found,", New Font("Courier New", 8), textBrush, 0, 0)
g.DrawString("no barcode generated", New Font("Courier New", 8), textBrush, 0, 10)
g.DrawString("Input was: ", New Font("Courier New", 8), textBrush, 0, 30)
g.DrawString(Code, New Font("Courier New", 8), textBrush, 0, 40)
End If
'write the image into a memorystream
Dim ms As New MemoryStream
Dim encodingParams As New EncoderParameters
encodingParams.Param(0) = New EncoderParameter(Encoder.Quality, 100)
Dim encodingInfo As ImageCodecInfo = FindCodecInfo("PNG")
b.Save(ms, encodingInfo, encodingParams)
'dispose of the object we won't need any more
g.Dispose()
b.Dispose()
Return ms
End Function
Protected Overridable Function FindCodecInfo(ByVal codec As String) As ImageCodecInfo
Dim encoders As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders
For Each e As ImageCodecInfo In encoders
If e.FormatDescription.Equals(codec) Then Return e
Next
Return Nothing
End Function
End Class
答案 1 :(得分:6)
如果您选择Code39,您可能可以从我编写的代码中编码
http://www.atalasoft.com/cs/blogs/loufranco/archive/2008/03/24/code-39-barcode-generation.aspx
我写它是为了使用我们的工具包来生成图像,但你可以很容易地重写它以使用.NET图像/图形。
答案 2 :(得分:6)
答案 3 :(得分:3)
这是一个用于.NET语言的开源条形码渲染库:http://www.codeplex.com/BarcodeRender
它可以渲染一些常用的编码。
许可证看起来很温和,似乎可以在开源和商业应用程序中使用(但是,IANAL,您可能想要自己查看license。)
这是另一个,也是开源的,使用Apache 2.0许可证:http://sourceforge.net/projects/onecode/
通常,当您从一开始就知道您正在寻找开源组件时,最好绕过Google并直接开始搜索SourceForge(它有一个很棒的搜索结果过滤系统,您可以过滤语言,这可能是您感兴趣的)或微软的CodePlex(选择通常更有限,但你去。)
答案 4 :(得分:1)
您要打印到标准打印机还是实际的条形码打印机(zebra或datamax)? zebra和datamax都有自己的脚本语言 - 实际上更像是标记语言。分别是ZPL和DPL。我更喜欢斑马,他们的文档更清晰。
如果您没有合法的条形码打印机,我建议您购买一台并执行以下操作....(它会比尝试构建小图像块并将其绘制成模拟条形码字体更清晰)
两者都具有很大的灵活性,您可以让打印机处理创建实际的条形码图像。
让您的程序发送一个自定义的ZPL / DPL脚本,其中包含您希望通过ftp将条形码作为条形码打印到打印机的值。基本上,你只需将包含该脚本的文本文件“放入”打印机的IP,打印机就会处理该字体。
答案 5 :(得分:1)
iTextSharp库虽然表面上是用于创建创建PDF,但也有一个包含Code39的条形码生成库。
添加对DLL的引用后,就像:
一样简单Barcode39 code39 = new Barcode39();
code39.Code = "Whatever You're Encoding";
哎呀,那是C#,但你明白了。创建后,您可以使用几乎任何图像格式渲染图像,并根据需要使用它。
答案 6 :(得分:0)
如果渲染客户端,则字体可以驻留在工作站上。这样你就可以使用3-of-9。我在几个项目中使用了9-of-9,这是最简单的解决方案。
答案 7 :(得分:0)
以下是how to generate Code39 barcodes in vb.net的示例。我现在测试它并且它可以工作。
Public Class code39
Private bitsCode As ArrayList
Public Sub New()
bitsCode = New ArrayList
bitsCode.Add(New String(3) {"0001101", "0100111", "1110010", "000000"})
bitsCode.Add(New String(3) {"0011001", "0110011", "1100110", "001011"})
bitsCode.Add(New String(3) {"0010011", "0011011", "1101100", "001101"})
bitsCode.Add(New String(3) {"0111101", "0100001", "1000010", "001110"})
bitsCode.Add(New String(3) {"0100011", "0011101", "1011100", "010011"})
bitsCode.Add(New String(3) {"0110001", "0111001", "1001110", "011001"})
bitsCode.Add(New String(3) {"0101111", "0000101", "1010000", "011100"})
bitsCode.Add(New String(3) {"0111011", "0010001", "1000100", "010101"})
bitsCode.Add(New String(3) {"0110111", "0001001", "1001000", "010110"})
bitsCode.Add(New String(3) {"0001011", "0010111", "1110100", "011010"})
End Sub
Public Function Generate(ByVal Code As String) As Image
Dim a As Integer = 0
Dim b As Integer = 0
Dim imgCode As Image
Dim g As Graphics
Dim i As Integer
Dim bCode As Byte()
Dim bitCode As Byte()
Dim tmpFont As Font
If Code.Length <> 12 Or Not IsNumeric(Code.Replace(".", "_").Replace(",", "_")) Then Throw New Exception("Le code doit être composé de 12 chiffres")
ReDim bCode(12)
For i = 0 To 11
bCode(i) = CInt(Code.Substring(i, 1))
If (i Mod 2) = 1 Then
b += bCode(i)
Else
a += bCode(i)
End If
Next
i = (a + (b * 3)) Mod 10
If i = 0 Then
bCode(12) = 0
Else
bCode(12) = 10 - i
End If
bitCode = getBits(bCode)
tmpFont = New Font("times new roman", 14, FontStyle.Regular, GraphicsUnit.Pixel)
imgCode = New Bitmap(110, 50)
g = Graphics.FromImage(imgCode)
g.Clear(Color.White)
g.DrawString(Code.Substring(0, 1), tmpFont, Brushes.Black, 2, 30)
a = g.MeasureString(Code.Substring(0, 1), tmpFont).Width
For i = 0 To bitCode.Length - 1
If i = 2 Then
g.DrawString(Code.Substring(1, 6), tmpFont, Brushes.Black, a, 30)
ElseIf i = 48 Then
g.DrawString(Code.Substring(7, 5) & bCode(12).ToString, tmpFont, Brushes.Black, a, 30)
End If
If i = 0 Or i = 2 Or i = 46 Or i = 48 Or i = 92 Or i = 94 Then
If bitCode(i) = 1 Then 'noir
g.DrawLine(Pens.Black, a, 0, a, 40)
a += 1
End If
Else
If bitCode(i) = 1 Then 'noir
g.DrawLine(Pens.Black, a, 0, a, 30)
a += 1
Else 'blanc
a += 1
End If
End If
Next
g.Flush()
Return imgCode
End Function
Private Function getBits(ByVal bCode As Byte()) As Byte()
Dim i As Integer
Dim res As Byte()
Dim bits As String = "101"
Dim cle As String = bitsCode(bCode(0))(3)
For i = 1 To 6
bits &= bitsCode(bCode(i))(CInt(cle.Substring(i - 1, 1)))
Next
bits &= "01010"
For i = 7 To 12
bits &= bitsCode(bCode(i))(2)
Next
bits += "101"
ReDim res(bits.Length - 1)
For i = 0 To bits.Length - 1
res(i) = Asc(bits.Chars(i)) - 48
Next
Return res
End Function
End Class
答案 8 :(得分:0)
我不想使用条形码字体,而是选择.net barcode generator component。以下是vb.net sample for creating Code 39 barcode。
Imports System.IO
Imports PQScan.BarcodeCreator
Namespace BarcodeGeneratorVB
Class Program
Private Shared Sub Main(args As String())
Dim barcode As New Barcode()
barcode.Data = "www.pqscan.com"
barcode.BarType = BarCodeType.Code39
barcode.Width = 300
barcode.Height = 100
barcode.CreateBarcode("code39-vb.jpeg")
End Sub
End Class
End Namespace
答案 9 :(得分:-1)
在我上一份工作中,我与vb.net中的几个不同的图书馆合作。我们有一个,并转移到另一个。我不记得他们的名字(如果我看到他们的话,我会再次认出他们),但我知道两个都是付费的,我们在转换时评估了几个不同的组件,我认为这包括一个免费的。我们是非常的小商店,非常成本敏感,所以如果免费组件有任何好处,你可以打赌我们会使用它(我认为我们需要128b支持,它只处理代码39)。
我还记得我们改变的原因是我们同时从.Net 1.1迁移到.Net 2.0,而第一个组件进行转换的速度太慢了。
总而言之,那里有一些东西,但3年前它没有任何好处。希望其他人可以来并填写一些实际的名字。