這個要看掃描槍的通訊接口和通訊協(xié)議,以及接口程序是否支持VB.NET等。
網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、微信小程序、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了赫山免費建站歡迎大家使用!
工具箱,然后隨便選擇一項,右鍵 選擇項,在.com控件里,microsoft bar ctrol 9.0
93條碼和CODE39一樣,起始符和結(jié)束符都是*號,你做的條碼掃描不出來的原因,估計和校驗碼有關(guān)。93條碼規(guī)定在數(shù)據(jù)的最后有兩個校驗位C和K,如果你的數(shù)據(jù)里沒有生成這兩個校驗位,那肯定是無法掃描的。
在本文中,我們將使用ASP.NET web services創(chuàng)建條形碼。我們還將創(chuàng)建含有條形碼的Windows Forms和Console應(yīng)用程序。該過程會用到Aspose.BarCode這個控件。
這樣做有什么好處呢?
Web services的主要優(yōu)勢在于軟件與外部應(yīng)用程序集成。標(biāo)準(zhǔn)化的請求/響應(yīng)模型,任何基于XML web service的客戶端應(yīng)用程序都可以從中受益。以下是簡短的條形碼服務(wù)的表現(xiàn)形式??蛻舳瞬恍枰诖税惭bAspose.BarCode for .NET。他們只需發(fā)送兩個字符串值(代碼電文和符號),就將從服務(wù)端獲取條形碼(字節(jié)數(shù)組)。
打開Microsoft Visual Studio,并創(chuàng)建一個“ASP.NET Web Service Application”新項目,命名為“BarCodeService”。 添加以下引用。
1.“Add Reference”對話框的System.Drawing from .NET選項卡
2. Aspose.BarCode。
找到 Aspose.BarCode for .NET安裝的位置并選擇。Visual Studio會添加了一個默認(rèn)的類“Service1“到Service1.asmx文檔的Web Service項目。 打開它,并為這個類添加以下方法。
[C#]
[WebMethod]
public byte[] GetBarcode(string strCodetext, string strSymbology)
{
// Initialize BarCodeBuilder
BarCodeBuilder builder = new BarCodeBuilder();
// Set codetext
builder.CodeText = strCodetext;
// Set barcode symbology
builder.SymbologyType = (Symbology) Enum.Parse(typeof(Symbology), strSymbology, true);
// Create and save the barcode image to memory stream
MemoryStream imgStream = new MemoryStream();
builder.Save(imgStream, ImageFormat.Png);
// Return the barcode image as a byte array
return imgStream.ToArray();
}
[VB.NET]
_
Public Function GetBarcode(ByVal strCodetext As String, ByVal strSymbology As String) As Byte()
' Initialize BarCodeBuilder
Dim builder As BarCodeBuilder = New BarCodeBuilder()
' Set codetext
builder.CodeText = strCodetext
' Set barcode symbology
builder.SymbologyType = CType(System.Enum.Parse(GetType(Symbology), strSymbology, True), Symbology)
' Create and save the barcode image to memory stream
Dim imgStream As MemoryStream = New MemoryStream()
builder.Save(imgStream, ImageFormat.Png)
' Return the barcode image as a byte array
Return imgStream.ToArray()
End Function
web方法需要客戶端以下兩個參數(shù):
1.Codetext
2.Symbology
這些參數(shù)為String字符串類型。這些參數(shù)被傳遞到BarCodeBuilder類,然后創(chuàng)建條形碼,并以字節(jié)數(shù)組的形式給客戶端發(fā)送條形碼。
使用Windows Forms應(yīng)用中的Web Service
打開Visual Studio,并創(chuàng)建一個新類型“Windows Application”的項目。命名項目為“GetBarCodeWinForms”。通過右鍵單擊“References”,選擇,然后從菜單中選擇““Add Service Reference”為web service添加引用。鍵入web service的地址。在得到正確的結(jié)果之后,在Namespace命名域中輸入“BarCodeService”,點擊“Ok”按鈕以添加引用。
設(shè)計形式如下圖所示:
它包含以下控件:
1.Textbox:輸入代碼
2.Combobox:輸入符號類型
3.Button:調(diào)用web service
4.Picturebox:顯示條形碼
為代碼的按鈕單擊事件添加以下代碼。
[C#]
// Initialize the Barcode Web Service
BarCodeService.Service1SoapClient barcodeService = new BarCodeService.Service1SoapClient();
// Call the GetBarcode web method
// Pass codetext and symbology in parameters
// Get the barcode image returned from the web method in the form of byte array
byte[] arrBarcodeImage = barcodeService.GetBarcode(txtCodetext.Text, cmbSymbology.Text);
// Create an instance of Image from the byte array
MemoryStream imgStream = new MemoryStream(arrBarcodeImage);
Image imgBarcode = Bitmap.FromStream(imgStream);
// Assign the barcode image to the Picturebox control
picBarcodeImage.Image = imgBarcode;
picBarcodeImage.Height = imgBarcode.Height;
picBarcodeImage.Width = imgBarcode.Width;
[VB.NET]
' Initialize the Barcode Web Service
Dim barcodeService As BarCodeService.Service1SoapClient = New BarCodeService.Service1SoapClient()
' Call the GetBarcode web method
' Pass codetext and symbology in parameters
' Get the barcode image returned from the web method in the form of byte array
Dim arrBarcodeImage As Byte() = barcodeService.GetBarcode(txtCodetext.Text, cmbSymbology.Text)
' Create an instance of Image from the byte array
Dim imgStream As MemoryStream = New MemoryStream(arrBarcodeImage)
Dim imgBarcode As Image = Bitmap.FromStream(imgStream)
' Assign the barcode image to the Picturebox control
picBarcodeImage.Image = imgBarcode
picBarcodeImage.Height = imgBarcode.Height
picBarcodeImage.Width = imgBarcode.Width
運行該應(yīng)用程序,指定某些值,點擊“Get Barcode”按鈕。應(yīng)用程序?qū)⑹褂脳l形碼web service,并從中獲取條形碼。條形碼將顯示在如下窗體中。
從Console Application控制臺應(yīng)用程序使用Web Service
在Visual Studio中創(chuàng)建一個“Console Application”新項目,將項目命名為“GetBarCodeConsole”。 將該引用添加到條碼服務(wù)中,方法和winforms應(yīng)用程序中的相同。在main()方法中編寫以下代碼。
[C#]
try
{
// Initialize the Barcode Web Service
BarCodeService.Service1SoapClient c = new GetBarCodeConsole.BarCodeService.Service1SoapClient();
// Call the GetBarcode web method
// Pass codetext and symbology in parameters
// Get the barcode image returned from the web method in the form of byte array
byte[] arrBarcodeImage = c.GetBarcode("console application", "pdf417");
// Save the byte array (barcode image) to disk
FileStream imgWriter = new FileStream("barcode.png", FileMode.Create);
imgWriter.Write(arrBarcodeImage, 0, arrBarcodeImage.Length);
imgWriter.Close();
// Open the barcode image
Process.Start("barcode.png");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("Press any key to exit....");
Console.ReadKey();
[VB.NET]
Try
' Initialize the Barcode Web Service
Dim c As BarCodeService.Service1SoapClient = New GetBarCodeConsole.BarCodeService.Service1SoapClient()
' Call the GetBarcode web method
' Pass codetext and symbology in parameters
' Get the barcode image returned from the web method in the form of byte array
Dim arrBarcodeImage As Byte() = c.GetBarcode("console application", "pdf417")
' Save the byte array (barcode image) to disk
Dim imgWriter As FileStream = New FileStream("barcode.png", FileMode.Create)
imgWriter.Write(arrBarcodeImage, 0, arrBarcodeImage.Length)
imgWriter.Close()
' Open the barcode image
Process.Start("barcode.png")
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Console.WriteLine("Press any key to exit....")
Console.ReadKey()
運行該應(yīng)用程序,它將使用條形碼web service,得到條形碼,并保存在本地磁盤上。
很簡單。只需要安裝Code39字體,或者加載到系統(tǒng)中就可以。
報表設(shè)置該字體自然就表示成條形碼了。
Code39字體從網(wǎng)上搜索能搜索到。
一、條形碼的讀取
用過鍵盤口式的掃條碼工具的朋友就知道,它就如同在鍵盤上按下數(shù)字鍵一樣,基本不需任何編程和處理。但如果你使用的是其它接口的話,可能你就要為該設(shè)備編寫通訊代碼了。以下有一段簡單的25針串口的條碼讀取器通訊代碼。
Option Explicit
Dim sData As String
Private Sub Form_Load()
With MSComm1
.CommPort = 3 '設(shè)為COM3,試運行的系統(tǒng)而定,你可提供一個Combox讓用戶選擇。
.PortOpen = True '打開通訊端口
End With
End Sub
Private Sub MSComm1_OnComm()
Dim EndPos As Integer
Select Case MSComm1.CommEvent
Case comEvReceive '當(dāng)有數(shù)據(jù)傳送過來時
sData = sData Trim(MSComm1.Input)
'檢索回車,通常讀卡機(jī)每組數(shù)據(jù)結(jié)尾都返回一個回車作為結(jié)束符
EndPos = InStr(1, sData, Chr(13))
If EndPos = 0 Then '如果未結(jié)束就繼續(xù)努力
Else '讀完一組。
lblBarCode.Caption = sData '顯示一組條形碼
With lstBarCode
.AddItem Mid(sData, 1, EndPos - 1) '添加一組條形碼到列表
End With
sData = "" '清空
End If
End Select
End Sub
Private Sub cmdEnd_Click()
MSComm1.PortOpen = False '關(guān)閉端口
End
End Sub
二:條形碼的生成
在VB上編程本來就不難。以下關(guān)于條形碼生成的代碼也是很容易理解,只需使用一個OFFICE的附帶的 BarCode控件就可以輕松打印出11種不同標(biāo)準(zhǔn)的條形碼,足以滿足我們的要求。想起我書架上的一本書中的一篇用Turbo C編寫條形碼打印程序文章,長篇大論,那時不知看了n天,打了n小時字結(jié)果也不盡人意,現(xiàn)在真是幸福多了:)。廢話說完,得回歸正題。且看條形碼生成的代碼及有關(guān)說明。
源代碼主要由兩個窗體(frmMain主窗體和frmOption條碼設(shè)置窗體)和兩個模塊組成(modGetScreen.bas、SysDLG32.bas)。考慮到篇幅,這里只列出部分較為關(guān)鍵的代碼。
新建一個標(biāo)準(zhǔn)工程,添加一個名為(Microsoft Access BarCode
Control9)的條形碼部件,并添加一個條碼控件到窗口,并將窗口改名為frmMain,如圖所示。由于控件比較多,這里不便細(xì)說,詳細(xì)內(nèi)容請看源代碼。
模塊modGetScreen.bas代碼如下:
Option Explicit
聲明BitBlt、GetDesktopWindow、GetWindowDC、ReleaseDC這幾個API函數(shù)略
Public RegUser As Boolean
Sub GetObjImage1(Obj As Object, OwnerForm As PictureBox, Picture1
As PictureBox)
'hDC
Dim hWndDesk As Long
Dim hDCDesk As Long
'區(qū)域表達(dá)變量
Dim x As Long
Dim y As Long
Dim w As Long
Dim h As Long
x = Obj.Left Screen.TwipsPerPixelX
y = Obj.Top Screen.TwipsPerPixelY
w = Obj.Width Screen.TwipsPerPixelX
h = Obj.Height Screen.TwipsPerPixelY
hDCDesk = OwnerForm.hdc
'取出圖像
Call BitBlt(Picture1.hdc, 0, 0, w, h, hDCDesk, x, y,
vbSrcCopy)
Call ReleaseDC(hWndDesk, hDCDesk)
End Sub
主窗體frmMain.frm部分代碼如下:
Private Sub cmdPrint_Click()
'生成條形碼圖像
Dim r As Long, i As Integer, t As String,cfile As
String '臨時變量
t = BarCode
For i = 0 To Val(Times) - 1
BarCode1.Value = BarCode + i
DoEvents
Picture1.Refresh
GetObjImage1 BarCode1, Conel, Picture1
If RegUser = False Then '如果未注冊添加MASK標(biāo)記
Picture1.PaintPicture Picture2.Picture, 300, 300
End If
If Dir(SavePath, vbDirectory) = "" Then MkDir SavePath
SavePath = SavePath IIf(Right(SavePath, 1) "", "",
"")
cfile = SavePath BarCode1.Value ".bmp"
SavePicture Picture1.Image, cfile '將條形碼保存為圖像文件以便打印
Next
BarCode = t
End Sub
條形碼設(shè)置窗體frmOption.frm代碼如下:
Option Explicit
'條形碼設(shè)置模塊
Private Sub cboBig_Click()
BarCode1.Style = cboBig.ListIndex '改變標(biāo)準(zhǔn)
End Sub
Private Sub cboDirection_Click()
BarCode1.Direction = cboDirection.ListIndex '改變方向
End Sub
Private Sub cboLine_Click()
BarCode1.LineWeight = cboLine.ListIndex '改變線寬
End Sub
Private Sub cboSmall_Click()
BarCode1.SubStyle = cboSmall.ListIndex '改變樣式
End Sub
Private Sub Check1_Click()
BarCode1.ShowData = Check1.Value '是否顯示數(shù)據(jù)
End Sub
Private Sub cmdChange_Click()
'設(shè)置長、寬大小
BarWidth = BarCode1.Height
BarHeight = BarCode1.Width
cmdRefresh_Click
End Sub
Private Sub cmdOK_Click()
'傳送條形碼設(shè)定到主界面
With frmMain.BarCode1
.LineWeight = BarCode1.LineWeight
.Style = BarCode1.Style
.SubStyle = BarCode1.SubStyle
.Direction = BarCode1.Direction
.Width = BarCode1.Width
.Height = BarCode1.Height
.ShowData = BarCode1.ShowData
Me.Hide
End With
With frmMain
.Picture1.Width = .BarCode1.Width
.Picture1.Height = .BarCode1.Height
.Conel.Width = .BarCode1.Width
.Conel.Height = .BarCode1.Height
End With
End Sub
Private Sub cmdRefresh_Click()
BarCode1.Width = BarWidth
BarCode1.Height = BarHeight
End Sub
Private Sub Form_Load()
LoadBarInfo
BarWidth = BarCode1.Width
BarHeight = BarCode1.Height
End Sub
Sub LoadBarInfo() '初始化選項
LoadBigClass cboBig
LoadSmallClass cboSmall
LoadLineSize cboLine
LoadDirection cboDirection
End Sub
Sub LoadBigClass(cbo As ComboBox) '條碼標(biāo)準(zhǔn)
With cbo
.AddItem "UPC-A"
.AddItem "UPC-E"
.AddItem "EAN-13"
.AddItem "EAN-8"
.AddItem "Case Code"
.AddItem "Codabar (NW-T)"
.AddItem "Code-39"
.AddItem "Code-128"
.AddItem "U.S. Postnet"
.AddItem "U.S. Postal FIM"
.AddItem "JP Post"
.ListIndex = 2
End With
End Sub
Sub LoadSmallClass(cbo As ComboBox) '條碼樣式
With cbo
.AddItem "Standard"
.AddItem "2-Digit Supplement"
.AddItem "5-Digit Supplement"
.AddItem "POS Case Code"
.ListIndex = 0
End With
End Sub
許多人在編寫數(shù)據(jù)庫應(yīng)用程序時,都想要加上條形碼功能加強(qiáng)工作效率,尤其是銷售管理,圖書館管理這類流量大的應(yīng)用軟件,但由于條形碼技術(shù)難以掌握、標(biāo)誰又多以及過去的技術(shù)種種原因,使得許多人望而卻步。本文介紹的一套簡單實用的條形碼解決方法,希望能幫助各位完善軟件系統(tǒng)的功能。