本帖最后由 523066680 于 2018-12-2 09:51 编辑
腾讯的官方文档:https://cloud.tencent.com/document/product/866/17734
官方的签名生成示例分别是 PHP JAVA Nodejs 和 C++ 的,所以我改了一个Perl的版本,补充了提交识别网络图片的代码。- =info
- 腾讯云 印刷体图片识别示例
- 523066680/vicyang
- 2018-12
- =cut
-
- use LWP::UserAgent;
- use Encode;
- use utf8;
- use MIME::Base64;
- use Digest::HMAC_SHA1 qw(hmac_sha1 hmac_sha1_hex);
-
- STDOUT->autoflush(1);
-
- my $s = sign();
- my $ua = LWP::UserAgent->new();
-
- my $json = '{"appid":"1254154701", "url":"http://www.code-by.org/styles/prosilver/theme/images/site_logo.png"}';
-
- my @header = (
- 'Authorization' => $s,
- 'Host' => 'recognition.image.myqcloud.com',
- 'Content-Type' => 'application/json',
- );
-
- #$ua->default_header( @header );
-
- my $res = $ua->post(
- 'http://recognition.image.myqcloud.com/ocr/general',
- @header,
- 'Content' => $json,
- );
-
- print encode('gbk', decode('utf8', $res->content()));
-
- sub sign
- {
- my $appid = "APPID";
- my $secret_id = "Secret_id";
- my $secret_key = "Secret_key";
- my $current = time();
- my $rdm = int( rand(10000) + 10000 );
- my $userid = 0;
-
- my @list = (
- [ 'a' => $appid ],
- [ 'k' => $secret_id ],
- [ 'e' => $current + 100 ],
- [ 't' => $current ],
- [ 'r' => $rdm ],
- [ 'u' => $userid ],
- [ 'f' => '' ],
- );
-
- $srcStr = join("", map { $_->[0] ."=". $_->[1] ."&" } @list );
- $srcStr =~s/&$//;
-
- # 生成签名,最右双引号表示不换行
- my $signStr = encode_base64( hmac_sha1( $srcStr, $secret_key) .$srcStr, "" );
-
- printf "%s\n", $srcStr;
- return $signStr;
- }
复制代码 输出:- {"code":0,"message":"OK","data":{"class":[],"angle":0.0,"items":[{"itemcoord":{"x":7,"y":20,"width":135,"height":13},"words":[{"character":"P","confidence":0.8444133400917053},{"character":"","confidence":1.0},{"character":"r","confidence":0.6299126744270325},{"character":"o","confidence":0.9383134841918944},{"character":"g","confidence":0.945770502090454},{"character":"r","confidence":0.9737642407417296},{"character":"","confidence":1.0},{"character":"a","confidence":0.9953640699386596},{"character":"","confidence":1.0},{"character":"m","confidence":0.9897430539131165},{"character":"m","confidence":0.9669923186302184},{"character":"i","confidence":0.9982288479804992},{"character":"n","confidence":0.999599277973175},{"character":"g","confidence":0.9985522627830504}],"itemstring":"P rogr a mming"}],"session_id":""}}
复制代码
|