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 3:37:27 PM(UTC)
Jack Ma

Rank: Member

Reputation:

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

Was thanked: 7 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
        }
        if (OCRText.length) {
            clip.SetText(OCRText);
            clr.System.Media.SystemSounds.Asterisk.Play();
        }
    }
    else {
        clip.SetText(result);
    }
    
    response.Dispose();
    reader.Dispose();
}

Edited by user Sunday, March 7, 2021 7:31:42 AM(UTC)  | Reason: Not specified

thanks 4 users thanked Jack Ma for this useful post.
Rob on 2/12/2021(UTC), liuchina on 2/22/2021(UTC), 2014218866 on 3/8/2021(UTC), soooulp on 4/18/2021(UTC)
liuchina  
#2 Posted : Monday, February 22, 2021 12:34:27 AM(UTC)
liuchina

Rank: Advanced Member

Reputation:

Groups: Approved
Joined: 9/26/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 : Friday, February 26, 2021 5:14:32 AM(UTC)
Jack Ma

Rank: Member

Reputation:

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

Was thanked: 7 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 : Friday, February 26, 2021 5:16:35 AM(UTC)
soooulp

Rank: Advanced Member

Reputation:

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

Thanks: 21 times
Was thanked: 3 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



我也看不到图片,添加了百度OCR接口,可以运行哦,就是执行后没有提示,直接保存到剪切板了
2014218866  
#5 Posted : Monday, March 8, 2021 9:58:34 AM(UTC)
2014218866

Rank: Advanced Member

Reputation:

Groups: Approved
Joined: 5/6/2019(UTC)
Posts: 94
China

Thanks: 17 times
Was thanked: 1 time(s) in 1 post(s)
Dear Jack Ma,

Thank you very much for the script. It's really useful. The only regret is that when the clipboard is empty (that is, there is no text in the selected area), S + will report an error. If the clipboard is empty, a pop-up prompt "the text is not recognized" will appear on the screen, and this script will be better.Love

UserPostedImage


Jack Ma  
#6 Posted : Monday, March 8, 2021 10:19:28 AM(UTC)
Jack Ma

Rank: Member

Reputation:

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

Was thanked: 7 time(s) in 2 post(s)
Originally Posted by: 2014218866 Go to Quoted Post
Dear Jack Ma,

Thank you very much for the script. It's really useful. The only regret is that when the clipboard is empty (that is, there is no text in the selected area), S + will report an error. If the clipboard is empty, a pop-up prompt "the text is not recognized" will appear on the screen, and this script will be better.Love

UserPostedImage




I have updated the script to include the condition.
soooulp  
#7 Posted : Sunday, April 18, 2021 10:20:18 AM(UTC)
soooulp

Rank: Advanced Member

Reputation:

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

Thanks: 21 times
Was thanked: 3 time(s) in 2 post(s)
Originally Posted by: Jack Ma Go to Quoted Post
Originally Posted by: 2014218866 Go to Quoted Post
Dear Jack Ma,

Thank you very much for the script. It's really useful. The only regret is that when the clipboard is empty (that is, there is no text in the selected area), S + will report an error. If the clipboard is empty, a pop-up prompt "the text is not recognized" will appear on the screen, and this script will be better.Love






I have updated the script to include the condition.


大佬,

1 如何通过其他快捷键截图,比如win10自带win+shift+s,调取剪切板图片然后识图呀
2 能否调取剪切板的文字,然后使用google或者百度API翻译,结果再保存到剪切板

识图的这样试了下发现不行

Code:

sp.Sleep(100); 
sp.SendModifiedVKeys([vk.LSHIFT,vk.LWIN], [vk.VK_S]);
sp.Sleep(1000); 

var ms = new clr.System.IO.MemoryStream();
var Img = clip.GetImage();
Img.Save(ms, ImageFormat.Png);
ms.Position = 0;
var base64Image = clr.System.Convert.ToBase64String(ms.GetBuffer());
ms.Dispose()



soooulp  
#8 Posted : Tuesday, April 20, 2021 3:53:35 AM(UTC)
soooulp

Rank: Advanced Member

Reputation:

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

Thanks: 21 times
Was thanked: 3 time(s) in 2 post(s)
Originally Posted by: soooulp Go to Quoted Post
Originally Posted by: Jack Ma Go to Quoted Post
Originally Posted by: 2014218866 Go to Quoted Post
Dear Jack Ma,

Thank you very much for the script. It's really useful. The only regret is that when the clipboard is empty (that is, there is no text in the selected area), S + will report an error. If the clipboard is empty, a pop-up prompt "the text is not recognized" will appear on the screen, and this script will be better.Love






I have updated the script to include the condition.


大佬,

1 如何通过其他快捷键截图,比如win10自带win+shift+s,调取剪切板图片然后识图呀
2 能否调取剪切板的文字,然后使用google或者百度API翻译,结果再保存到剪切板

识图的这样试了下发现不行







The first one is solved by the post https://forum.strokesplus.net/posts/t8263

Thank you, Jack Ma.


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.