• 微信小程序对接串口摄像头


    串口摄像头由树莓派控制,代码如下:

    # _*_ coding:utf-8
    import serial
    import time
    import traceback
    import pycurl
    import requests
    import io
    import json
    import base64
    
    ser = serial.Serial("/dev/ttyAMA0",115200)
    print(ser.name)
    print(ser.port)
    
    f=None
    
    def 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()
                upload()
            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 resp
    
    def getCurrentLength():
        bitarray = [0x56,0x00,0x34,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>=9:
                break;
        return resp
    def 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 resp
    def 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 resp
    def 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())

    微信小程序代码如下:

    wxml

        <view 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;100%" >
          <image src='{{userImageBase64}}'  mode="widthFix" style="100%;background-color: #66ffff;"/>
        </view>

    js

    const 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
              }
              ),
              //修改标题栏
              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 });
               
              }
            });
    
          }
        });
      },
      
    })
  • 相关阅读:
    spring中配置quartz
    理解Quartz触发器
    spring事物策略
    lucene 的关键字变色 与排序
    spring中配置quartz
    lucene 的关键字变色 与排序
    Quartz CronTrigger时间最完整配置说明
    理解Quartz触发器
    Quartz CronTrigger时间最完整配置说明
    linux vps
  • 原文地址:https://www.cnblogs.com/wuchaodzxx/p/8708325.html
Copyright © 2020-2023  润新知