国产精品三级国产专区,国产欧美二区亚洲综合,欧美精品专区一中文字在线观看,国产免费A∨片在线播放

  • 
    
  • <mark id="t4lup"><thead id="t4lup"><input id="t4lup"></input></thead></mark>

    <s id="t4lup"></s>

      湖北企業(yè)新聞網(wǎng),歡迎您!

      幫助中心 廣告聯(lián)系

      網(wǎng)站關(guān)鍵詞: 湖北企業(yè)新聞網(wǎng)

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

      來源:時間:2020-11-19 07:30:09 閱讀:-

      百度云開發(fā)注冊與配置

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

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

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

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


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

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

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

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

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

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

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

      const cloud = require('wx-server-sdk')

      let AipOcrClient = require("baidu-aip-sdk").ocr;

      const args = require("conf.js");

      cloud.init();

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

      exports.main = async (event, context) => {

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

      let APP_ID = args.APP_ID;

      let API_KEY = args.API_KEY;

      let SECRET_KEY = args.SECRET_KEY;

      // 新建一個對象,保存一個對象調(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("base64");

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

      return client.generalBasic(image);

      //console.log(result);

      // .then(function (result) {

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

      // return result;

      // })

      }

      小程序客戶端開發(fā)

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

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

      let tempFiles;

      let tempFilePaths;

      wx.chooseImage({

      count: 1,

      sizeType: ['compressed'],

      sourceType: ['album', 'camera'],

      success:res=>{

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

      tempFiles = res.tempFiles[0].size;

      tempFilePaths = res.tempFilePaths[0];

      if (tempFiles > 3000000) {//大于3m

      wx.showToast({

      title: '圖片大小大于3M',

      icon: 'none',

      duration: 2000

      });

      return;

      }

      wx.showLoading({

      title: '識別中'

      });

      this.uplaodF(tempFilePaths);

      setTimeout(function () {

      wx.hideLoading();

      }, 3000);

      }

      });

      },

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

      let ctx = wx.createCameraContext();

      ctx.takePhoto({

      quality: "normal",

      success: (res) => {

      let tempFilePaths = res.tempImagePath;

      this.setData({

      camera: false

      });

      wx.showLoading({

      title: '識別中'

      });

      this.uplaodF(tempFilePaths);

      setTimeout(function () {

      wx.hideLoading();

      }, 3000);

      }

      });

      },

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

      uplaodF(path){

      let result = false;

      let name = path.substring(path.lastIndexOf('/') + 1, path.lastIndexOf('.'));

      wx.cloud.uploadFile({

      cloudPath: name,

      filePath: path, // 文件路徑

      }).then(res => {

      // get resource ID

      let id = res.fileID;

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

      wx.cloud.callFunction({

      name: 'tongyong',

      data: {

      fileID: id

      }

      }).then(res => {

      let result = res.result.words_result;

      if (result.length > 0) {

      let arr = '';

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

      arr += result[i].words

      }

      this.setData({

      words_result: arr

      })

      }else{

      this.setData({

      words_result: ''

      })

      }

      //刪除圖片

      wx.cloud.deleteFile({

      fileList: [id]

      }).then(res => {

      // handle success

      }).catch(error => {

      // handle error

      })

      }).catch(err => {

      console.log(err)

      });


      }).catch(error => {


      });

      },

      推薦閱讀:西安之聲