Skip to content

Commit

Permalink
Merge pull request #2970 from JeffreySu/Developer
Browse files Browse the repository at this point in the history
Developer
  • Loading branch information
JeffreySu authored Dec 11, 2023
2 parents b02d16a + 0b1598f commit d8e59ad
Show file tree
Hide file tree
Showing 19 changed files with 912 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#region Apache License Version 2.0
/*----------------------------------------------------------------
Copyright 2023 Jeffrey Su & Suzhou Senparc Network Technology Co.,Ltd.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied. See the License for the specific language governing permissions
and limitations under the License.
Detail: https://github.com/JeffreySu/WeiXinMPSDK/blob/master/license.md
----------------------------------------------------------------*/
#endregion Apache License Version 2.0

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Senparc.Weixin.MP.AdvancedAPIs;
using Senparc.Weixin.MP.AdvancedAPIs.CV.OCR;
using Senparc.Weixin.MP.AdvancedAPIs.Poi;
using Senparc.Weixin.MP.CommonAPIs;
using Senparc.Weixin.MP.Containers;
using Senparc.Weixin.MP.Entities;
using Senparc.Weixin.MP.Test.CommonAPIs;

namespace Senparc.Weixin.MP.Test.AdvancedAPIs
{
[TestClass]
public class OCRTest : CommonApiTest
{
[TestMethod]
public void IdCardTest()
{
var accessToken = AccessTokenContainer.GetAccessToken(_appId);
var url = "https://";
var result = OCRApi.IdCard(accessToken, url);
Assert.IsNotNull(result);
Assert.AreEqual(result.errcode, ReturnCode.请求成功);
}

[TestMethod]
public void MenuTest()
{
var accessToken = AccessTokenContainer.GetAccessToken(_appId);
var url = "https://";
var result = OCRApi.Menu(accessToken, url);
Assert.IsNotNull(result);
Assert.AreEqual(result.errcode, ReturnCode.请求成功);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void RequestXmlTest()
<EventKey><![CDATA[OneClick]]></EventKey>
</xml>";

var xml = MessageAgent.RequestXml(null, BaseTest._serviceProvider, url, token, requestXml);
var xml = MessageAgent.RequestXml(null, _serviceProvider, url, token, requestXml);
var responseMessage = ResponseMessageBase.CreateFromResponseXml(xml, MpMessageEntityEnlightener.Instance);
Assert.IsNotNull(responseMessage);
Assert.IsInstanceOfType(responseMessage, typeof(ResponseMessageText));
Expand All @@ -66,16 +66,16 @@ public void CheckUrlAndTokenTest()
{
var url = "https://sdk.weixin.senparc.com/weixin";
var token = "weixin";
var result = MessageAgent.CheckUrlAndToken(BaseTest._serviceProvider, url, token);
var result = MessageAgent.CheckUrlAndToken(_serviceProvider, url, token);
Assert.IsTrue(result);

token = "wrong_token";
result = MessageAgent.CheckUrlAndToken(BaseTest._serviceProvider, url, token);
result = MessageAgent.CheckUrlAndToken(_serviceProvider, url, token);
Assert.IsFalse(false);

url = "wrong_url";
token = "weixin";
result = MessageAgent.CheckUrlAndToken(BaseTest._serviceProvider, url, token);
result = MessageAgent.CheckUrlAndToken(_serviceProvider, url, token);
Assert.IsFalse(false);
}
}
Expand Down
273 changes: 273 additions & 0 deletions src/Senparc.Weixin.MP/Senparc.Weixin.MP/AdvancedAPIs/CV/OCR/OCRApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,273 @@
#region Apache License Version 2.0
/*----------------------------------------------------------------
Copyright 2023 Jeffrey Su & Suzhou Senparc Network Technology Co.,Ltd.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied. See the License for the specific language governing permissions
and limitations under the License.
Detail: https://github.com/JeffreySu/WeiXinMPSDK/blob/master/license.md
----------------------------------------------------------------*/
#endregion Apache License Version 2.0

/*----------------------------------------------------------------
Copyright (C) 2023 Senparc
文件名:OCRApi.cs
文件功能描述:智能接口 /OCR识别
创建标识:yaofeng - 20231204
----------------------------------------------------------------*/

using Senparc.NeuChar;
using Senparc.Weixin.CommonAPIs;
using System.Threading.Tasks;

namespace Senparc.Weixin.MP.AdvancedAPIs.CV.OCR
{
/// <summary>
/// 智能接口 /OCR识别
/// https://developers.weixin.qq.com/doc/offiaccount/Intelligent_Interface/OCR.html
/// </summary>
[NcApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, true)]
public static class OCRApi
{
#region 同步方法
/// <summary>
/// 身份证OCR识别
/// </summary>
/// <param name="accessTokenOrAppId">Token</param>
/// <param name="img_url">图片地址</param>
/// <param name="timeOut">代理请求超时时间(毫秒)</param>
/// <returns></returns>
public static IdCardJsonResult IdCard(string accessTokenOrAppId, string img_url, int timeOut = Config.TIME_OUT)
{
return ApiHandlerWapper.TryCommonApi(accessToken =>
{
var url = string.Format(Config.ApiMpHost + "/cv/ocr/idcard?img_url={0}&access_token={1}", System.Web.HttpUtility.UrlEncode(img_url), accessToken);
return CommonJsonSend.Send<IdCardJsonResult>(null, url, new { }, CommonJsonSendType.POST, timeOut: timeOut, contentType: "application/json");
}, accessTokenOrAppId);
}

/// <summary>
/// 银行卡OCR识别
/// </summary>
/// <param name="accessTokenOrAppId">Token</param>
/// <param name="img_url">图片地址</param>
/// <param name="timeOut">代理请求超时时间(毫秒)</param>
/// <returns></returns>
public static BankCardJsonResult BankCard(string accessTokenOrAppId, string img_url, int timeOut = Config.TIME_OUT)
{
return ApiHandlerWapper.TryCommonApi(accessToken =>
{
string url = string.Format(Config.ApiMpHost + "/cv/ocr/bankcard?img_url={0}&access_token={1}", System.Web.HttpUtility.UrlEncode(img_url), accessToken);
return CommonJsonSend.Send<BankCardJsonResult>(null, url, new { }, CommonJsonSendType.POST, timeOut: timeOut, contentType: "application/json");
}, accessTokenOrAppId);
}

/// <summary>
/// 行驶证OCR识别
/// </summary>
/// <param name="accessTokenOrAppId">Token</param>
/// <param name="img_url">图片地址</param>
/// <param name="timeOut">代理请求超时时间(毫秒)</param>
/// <returns></returns>
public static DrivingJsonResult Driving(string accessTokenOrAppId, string img_url, int timeOut = Config.TIME_OUT)
{
return ApiHandlerWapper.TryCommonApi(accessToken =>
{
string url = string.Format(Config.ApiMpHost + "/cv/ocr/driving?img_url={0}&access_token={1}", System.Web.HttpUtility.UrlEncode(img_url), accessToken);
return CommonJsonSend.Send<DrivingJsonResult>(null, url, new { }, CommonJsonSendType.POST, timeOut: timeOut, contentType: "application/json");
}, accessTokenOrAppId);
}

/// <summary>
/// 营业执照OCR识别
/// </summary>
/// <param name="accessTokenOrAppId">Token</param>
/// <param name="img_url">图片地址</param>
/// <param name="timeOut">代理请求超时时间(毫秒)</param>
/// <returns></returns>
public static BizLicenseJsonResult BizLicense(string accessTokenOrAppId, string img_url, int timeOut = Config.TIME_OUT)
{
return ApiHandlerWapper.TryCommonApi(accessToken =>
{
string url = string.Format(Config.ApiMpHost + "/cv/ocr/bizlicense?img_url={0}&access_token={1}", System.Web.HttpUtility.UrlEncode(img_url), accessToken);
return CommonJsonSend.Send<BizLicenseJsonResult>(null, url, new { }, CommonJsonSendType.POST, timeOut: timeOut, contentType: "application/json");
}, accessTokenOrAppId);
}

/// <summary>
/// 通用印刷体OCR识别
/// </summary>
/// <param name="accessTokenOrAppId">Token</param>
/// <param name="img_url">图片地址</param>
/// <param name="timeOut">代理请求超时时间(毫秒)</param>
/// <returns></returns>
public static CommJsonResult Comm(string accessTokenOrAppId, string img_url, int timeOut = Config.TIME_OUT)
{
return ApiHandlerWapper.TryCommonApi(accessToken =>
{
string url = string.Format(Config.ApiMpHost + "/cv/ocr/comm?img_url={0}&access_token={1}", System.Web.HttpUtility.UrlEncode(img_url), accessToken);
return CommonJsonSend.Send<CommJsonResult>(null, url, new { }, CommonJsonSendType.POST, timeOut: timeOut, contentType: "application/json");
}, accessTokenOrAppId);
}

/// <summary>
/// 车牌OCR识别
/// </summary>
/// <param name="accessTokenOrAppId">Token</param>
/// <param name="img_url">图片地址</param>
/// <param name="timeOut">代理请求超时时间(毫秒)</param>
/// <returns></returns>
public static PlateNumJsonResult PlateNum(string accessTokenOrAppId, string img_url, int timeOut = Config.TIME_OUT)
{
return ApiHandlerWapper.TryCommonApi(accessToken =>
{
string url = string.Format(Config.ApiMpHost + "/cv/ocr/platenum?img_url={0}&access_token={1}", System.Web.HttpUtility.UrlEncode(img_url), accessToken);
return CommonJsonSend.Send<PlateNumJsonResult>(null, url, new { }, CommonJsonSendType.POST, timeOut: timeOut, contentType: "application/json");
}, accessTokenOrAppId);
}

/// <summary>
/// 菜单OCR识别
/// </summary>
/// <param name="accessTokenOrAppId">Token</param>
/// <param name="img_url">图片地址</param>
/// <param name="timeOut">代理请求超时时间(毫秒)</param>
/// <returns></returns>
public static MenuJsonResult Menu(string accessTokenOrAppId, string img_url, int timeOut = Config.TIME_OUT)
{
return ApiHandlerWapper.TryCommonApi(accessToken =>
{
string url = string.Format(Config.ApiMpHost + "/cv/ocr/menu?img_url={0}&access_token={1}", System.Web.HttpUtility.UrlEncode(img_url), accessToken);
return CommonJsonSend.Send<MenuJsonResult>(null, url, new { }, CommonJsonSendType.POST, timeOut: timeOut, contentType: "application/json");
}, accessTokenOrAppId);
}
#endregion

#region 异步方法
/// <summary>
/// 身份证OCR识别
/// </summary>
/// <param name="accessTokenOrAppId">Token</param>
/// <param name="img_url">图片地址</param>
/// <param name="timeOut">代理请求超时时间(毫秒)</param>
/// <returns></returns>
public static async Task<IdCardJsonResult> IdCardAsync(string accessTokenOrAppId, string img_url, int timeOut = Config.TIME_OUT)
{
return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
{
string url = string.Format(Config.ApiMpHost + "/cv/ocr/idcard?img_url={0}&access_token={1}", System.Web.HttpUtility.UrlEncode(img_url), accessToken);
return await CommonJsonSend.SendAsync<IdCardJsonResult>(null, url, new { }, CommonJsonSendType.POST, timeOut: timeOut, contentType: "application/json");
}, accessTokenOrAppId);
}

/// <summary>
/// 银行卡OCR识别
/// </summary>
/// <param name="accessTokenOrAppId">Token</param>
/// <param name="img_url">图片地址</param>
/// <param name="timeOut">代理请求超时时间(毫秒)</param>
/// <returns></returns>
public static async Task<BankCardJsonResult> BankCardAsync(string accessTokenOrAppId, string img_url, int timeOut = Config.TIME_OUT)
{
return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
{
string url = string.Format(Config.ApiMpHost + "/cv/ocr/bankcard?img_url={0}&access_token={1}", System.Web.HttpUtility.UrlEncode(img_url), accessToken);
return await CommonJsonSend.SendAsync<BankCardJsonResult>(null, url, new { }, CommonJsonSendType.POST, timeOut: timeOut, contentType: "application/json");
}, accessTokenOrAppId);
}

/// <summary>
/// 行驶证OCR识别
/// </summary>
/// <param name="accessTokenOrAppId">Token</param>
/// <param name="img_url">图片地址</param>
/// <param name="timeOut">代理请求超时时间(毫秒)</param>
/// <returns></returns>
public static async Task<DrivingJsonResult> DrivingAsync(string accessTokenOrAppId, string img_url, int timeOut = Config.TIME_OUT)
{
return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
{
string url = string.Format(Config.ApiMpHost + "/cv/ocr/driving?img_url={0}&access_token={1}", System.Web.HttpUtility.UrlEncode(img_url), accessToken);
return await CommonJsonSend.SendAsync<DrivingJsonResult>(null, url, new { }, CommonJsonSendType.POST, timeOut: timeOut, contentType: "application/json");
}, accessTokenOrAppId);
}

/// <summary>
/// 营业执照OCR识别
/// </summary>
/// <param name="accessTokenOrAppId">Token</param>
/// <param name="img_url">图片地址</param>
/// <param name="timeOut">代理请求超时时间(毫秒)</param>
/// <returns></returns>
public static async Task<BizLicenseJsonResult> BizLicenseAsync(string accessTokenOrAppId, string img_url, int timeOut = Config.TIME_OUT)
{
return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
{
string url = string.Format(Config.ApiMpHost + "/cv/ocr/bizlicense?img_url={0}&access_token={1}", System.Web.HttpUtility.UrlEncode(img_url), accessToken);
return await CommonJsonSend.SendAsync<BizLicenseJsonResult>(null, url, new { }, CommonJsonSendType.POST, timeOut: timeOut, contentType: "application/json");
}, accessTokenOrAppId);
}

/// <summary>
/// 通用印刷体OCR识别
/// </summary>
/// <param name="accessTokenOrAppId">Token</param>
/// <param name="img_url">图片地址</param>
/// <param name="timeOut">代理请求超时时间(毫秒)</param>
/// <returns></returns>
public static async Task<CommJsonResult> CommAsync(string accessTokenOrAppId, string img_url, int timeOut = Config.TIME_OUT)
{
return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
{
string url = string.Format(Config.ApiMpHost + "/cv/ocr/comm?img_url={0}&access_token={1}", System.Web.HttpUtility.UrlEncode(img_url), accessToken);
return await CommonJsonSend.SendAsync<CommJsonResult>(null, url, new { }, CommonJsonSendType.POST, timeOut: timeOut, contentType: "application/json");
}, accessTokenOrAppId);
}

/// <summary>
/// 车牌OCR识别
/// </summary>
/// <param name="accessTokenOrAppId">Token</param>
/// <param name="img_url">图片地址</param>
/// <param name="timeOut">代理请求超时时间(毫秒)</param>
/// <returns></returns>
public static async Task<PlateNumJsonResult> PlateNumAsync(string accessTokenOrAppId, string img_url, int timeOut = Config.TIME_OUT)
{
return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
{
string url = string.Format(Config.ApiMpHost + "/cv/ocr/platenum?img_url={0}&access_token={1}", System.Web.HttpUtility.UrlEncode(img_url), accessToken);
return await CommonJsonSend.SendAsync<PlateNumJsonResult>(null, url, new { }, CommonJsonSendType.POST, timeOut: timeOut, contentType: "application/json");
}, accessTokenOrAppId);
}

/// <summary>
/// 菜单OCR识别
/// </summary>
/// <param name="accessTokenOrAppId">Token</param>
/// <param name="img_url">图片地址</param>
/// <param name="timeOut">代理请求超时时间(毫秒)</param>
/// <returns></returns>
public static async Task<MenuJsonResult> MenuAsync(string accessTokenOrAppId, string img_url, int timeOut = Config.TIME_OUT)
{
return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
{
string url = string.Format(Config.ApiMpHost + "/cv/ocr/menu?img_url={0}&access_token={1}", System.Web.HttpUtility.UrlEncode(img_url), accessToken);
return await CommonJsonSend.SendAsync<MenuJsonResult>(null, url, new { }, CommonJsonSendType.POST, timeOut: timeOut, contentType: "application/json");
}, accessTokenOrAppId);
}
#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*----------------------------------------------------------------
Copyright (C) 2023 Senparc
文件名:BankCardJsonResult.cs
文件功能描述:OCR 银行卡识别返回结果
创建标识:yaofeng - 20231204
----------------------------------------------------------------*/

using Senparc.Weixin.Entities;

namespace Senparc.Weixin.MP.AdvancedAPIs.CV.OCR
{
/// <summary>
/// 银行卡识别
/// </summary>
public class BankCardJsonResult : WxJsonResult
{
/// <summary>
/// 银行卡号
/// </summary>
public string number { get; set; }
}
}
Loading

0 comments on commit d8e59ad

Please sign in to comment.