├── README.md ├── docs └── demo.gif ├── icon.png ├── info.plist ├── ocr.php └── snapshot.sh /README.md: -------------------------------------------------------------------------------- 1 | # Alfred-OCR 2 | 3 | ## 用法 4 | 5 | 在 release 下载 .workflow 文件,双击安装(需要 Alfred Powerpack)。 6 | 7 | 唤醒 Alfred,输入 OCR,回车确定,截图。稍等几秒(视网速而定),粘贴即可。 8 | 9 | ![demo](docs/demo.gif) 10 | 11 | ## 致谢 12 | 13 | 谢谢 nannanziyu 在 https://www.v2ex.com/t/411218 提供的想法。本插件也使用了他公布的百度 API KEY。 14 | 15 | 感谢 https://github.com/ginfuru/alfred-screen-capture ,我在其中找到了调用 Mac 截图的方法,也偷懒使用了他的图标。 16 | 17 | -------------------------------------------------------------------------------- /docs/demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jadec0der/alfred-ocr/8ed93b736fc0e2b7819c86415e2fed6173ae9f3e/docs/demo.gif -------------------------------------------------------------------------------- /icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jadec0der/alfred-ocr/8ed93b736fc0e2b7819c86415e2fed6173ae9f3e/icon.png -------------------------------------------------------------------------------- /info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | bundleid 6 | ocr.jadecoder 7 | category 8 | Productivity 9 | connections 10 | 11 | 3DCFE25F-0397-4621-9A4C-F581010F9ED9 12 | 13 | 14 | destinationuid 15 | A7F287C4-094C-4029-93E7-90228EDB11DB 16 | modifiers 17 | 0 18 | modifiersubtext 19 | 20 | vitoclose 21 | 22 | 23 | 24 | 8F58BF32-7272-40EF-9CAA-10A7E6B9AFF6 25 | 26 | 27 | destinationuid 28 | 5FD4A95E-C4F8-46A6-8FF6-BEC276C51E15 29 | modifiers 30 | 0 31 | modifiersubtext 32 | 33 | vitoclose 34 | 35 | 36 | 37 | destinationuid 38 | 0D93798F-3A8F-4D30-B1C7-CC7C3914A956 39 | modifiers 40 | 0 41 | modifiersubtext 42 | 43 | vitoclose 44 | 45 | 46 | 47 | A7F287C4-094C-4029-93E7-90228EDB11DB 48 | 49 | 50 | destinationuid 51 | 8F58BF32-7272-40EF-9CAA-10A7E6B9AFF6 52 | modifiers 53 | 0 54 | modifiersubtext 55 | 56 | vitoclose 57 | 58 | 59 | 60 | 61 | createdby 62 | jadecoder 63 | description 64 | Take a snapshot and use Baidu OCR API to recoginze 65 | disabled 66 | 67 | name 68 | OCR 69 | objects 70 | 71 | 72 | config 73 | 74 | concurrently 75 | 76 | escaping 77 | 100 78 | script 79 | $token_url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=N7GWF63CRc2RM8iYHih6GBjZ&client_secret=McqgCdBOprgOjdiysA7U18FTouX1iwDz"; 80 | $arrContextOptions=["ssl" => ["verify_peer" => false, "verify_peer_name" => false]]; 81 | 82 | $file_path = "{query}"; 83 | 84 | $image = file_get_contents($file_path); 85 | $base64_img = base64_encode($image); 86 | 87 | $token_response = file_get_contents($token_url, false, stream_context_create($arrContextOptions)); 88 | $token = json_decode($token_response, true)["access_token"]; 89 | $ocr_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general?access_token=".$token; 90 | 91 | $ch = curl_init(); 92 | curl_setopt($ch, CURLOPT_URL,$ocr_url); 93 | curl_setopt($ch, CURLOPT_POST, 1); 94 | curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(["image" => $base64_img])); 95 | curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); 96 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 97 | 98 | $result = curl_exec($ch); 99 | 100 | curl_close ($ch); 101 | 102 | $words = json_decode($result, true)['words_result']; 103 | foreach ($words as $word) { 104 | echo $word["words"]."\n"; 105 | } 106 | scriptargtype 107 | 0 108 | scriptfile 109 | 110 | type 111 | 1 112 | 113 | type 114 | alfred.workflow.action.script 115 | uid 116 | 8F58BF32-7272-40EF-9CAA-10A7E6B9AFF6 117 | version 118 | 2 119 | 120 | 121 | config 122 | 123 | autopaste 124 | 125 | clipboardtext 126 | {query} 127 | transient 128 | 129 | 130 | type 131 | alfred.workflow.output.clipboard 132 | uid 133 | 5FD4A95E-C4F8-46A6-8FF6-BEC276C51E15 134 | version 135 | 2 136 | 137 | 138 | config 139 | 140 | argumenttype 141 | 2 142 | keyword 143 | ocr 144 | subtext 145 | 146 | text 147 | Take Snapshot and Recognize 148 | withspace 149 | 150 | 151 | type 152 | alfred.workflow.input.keyword 153 | uid 154 | 3DCFE25F-0397-4621-9A4C-F581010F9ED9 155 | version 156 | 1 157 | 158 | 159 | config 160 | 161 | concurrently 162 | 163 | escaping 164 | 63 165 | script 166 | screencapture -i /tmp/ocr_snapshot.png 167 | 168 | echo -n /tmp/ocr_snapshot.png 169 | scriptargtype 170 | 0 171 | scriptfile 172 | 173 | type 174 | 0 175 | 176 | type 177 | alfred.workflow.action.script 178 | uid 179 | A7F287C4-094C-4029-93E7-90228EDB11DB 180 | version 181 | 2 182 | 183 | 184 | config 185 | 186 | lastpathcomponent 187 | 188 | onlyshowifquerypopulated 189 | 190 | removeextension 191 | 192 | text 193 | {query} 194 | title 195 | OCR Complete! 196 | 197 | type 198 | alfred.workflow.output.notification 199 | uid 200 | 0D93798F-3A8F-4D30-B1C7-CC7C3914A956 201 | version 202 | 1 203 | 204 | 205 | readme 206 | Take a snapshot and call Baidu OCR API to recognize. 207 | uidata 208 | 209 | 0D93798F-3A8F-4D30-B1C7-CC7C3914A956 210 | 211 | xpos 212 | 680 213 | ypos 214 | 330 215 | 216 | 3DCFE25F-0397-4621-9A4C-F581010F9ED9 217 | 218 | xpos 219 | 110 220 | ypos 221 | 170 222 | 223 | 5FD4A95E-C4F8-46A6-8FF6-BEC276C51E15 224 | 225 | xpos 226 | 680 227 | ypos 228 | 170 229 | 230 | 8F58BF32-7272-40EF-9CAA-10A7E6B9AFF6 231 | 232 | xpos 233 | 500 234 | ypos 235 | 170 236 | 237 | A7F287C4-094C-4029-93E7-90228EDB11DB 238 | 239 | xpos 240 | 320 241 | ypos 242 | 170 243 | 244 | 245 | version 246 | 0.2.0 247 | webaddress 248 | 249 | 250 | 251 | -------------------------------------------------------------------------------- /ocr.php: -------------------------------------------------------------------------------- 1 | ["verify_peer" => false, "verify_peer_name" => false]]; 5 | 6 | $file_path = "{query}"; 7 | 8 | $image = file_get_contents($file_path); 9 | $base64_img = base64_encode($image); 10 | 11 | $token_response = file_get_contents($token_url, false, stream_context_create($arrContextOptions)); 12 | $token = json_decode($token_response, true)["access_token"]; 13 | $ocr_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general?access_token=".$token; 14 | 15 | $ch = curl_init(); 16 | curl_setopt($ch, CURLOPT_URL,$ocr_url); 17 | curl_setopt($ch, CURLOPT_POST, 1); 18 | curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(["image" => $base64_img])); 19 | curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); 20 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 21 | 22 | $result = curl_exec($ch); 23 | 24 | curl_close ($ch); 25 | 26 | $words = json_decode($result, true)['words_result']; 27 | foreach ($words as $word) { 28 | echo $word["words"]."\n"; 29 | } 30 | -------------------------------------------------------------------------------- /snapshot.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | screencapture -i /tmp/ocr_snapshot.png 4 | 5 | echo -n /tmp/ocr_snapshot.png 6 | --------------------------------------------------------------------------------