|
因为公司电脑VBA代码保存生成的文件都自动加密(有安企通保密软件),所以只能改变思路,通过剪切板存放的图片转换为字节数组了。
VBA没有Picture1控件,因使用到此控件的PropertyBag,所以我将以下代码转换在VB6来使用,直接让验证码的截图转换为字节数组的变量来识别验证码。但运行起来验证码识别是空的,难道是我处理字节数组出了问题?还请老师们帮忙查看下,谢谢了。
Option Explicit
'地址改为自己的
Private Declare Sub InitModel Lib "C:\Script\Lcb\ddocr_qs.dll" (ByVal threadnum As Long)
Private Declare Sub FreeModel Lib "C:\Script\Lcb\ddocr_qs.dll" ()
'-----------
Private Declare Function Identify Lib "C:\Script\Lcb\ddocr_qs.dll" (ByVal im As Long, ByVal imlen As Long) As Long
'Declare Function Identify Lib "C:\Script\Lcb\ddocr_qs.dll" (ByVal im As Byte, ByVal imlen As Integer) As String '斌弄的出错
'-----------
Private Declare Function lstrlenA Lib "kernel32.dll" (ByVal lpString As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByVal Destination As Long, ByVal Source As Long, ByVal Length As Long)
Private Sub Form_Load()
'用QQ截图工具先把验证码的图片截图到内存剪粘板,再运行此程序
Dim path As String, i As Long, pathbase As String
Dim Picbytearr() As Byte
Dim address
Dim str$
'path = App.path & "\" & 1 & ".BMP"
'Picture1.Picture = LoadPicture(path) '只能加载BMP文件
Picture1.AutoRedraw = True '必须设为 True ,这样才能图形和文本输出到屏幕,并存储在内存的图象中。然后可以使用 Image 属性
Picture1.Picture = Clipboard.GetData(vbCFBitmap) '剪粘板的截图赋值给Picture1
Picbytearr = PictureToBin(Picture1.Picture) '转成字节数组
InitModel (5)
address = Identify(VarPtr(Picbytearr(0)), UBound(Picbytearr) + 1)
str = StringFromPointerA(address)
End Sub
' ***********************************************************************
' 将图片转换成字节数组
Public Function PictureToBin(Picture As StdPicture) As Byte()
'在VB6下建立窗体From1,再添加Picture1控件
Dim oPB As PropertyBag
Set oPB = New PropertyBag
oPB.WriteProperty "P", Picture, Nothing
PictureToBin = oPB.Contents
Set oPB = Nothing
End Function
' 从字节数组还原图片
Public Function BinToPicture(Data() As Byte) As StdPicture
Dim oPB As PropertyBag
Set oPB = New PropertyBag
oPB.Contents = Data()
Set BinToPicture = oPB.ReadProperty("P", Nothing)
Set oPB = Nothing
End Function
' ***********************************************************************
'获取识别结果
Function GetStr(path As String)
Dim address, str As String, bytearr() As Byte
bytearr = LoadImageData(path)
InitModel (5)
'InitModel (6)
address = Identify(VarPtr(bytearr(0)), UBound(bytearr) + 1)
GetStr = StringFromPointerA(address)
Debug.Print GetStr
FreeModel
End Function
' 加载图片数据到字节数组
Function LoadImageData(FilePath As String) As Byte()
Dim fileNo As Integer
fileNo = FreeFile()
Open FilePath For Binary As #fileNo
ReDim bytes(LOF(fileNo) - 1) As Byte
Get #fileNo, , bytes
Close #fileNo
LoadImageData = bytes
End Function
'由字符串指针(内存地址)获取字符串
Public Function StringFromPointerA(ByVal pointerToString As Long) As String
Dim tmpBuffer() As Byte
Dim byteCount As Long
Dim retVal As String
byteCount = lstrlenA(pointerToString)
If byteCount > 0 Then
ReDim tmpBuffer(0 To byteCount - 1) As Byte
Call CopyMemory(VarPtr(tmpBuffer(0)), pointerToString, byteCount)
End If
retVal = StrConv(tmpBuffer, vbUnicode)
StringFromPointerA = retVal
End Function
|
|