国产午夜男女在线|欧美日本一道高清国产|亚洲日韩乱码中文字幕|麻豆国产97在线精品一区|日韩一区2区三区另类图片|亚洲精品国产99在线观看|亚洲国产午夜福利精品大秀在线|一级做a爰片性色毛片免费网站

          1. <form id="n2a4a"><nav id="n2a4a"></nav></form>
          2. 您當(dāng)前的位置 :寧夏資訊網(wǎng) > 消費(fèi) >  內(nèi)容正文
            投稿

            Node.js開(kāi)發(fā)小程序,調(diào)用百度文字識(shí)別接口實(shí)現(xiàn)圖文識(shí)別

            寧夏資訊網(wǎng) 2020-03-28 06:49:57 來(lái)源: 閱讀:-

            百度云開(kāi)發(fā)注冊(cè)與配置

            首先需要注冊(cè)百度賬號(hào),并登錄百度云,進(jìn)入管理控制臺(tái),創(chuàng)建文字識(shí)別應(yīng)用,如下圖

            創(chuàng)建完應(yīng)用后,打開(kāi)應(yīng)用管理可見(jiàn)APP_ID、API_KEY、SECRET_KEY,需要用在小程序端調(diào)用文字識(shí)別接口。


            小程序服務(wù)端開(kāi)發(fā)

            由于百度提供了node.js的api,而小程序服務(wù)端云函數(shù)正是基于node的開(kāi)發(fā),在小程序開(kāi)發(fā)工具云函數(shù)目錄下打開(kāi)終端導(dǎo)入文字識(shí)別api,命令:npm install baidu-aip-sdk,下載完成后,可在云函數(shù)目錄看見(jiàn)node_modeules中&#39;baidu-aip-sdk&#39; api。

            在云函數(shù)目錄下新建conf.js用來(lái)存放APP_ID、API_KEY、SECRET_KEY。

            然后吊用api中的通用文字識(shí)別接口,傳入圖片即可。

            // 云函數(shù)入口文件

            const cloud = require(&#39;wx-server-sdk&#39;)

            let AipOcrClient = require(&#34;baidu-aip-sdk&#34;).ocr;

            const args = require(&#34;conf.js&#34;);

            cloud.init();

            // 云函數(shù)入口函數(shù)

            exports.main = async (event, context) =&gt; {

            // 設(shè)置APPID/AK/SK

            let APP_ID = args.APP_ID;

            let API_KEY = args.API_KEY;

            let SECRET_KEY = args.SECRET_KEY;

            // 新建一個(gè)對(duì)象,保存一個(gè)對(duì)象調(diào)用服務(wù)接口

            let client = new AipOcrClient(APP_ID, API_KEY, SECRET_KEY);

            let fileID = event.fileID;

            let res = await cloud.downloadFile({

            fileID: fileID,

            })

            let image = res.fileContent.toString(&#34;base64&#34;);

            // 調(diào)用通用文字識(shí)別, 圖片參數(shù)為遠(yuǎn)程url圖片

            return client.generalBasic(image);

            //console.log(result);

            // .then(function (result) {

            // let result = JSON.stringify(result);

            // return result;

            // })

            }

            小程序客戶(hù)端開(kāi)發(fā)

            圖片來(lái)源有兩種途徑,相冊(cè)選擇和相機(jī)拍攝。

            xaingce(e){//相冊(cè)響應(yīng)函數(shù)

            let tempFiles;

            let tempFilePaths;

            wx.chooseImage({

            count: 1,

            sizeType: [&#39;compressed&#39;],

            sourceType: [&#39;album&#39;, &#39;camera&#39;],

            success:res=&gt;{

            // tempFilePath可以作為img標(biāo)簽的src屬性顯示圖片

            tempFiles = res.tempFiles[0].size;

            tempFilePaths = res.tempFilePaths[0];

            if (tempFiles &gt; 3000000) {//大于3m

            wx.showToast({

            title: &#39;圖片大小大于3M&#39;,

            icon: &#39;none&#39;,

            duration: 2000

            });

            return;

            }

            wx.showLoading({

            title: &#39;識(shí)別中&#39;

            });

            this.uplaodF(tempFilePaths);

            setTimeout(function () {

            wx.hideLoading();

            }, 3000);

            }

            });

            },

            camera(){//相機(jī)響應(yīng)函數(shù)

            let ctx = wx.createCameraContext();

            ctx.takePhoto({

            quality: &#34;normal&#34;,

            success: (res) =&gt; {

            let tempFilePaths = res.tempImagePath;

            this.setData({

            camera: false

            });

            wx.showLoading({

            title: &#39;識(shí)別中&#39;

            });

            this.uplaodF(tempFilePaths);

            setTimeout(function () {

            wx.hideLoading();

            }, 3000);

            }

            });

            },

            圖片上傳實(shí)現(xiàn)代碼

            uplaodF(path){

            let result = false;

            let name = path.substring(path.lastIndexOf(&#39;/&#39;) + 1, path.lastIndexOf(&#39;.&#39;));

            wx.cloud.uploadFile({

            cloudPath: name,

            filePath: path, // 文件路徑

            }).then(res =&gt; {

            // get resource ID

            let id = res.fileID;

            //調(diào)用云函數(shù)識(shí)別圖片

            wx.cloud.callFunction({

            name: &#39;tongyong&#39;,

            data: {

            fileID: id

            }

            }).then(res =&gt; {

            let result = res.result.words_result;

            if (result.length &gt; 0) {

            let arr = &#39;&#39;;

            for (let i = 0; i &lt; result.length; i++) {

            arr += result[i].words

            }

            this.setData({

            words_result: arr

            })

            }else{

            this.setData({

            words_result: &#39;&#39;

            })

            }

            //刪除圖片

            wx.cloud.deleteFile({

            fileList: [id]

            }).then(res =&gt; {

            // handle success

            }).catch(error =&gt; {

            // handle error

            })

            }).catch(err =&gt; {

            console.log(err)

            });


            }).catch(error =&gt; {


            });

            },

            (正文已結(jié)束)

            推薦閱讀:硬盤(pán)作用是什么

            免責(zé)聲明及提醒:此文內(nèi)容為本網(wǎng)所轉(zhuǎn)載企業(yè)宣傳資訊,該相關(guān)信息僅為宣傳及傳遞更多信息之目的,不代表本網(wǎng)站觀點(diǎn),文章真實(shí)性請(qǐng)瀏覽者慎重核實(shí)!任何投資加盟均有風(fēng)險(xiǎn),提醒廣大民眾投資需謹(jǐn)慎!

            網(wǎng)站簡(jiǎn)介 - 聯(lián)系我們 - 營(yíng)銷(xiāo)服務(wù) - 老版地圖 - 版權(quán)聲明 - 網(wǎng)站地圖
            Copyright.2002-2019 寧夏資訊網(wǎng) 版權(quán)所有 本網(wǎng)拒絕一切非法行為 歡迎監(jiān)督舉報(bào) 如有錯(cuò)誤信息 歡迎糾正
            夏河县| 海阳市| 惠州市| 永安市| 平湖市| 宝坻区| 荆州市| 广南县| 海宁市| 南投县| 临漳县| 阳东县| 邵武市| 镇江市| 安徽省| 旬阳县| 小金县| 清镇市| 龙门县| 林州市| 松滋市| 甘南县| 陆良县| 长沙县| 柞水县| 施甸县| 河南省| 承德市| 宁明县| 阿克苏市| 江口县| 沙雅县| 乌苏市| 山西省| 图木舒克市| 启东市| 深泽县| 曲麻莱县| 延津县| 邓州市| 屏山县|