E企盈营销工具技术服务商 热线:4006-838-530

微信小程序对接串芮城县E企盈小程序口摄像头

串口摄像头由树莓派控制,代码如下:# _*_ coding:utf-8 import serialimport timeimport tracebackimport pycurlimport requestsimport ioimport jsonimport base64ser = serial.Serial("/dev/ttyAMA0",115200)print(ser.name)print(ser.port)f=Nonedef main():    print("main:")    res0 = setResolution()    res0 = reset()    print(res0)    while True:         try:            res1=stopCurrent()            print(res1)            res2=getCurrentLength()            print(res2)                hBit = res2[7]8            lBit = res2[8]            imageSize = hBit+lBit;            dataSize = imageSize+10            print(imageSize)            res3=getCurrentImage(res2[7],res2[8],dataSize)            imageData = res3[5:(dataSize-5)]                f = open('/home/pi/Camera/image.jpg','wb')            for i in range(len(imageData)):                f.write(chr(imageData[i]))            f.close            resumCurrent()            开通粽子券小程序电话:4006-838-530upload()        except Exception:            print(traceback.print_exc())def setResolution():    bitarray = [0x56,0x00,0x31,0x05,0x04,0x01,0x00,0x19,0x11]    ser.write(bitarray)    time.sleep(0.1)    index=0    resp=[]    while True:        s = ser.read()        if s!=None:            index = index+1            resp.append(ord(s))        if index=5:            break;    return resp  def setCompress():    bitarray = [0x56,0x00,0x31,0x05,0x01,0x01,0x12,0x04,0x36]    ser.write(bitarray)    time.sleep(0.1)    index=0    resp=[]    while True:        s = ser.read()        if s!=None:            index = index+1            resp.append(ord(s))        if index=5:            break;    return resp  def resumCurrent():    bitarray = [0x56,0x00,0x36,0x01,0x03]    ser.write(bitarray)    time.sleep(0.1)    index=0    resp=[]    while True:        s = ser.read()        if s!=None:            index = index+1            #print(s)             resp.append(ord(s))        if index=5:            break;    return resp  def stopCurrent():    bitarray = [0x56,0x00,0x36,0x01,0x00]    ser.write(bitarray)    time.sleep(0.1)    index=0    resp=[]    while True:        s = ser.read()        if s!=None:            index = index+1            #print(s)             resp.append(ord(s))        if index=5:            break;    return respdef getCurrentLength():    bitarray = [0x56,0x00,0x34,0x01,0x00]    ser.write(bitarray)    time.sleep(0.1)    index=0    res开通手拿包小程序电话:4006-838-530p=[]    while True:        s = ser.read()        if s!=None:            index = index+1            #print(s)             resp.append(ord(s))        if index=9:            break;    return respdef getCurrentImage(hBit,lBit,dataSize):    bitarray = [0x56,0x00,0x32,0x0C,0x00,0x0A,0x00,0x00,0x00,0x00,0x00,0x00,chr(hBit),chr(lBit),0x10,0x00]    ser.write(bitarray)    time.sleep(0.1)    index=0    resp=[]    f=io.BytesIO()    while True:        s = ser.read()        if s!=None:            index = index+1            #print(s)             resp.append(ord(s))            #f.write(s) if index=dataSize:            break;    return respdef reset():    bitarray = [0x56,0x00,0x26,0x00]    ser.write(bitarray)    time.sleep(2)    index=0    resp=[]    len = ser.inWaiting()    s = ser.read(len)    resp.append(s)    return respdef upload(device=27390392):    url='http://api.heclouds.com/bindata'    headers={"api-key":"*******","Content-Type":"image/jpg"}    queryString = {"device_id":str(device),"datastream_id":"picture"}    with open('/home/pi/Camera/image.jpg','rb') as f:    base64_data = base64.b64encode(f.read())        r = requests.post(url,params=queryString,headers=headers,data=base64_data)        print(r.text)        return f    if __name__ == '__main__':    try:        main()    except KeyboardInterrupt:        if ser != None:            ser.close()    except Exception:        print(traceback.print_exc())

微信小程序代码如下:wxmlview class="weui-flex justify" button bindtap="receivePic" class="weui_btn mini_btn" style="justify-content: center;" stype="plain-default" size="mini"刷图/button /view view style="height:20px;" /view view style="weui-flex justify;width:100%" image src='{{userImageBase64}}'  mode="widthFix" style="width:100%;background-color: #66ffff;"/ /view

jsconst util = require('../../utils/util.js')Page({  data: {    device: {},    userImageBase64:{},  },  onLoad: function (options) {    var _this = this;    console.log(options.device_id);    //查询该设备的详细信息     wx.request({      url: 'https://api.heclouds.com/devices/' + options.device_id, //仅为示例,并非真实的接口地址       header: {        'content-type': 'application/json', // 默认值        'api-key': '**'      },      success: function (res) {        console.log(res.data),          //执行this.setData方法,就会渲染视图数据           _this.setData({            device: res.data.data          }开通保温杯小程序电话:4006-838-530          ),          //修改标题栏           wx.setNavigationBarTitle({ title: _this.data.device.title })      }    });      receivePic:function(){    var _this = this;    wx.request({      url: 'https://api.heclouds.com/devices/' + this.data.device.id + '/datapoints',      header: {        'content-type': 'application/json', // 默认值        'api-key': '**'      },      success: function (res) {        var __this = _this;        console.log("PIC")        var index = res.data.data.datastreams[1].datapoints[0].value.index;        console.log(res);        console.log(index)        wx.request({          url: 'https://api.heclouds.com/bindata/'+index,          header: {            'content-type': 'image/jpg', // 默认值            'api-key': '**'          },          success: function (res) {            console.log("PIC")            var image = res.data;             //var a = image.split('');             console.log(image);            __this.setData({ userImageBase64: "data:image/png;base64," + image });                     }        });      }    });  },  })

作者:且听风吟-wuchao链接:微信小程序对接串口摄像头 – 且听风吟-wuchao – 博客园著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

赞(0) 打赏
未经允许不得转载:E企盈小程序开发-热线:4006-838-530 » 微信小程序对接串芮城县E企盈小程序口摄像头
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

模王系统开发

联系我们升级日志

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏