油猴JavaScript脚本如何调用百度OCR接口获取access_token?

12style
  • 38

通过toDataURL,将图片转化为base64,结果为dataUrl,去掉编码头data:image/jpeg;base64, 进行urlencode,然后使用GM_xmlhttpRequest进行https请求,提示json未定义,如何获取access_token,以及如何输出最后识别的文字结果?

API文档

toDataURL(
                'https://i.imgur.com/GniA3wQ.png',
                function(dataUrl) {
                    const base64Data = dataUrl.split(';base64,')[1];
                    var API_Key="xxxxxxxxxxxxxxxxxxxxxxxx";
                    var Secret_Key="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
                    //access_token获取地址。
                    var getTokenUrl="https://aip.baidubce.com/oauth/2.0/token";
                    var token_Res = GM_xmlhttpRequest({
                        method: "POST",
                        url: getTokenUrl,
                        grant_type: "client_credentials",
                        client_id: API_Key,
                        client_secret: Secret_Key,
                        onload: function(response){
                            console.log("请求成功");
                            console.log(response.responseText);
                        },
                        onerror: function(response){
                            console.log("请求失败");
                        }
                    });
                    var access_token = token_Res.body.json().access_token;//提示json未定义

                    //通用文字识别,50000次/天免费
                    var ocrUrl = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic";
                    var ocr_Res = GM_xmlhttpRequest({
                        method: "POST",
                        headers: {
                            "Content-Type": "application/x-www-form-urlencoded"
                        },
                        access_token: access_token,
                        image: "image64=" + encodeURIComponent (base64Data),
                        language_type:"CHN_ENG",
                        //data: dataUrl,
                        onload: function(response){
                            console.log("请求成功");
                            console.log(response.responseText);
                        },
                        onerror: function(response){
                            console.log("请求失败");
                        }
                    });
                    var json = ocr_Res.body.json();
                    console.log(json);
                }

//img转化base64

    function toDataURL(src, callback, outputFormat) {
        var img = new Image();
        //img.crossOrigin = 'Anonymous';
        img.crossOrigin = "";
        img.onload = function() {
            var canvas = document.createElement('CANVAS');
            var ctx = canvas.getContext('2d');
            var dataURL;
            canvas.height = this.naturalHeight;
            canvas.width = this.naturalWidth;
            ctx.drawImage(this, 0, 0);
            dataURL = canvas.toDataURL(outputFormat);
            callback(dataURL);
        };
        img.src = src;
        if (img.complete || img.complete === undefined) {
            img.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
            img.src = src;
        }

    }
回复
阅读 237
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
你知道吗?

宣传栏