微信小程序使用百度 API 实现识别车牌号

一、打开浏览器,搜索百度云

1e4a7509445e45768c59e8cf5acd58ac.png二、进入官网

7314ffe597cd4755a2d62ca4fb0afdc8.png

三、首先登录,登录完点击控制台,搜索文字识别

2ffe1c39f7ce4d5d80505058361a47d2.png四、点击立即使用

b1be22801e1048ae80457c9aa056b015.png五、选择交通场景,找到车牌识别

1527fcfead0d40718f6c8265a47764b9.png这里的话每个月免费赠送1000次

实现代码如下:

Page({

  data: {

    baiduToken: '',

    captureInterval: null,  // 用于存储定时器的引用  

    lastCapturedTime: 0,    // 上次拍照的时间戳  

    captureIntervalTime: 3000, // 拍照间隔,单位毫秒  

    licensePlate: ''

  },

  onLoad: function(options) {

    this.getBaiduToken();

    this.startAutoCapture();

  },

  onUnload: function() {  

    // 清理定时器,避免内存泄漏  

    if (this.data.captureInterval) {  

      clearInterval(this.data.captureInterval);  

      this.setData({  

        captureInterval: null  

      });  

    }  

  },

  // 获取百度access_token

  getBaiduToken: function() {

    const apiKey = '';

    const seckey = '';

    const tokenUrl = ``;

    let that = this;

    wx.request({

      url: tokenUrl,

      method: 'POST',

      dataType: 'json',

      header: {

        'content-type': 'application/json; charset=UTF-8'

      },

      success: function(res) {

        console.log('getBaiduToken提示pass', res);

        that.setData({

          baiduToken: res.data.access_token

        })

      },

      fail: function(res) {

        console.log('getBaiduToken提示fail', res);

      }

    })

  },

  startAutoCapture: function() {  

    if (!this.data.captureInterval) {  

      this.data.captureInterval = setInterval(() => {  

        const now = new Date().getTime();  

        if (now - this.data.lastCapturedTime >= this.data.captureIntervalTime) {  

          this.captureImage();  

          this.setData({  

            lastCapturedTime: now  

          });  

        }  

      }, 1000); // 这里设置为1000毫秒检查一次,但实际拍照间隔由 captureIntervalTime 控制  

    }  

  },

  captureImage: function() {  

    let ctx = wx.createCameraContext();  

    ctx.takePhoto({  

      quality: 'high',  

      success: (res) => {  

        wx.getFileSystemManager().readFile({  

          filePath: res.tempImagePath,  

          encoding: 'base64',  

          success: res => {  

            this.scanLicensePlate(res.data);  

          },  

          fail: console.error  

        });  

      }  

    });  

  },

  //扫描图片中的车牌号(假设的API端点和参数)  

scanLicensePlate: function(imageData) {  

  let that = this;  

  // 假设你已经有了一个有效的access_token  

  if (!that.data.baiduToken) {  

      console.error('No valid access token found!');  

      return;  

  }  

  const licensePlateUrl = 'https://yourserver.com/rest/2.0/ocr/v1/license_plate?access_token=' + that.data.baiduToken;  

  wx.showLoading({  

      title: '识别中...',  

  });  

  wx.request({

      url: licensePlateUrl,  

      method: 'POST',  

      data: {  

          image: imageData,  

      },  

      header: {  

          'Content-Type': 'application/x-www-form-urlencoded',

      },  

      success: function(res) {  

          wx.hideLoading();  

          if (res.statusCode === 200) {  

              console.log('车牌识别成功', res.data);

              that.setData({

                licensePlate:res.data.words_result.number

              }) 

          } else {  

              console.error('车牌识别失败', res.data);  

          }  

      },  

      fail: function(err) {  

          wx.hideLoading();  

          console.error('请求失败', err);  

      } 

  });  

},

})

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值