|
楼主 |
发表于 2024-5-9 14:25
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
本帖最后由 gnefnuy 于 2024-5-9 17:39 编辑
[ 本帖最后由 gnefnuy 于 2024-5-9 17:38 编辑 ]\n\n第一段代码被吞了?放这楼吧,或者版主帮助编辑一下,谢谢
- ' 实现百度云OCR的支持
- ' gnefnuy 2024.5.9
- Option Explicit
- Private Const BASE_URL As String = "https://aip.baidubce.com/rest/2.0"
- Private Const OCR_URL As String = BASE_URL & "/ocr/v1/"
- Private Const IMAGE_CLASSIFY_URL As String = BASE_URL & "/image-classify/v1/"
- Public apiKey As String
- Public secretKey As String
- Public Sub Setup(apiKey As String, secretKey As String)
- Me.apiKey = apiKey
- Me.secretKey = secretKey
- End Sub
- ' 使用 AK,SK 生成鉴权签名(Access Token),有效期30天,需加入避免重复获取
- Public Function GetAccessToken() As String
- Dim token As String
- ' TODO:先获取本地数据,是否有Token,如果没有就去平台获取
- token = "*****可以用30天************"
- GetAccessToken = token: Exit Function
- Dim client As New WebClient
- Dim request As New WebRequest
- Dim response As WebResponse
-
- With request
- .Resource = "https://aip.baidubce.com/oauth/2.0/token"
- .Method = WebMethod.HttpPost
- .AddQuerystringParam "grant_type", "client_credentials"
- .AddQuerystringParam "client_id", Me.apiKey
- .AddQuerystringParam "client_secret", Me.secretKey
- End With
- Set response = client.Execute(request)
- GetAccessToken = response.data("access_token")
- End Function
- ' 通过本地行驶证图片文件识别
- Public Function VehicleLicenseFile(Imgpath As String) As Dictionary
- Set VehicleLicenseFile = OCR("vehicle_license", Imgpath)
- End Function
- ' 通过行驶证图片网络地址识别
- Public Function VehicleLicenseUrl(ImgUrl As String) As Dictionary
- Set VehicleLicenseUrl = OCR("vehicle_license", ImgUrl, 2)
- End Function
- ' 通过本地驾驶证图片文件识别
- Public Function DrivingLicenseFile(Imgpath As String) As Dictionary
- Set DrivingLicenseFile = OCR("driving_license", Imgpath)
- End Function
- ' 通过本地图片识别车牌
- Public Function LicensePlateFile(Imgpath As String) As Dictionary
- Set LicensePlateFile = OCR("license_plate", Imgpath)
- End Function
- ' 通过本地图片识别VIN
- Public Function VinCodeFile(Imgpath As String) As Dictionary
- Set VinCodeFile = OCR("vin_code", Imgpath)
- End Function
- ' 通过本地图片识别机动车销售发票
- Public Function VehicleInvoiceFile(Imgpath As String) As Dictionary
- Set VehicleInvoiceFile = OCR("vehicle_invoice", Imgpath)
- End Function
- ' 通过本地图片识别身份证
- Public Function IdcardFile(Imgpath As String) As Dictionary
- Set IdcardFile = OCR("idcard", Imgpath)
- End Function
- ' 识别器
- ' OcrType: 识别类别,按照供应商提供的数据来
- ' UriType: 0->本地文件路径(默认),2->网络地址,3->BASE64
- ' body: 对应的相应参数,默认为空
- Private Function OCR(OcrType As String, Uri As String, Optional UriType As Integer = 0, Optional body As Dictionary = Nothing) As Dictionary
- Dim client As New WebClient
- Dim request As New WebRequest
- Dim response As WebResponse
- Dim body_ As New Dictionary
- Dim result As New Dictionary
-
- Set body_ = body
- ' 设置请求参数
- With request
- .Resource = OCR_URL & OcrType & "?access_token=" & GetAccessToken()
- .Method = WebMethod.HttpPost
- .AddHeader "Content-Type", "application/x-www-form-urlencoded"
- .AddHeader "Accept", "application/json"
- .RequestFormat = FormUrlEncoded
- End With
-
- Select Case UriType
- Case 0
- body_.Add "image", WebHelpers.Base64EncodeFile(Uri) '这里我自己添加的Base64EncodeFile
- Case 1
- body_.Add "url", Uri
- Case 2
- body_.Add "image", Uri
- End Select
- ' 将请求正文添加到请求中
- Set request.body = body_
- ' 发送请求并获取响应
- Set response = client.Execute(request)
- Dim aStr As String
- aStr = DecodeToBytes(response.body) '将返回的UTF8字符转为VB可显示的ANSI字符,我自己加的
- 'aStr = WebHelpers.StringToAnsiBytes(response.body)'或者试试这个自带的
- Set result = WebHelpers.ParseJson(aStr)' 我去掉了非ASCII字符的编码,中文不会输出为\u****类的编码
- ' 输出响应内容
- Set OCR = result
- End Function
复制代码
|
评分
-
2
查看全部评分
-
|