基于百度AI接口的微信小程序-數(shù)字識別

效果圖
  • 代碼詳情:
    1. wxml
         <view class="container">
            <camera
                 class="camera"
                 device-position="back"
                 flash="off"
                 binderror="error"></camera>
             <view class="button" catchtap="numberRecognition">識別</view>
           <!-- 遮罩 -->
             <view wx:if="{{isShow}}" class='cover'>
               <view class='cover_child'>
                 <text class="child-title">識別結(jié)果</text>
                 <block wx:for="{{numberList}}">
                   <view class="child-row">
                     <text>{{item.title}}</text>
                     <text>{{item.number}}</text>
                   </view>
                 </block>
               </view>
               <image catchtap="hideCover" class="cross" src="../../images/cancel.png " />
               <view catchtap="hideCover" wx:if="{{isShow}}" class='bg'></view>
             </view>
         </view>
  1. wxss
         page {
           height: 100%;
           width: 100%;
         }
         
         .container {
           height: 100%;
           width: 100%;
           display: flex;
           flex-direction: column;
           align-items: center;
         }
         /* 相機 */
         .camera {
           width: 100%;
           height: 100%;
         }
         /* 識別按鈕 */
         .button{
           position: absolute;
           margin-top: 1000rpx;
           background: white;
           opacity: 0.5;
           width: 200rpx;
           height: 200rpx;
           border-radius: 50%;
           text-align: center;
           line-height: 200rpx;
           font-weight: bold;
           font-size: 44rpx;
         }
         
         /* 遮罩樣式 */
         
         .bg {
           position: fixed;
           top: 0;
           left: 0;
           width: 100%;
           height: 100%;
           background: rgba(0, 0, 0, 0.5);
         }
         
         .cover {
           width: 100%;
           height: 100%;
           position: fixed;
           top: 0;
           left: 0;
           display: flex;
           justify-content: center;
           align-items: center;
           z-index: 0;
         }
         
         .cover_child {
           width: 600rpx;
           height: 830rpx;
           background: rgba(255, 255, 255, 1);
           border-radius: 20rpx;
           display: flex;
           flex-direction: column;
           z-index: 5;
         }
         
         .child-title {
           white-space: nowrap;
           margin-left: 43rpx;
           margin-top: 32rpx;
           width: 137rpx;
           height: 32rpx;
           font-size: 34rpx;
           font-weight: bold;
           color: rgba(18, 90, 217, 1);
           line-height: 36rpx;
           margin-bottom: 31rpx;
         }
         
         .child-row {
           display: flex;
           flex-direction: row;
           margin-left: 41rpx;
           margin-top: 40rpx;
           height: 28rpx;
           font-size: 30rpx;
           font-weight: 500;
           color: rgba(3, 3, 3, 1);
           line-height: 36rpx;
         }
         
         .cross {
           margin-bottom: 110rpx;
           bottom: 0rpx;
           position: fixed;
           width: 60rpx;
           height: 60rpx;
           z-index: 5;
         }
  1. javascript
         Page({
           data: {
             src: "",
             token: "",
             base64: "",
             isShow: false,
             numberList: []
           },
           numberRecognition(){
             wx.showLoading({
               title: '識別中...',
             })
             this.takePhoto();
             this.uploadAndRecognition();
           },
           //拍照
           takePhoto() {
             var that = this;
             //創(chuàng)建拍照上下文對象
             const ctx = wx.createCameraContext()
             ctx.takePhoto({
               quality: 'high',
               //拍照成功
               success: (res) => {
                 //console.log(res)
                 wx.getFileSystemManager().readFile({
                   filePath: res.tempImagePath,
                   encoding: 'base64',
                   success: res => {
                     //console.log(res)
                     this.setData({
                       base64: res.data
                     })
                   },
                   //拍照失敗
                   fail: err => {
                     console.log(err)
                     this.showToast();
                   }
                 })
               },
               fail: err => {
                 this.showToast();
               }
             })
           },
         
           //上傳識別
           uploadAndRecognition() {
             //調(diào)用接口薄风,獲取token
             wx.request({
               url: 'https://aip.baidubce.com/oauth/2.0/token', //百度智能云相關的接口地址
               data: {
                 grant_type: 'client_credentials',
                 client_id: 'xxxxxxxxxxxxxxxxxxxxx',//用你創(chuàng)建的應用的API Key
                 client_secret: 'xxxxxxxxxxxxxxxxxxxxx'//用你創(chuàng)建的應用的Secret Key
               },
               header: {
                 'Content-Type': 'application/json' // 默認值
               },
               //獲取成功之后
               success: res => {
                 console.log(res)
                 wx.request({
                   url: 'https://aip.baidubce.com/rest/2.0/ocr/v1/numbers?access_token=' + res.data.access_token,
                   method: 'POST',
                   data: {
                     //所有圖片均需要base64編碼、去掉編碼頭后再進行urlencode
                     image: decodeURI(this.data.base64),
                   },
                   header: {
                     'Content-Type': 'application/x-www-form-urlencoded'
                   },
                   success: res => {
                     console.log(res.data)
                     let length = res.data.words_result_num;
                     console.log(length)
                     if (length > 0){
                       for (let i = 0; i < length;i++){
                         let tempDataList = {
                           title:"第"+i+"個:",
                           number: res.data.words_result[i].words
                         } 
                         console.log(tempDataList)
                         let temp = "numberList["+ i +"]";
                         console.log(temp)
                         this.setData({
                           [temp]:tempDataList
                         })
                       }
                       this.showCover();
                       console.log(this.data.numberList)
                     }else{
                       wx.showModal({
                         content: '未識別到數(shù)字',
                       })
                     }
                   },
                   fail: err => {
                     console.log(err)
                     this.showToast();
                   }
                 })
               },
               fail:err=>{
                 console.log(err)
                 this.showToast();
               },
               complete:()=>{
                 wx.hideLoading();
               }
             })
           },
           //隱藏遮罩
           hideCover() {
             this.setData({
               isShow: false
             })
           },
           //顯示遮罩
           showCover() {
             this.setData({
               isShow: true
             })
           },
           //封裝的wx.showToast
           showToast() {
             wx.showToast({
               title: "服務異常翩概,請稍后重試",
               icon: 'none',
               duration: 3000
             })
           }
         })
  1. json
{
  "navigationStyle":"custom"
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末涉馁,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子峭火,更是在濱河造成了極大的恐慌毁习,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,548評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件卖丸,死亡現(xiàn)場離奇詭異纺且,居然都是意外死亡,警方通過查閱死者的電腦和手機稍浆,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評論 3 399
  • 文/潘曉璐 我一進店門载碌,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人衅枫,你說我怎么就攤上這事嫁艇。” “怎么了弦撩?”我有些...
    開封第一講書人閱讀 167,990評論 0 360
  • 文/不壞的土叔 我叫張陵步咪,是天一觀的道長。 經(jīng)常有香客問我益楼,道長猾漫,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,618評論 1 296
  • 正文 為了忘掉前任感凤,我火速辦了婚禮悯周,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘陪竿。我一直安慰自己禽翼,他們只是感情好,可當我...
    茶點故事閱讀 68,618評論 6 397
  • 文/花漫 我一把揭開白布族跛。 她就那樣靜靜地躺著捐康,像睡著了一般。 火紅的嫁衣襯著肌膚如雪庸蔼。 梳的紋絲不亂的頭發(fā)上解总,一...
    開封第一講書人閱讀 52,246評論 1 308
  • 那天,我揣著相機與錄音姐仅,去河邊找鬼花枫。 笑死刻盐,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的劳翰。 我是一名探鬼主播敦锌,決...
    沈念sama閱讀 40,819評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼佳簸!你這毒婦竟也來了乙墙?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,725評論 0 276
  • 序言:老撾萬榮一對情侶失蹤生均,失蹤者是張志新(化名)和其女友劉穎听想,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體马胧,經(jīng)...
    沈念sama閱讀 46,268評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡汉买,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,356評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了佩脊。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蛙粘。...
    茶點故事閱讀 40,488評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖威彰,靈堂內(nèi)的尸體忽然破棺而出出牧,到底是詐尸還是另有隱情,我是刑警寧澤歇盼,帶...
    沈念sama閱讀 36,181評論 5 350
  • 正文 年R本政府宣布舔痕,位于F島的核電站,受9級特大地震影響旺遮,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜盈咳,卻給世界環(huán)境...
    茶點故事閱讀 41,862評論 3 333
  • 文/蒙蒙 一耿眉、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧鱼响,春花似錦鸣剪、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,331評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至江滨,卻和暖如春铛纬,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背唬滑。 一陣腳步聲響...
    開封第一講書人閱讀 33,445評論 1 272
  • 我被黑心中介騙來泰國打工告唆, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留棺弊,地道東北人。 一個月前我還...
    沈念sama閱讀 48,897評論 3 376
  • 正文 我出身青樓擒悬,卻偏偏與公主長得像模她,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子懂牧,可洞房花燭夜當晚...
    茶點故事閱讀 45,500評論 2 359