StrokesPlus.net
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
Jack Ma  
#1 Posted : Friday, February 12, 2021 8:37:27 AM(UTC)
Jack Ma

Rank: Member

Reputation:

Groups: Approved
Joined: 11/22/2020(UTC)
Posts: 4
China

Was thanked: 4 time(s) in 2 post(s)
Replace clientId and clientSecret with your own keys.
Baidu OCR documents: https://ai.baidu.com/tech/ocr
Code:
var clientId = "5Pkb*************CqL";
var clientSecret = "fRoXM****************qydekZ";
sp.Sleep(100); 
//Create a new Bitmap in memory
var memoryImage = new drawing.System.Drawing.Bitmap(action.Bounds.Width, action.Bounds.Height);
//Create a graphics object associated with the bitmap
var memoryGraphics = drawing.System.Drawing.Graphics.FromImage(memoryImage);
//Copy the screen within the bounding rectangle of the drawn gesture area
//I used a square gesture since that seems more intuitive, but it's not neccessary
memoryGraphics.CopyFromScreen(action.Bounds.X, action.Bounds.Y, 0, 0, action.Bounds.Size);
var ms = new clr.System.IO.MemoryStream();
memoryImage.Save(ms, drawing.System.Drawing.Imaging.ImageFormat.Png);
ms.Position = 0;
var base64Image = clr.System.Convert.ToBase64String(ms.GetBuffer());
memoryGraphics.Dispose();
memoryImage.Dispose();
ms.Dispose()

var authHost = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=" + clientId + "&client_secret=" + clientSecret;
var client = new http.System.Net.Http.HttpClient();
var json = client.GetStringAsync(authHost);
var obj = JSON.parse(json.Result);
client.Dispose();

if (obj.access_token) {
    var token = obj.access_token;
    var ocrHost = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=" + token;
    var request = clr.System.Net.WebRequest.Create(ocrHost);
    request.Method = "post";
    var encoding = clr.System.Text.Encoding.Default;
    var paraString = "image=" + clr.System.Net.WebUtility.UrlEncode(base64Image);
    var buffer =  encoding.GetBytes(paraString);
    request.ContentLength = buffer.Length;
    request.GetRequestStream().Write(buffer, 0, buffer.Length);

    var response = request.GetResponse();
    var reader = new clr.System.IO.StreamReader(response.GetResponseStream(), clr.System.Text.Encoding.UTF8);
    var result = reader.ReadToEnd();

    var json = JSON.parse(result);
    var jsonArray = json.words_result;
    if (jsonArray) {
        var OCRText = "";
        for(var i=0; i<jsonArray.length; i++) {
            OCRText += jsonArray[i].words
        }
        clip.SetText(OCRText);
    }
    else {
        clip.SetText(result);
    }
    
    response.Dispose();
    reader.Dispose();
}
thanks 2 users thanked Jack Ma for this useful post.
Rob on 2/12/2021(UTC), liuchina on 2/21/2021(UTC)
liuchina  
#2 Posted : Sunday, February 21, 2021 5:34:27 PM(UTC)
liuchina

Rank: Advanced Member

Reputation:

Groups: Approved
Joined: 9/25/2018(UTC)
Posts: 43
China
Location: 北京

Thanks: 14 times
Was thanked: 1 time(s) in 1 post(s)
I thank you very much for your script, but there is an error when my computer runs this script. What is the reason for this?no
Jack Ma  
#3 Posted : Thursday, February 25, 2021 10:14:32 PM(UTC)
Jack Ma

Rank: Member

Reputation:

Groups: Approved
Joined: 11/22/2020(UTC)
Posts: 4
China

Was thanked: 4 time(s) in 2 post(s)
Originally Posted by: liuchina Go to Quoted Post
I thank you very much for your script, but there is an error when my computer runs this script. What is the reason for this?no


I can't load the image, can you paste the error message?
soooulp  
#4 Posted : Thursday, February 25, 2021 10:16:35 PM(UTC)
soooulp

Rank: Advanced Member

Reputation:

Groups: Approved
Joined: 4/23/2020(UTC)
Posts: 56
China

Thanks: 11 times
Was thanked: 1 time(s) in 1 post(s)
Originally Posted by: liuchina Go to Quoted Post
I thank you very much for your script, but there is an error when my computer runs this script. What is the reason for this?no



我也看不到图片,添加了百度OCR接口,可以运行哦,就是执行后没有提示,直接保存到剪切板了
Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.