首页 > 热点 > 微信 > 正文

微信小程序实现拍照画布指定区域生成图片

2024-07-22 01:17:58
字体:
来源:转载
供稿:网友

最近写识别行驶证功能,点击拍照把指定区域截取,生成图片功能。

系统相机。该组件是原生组件,使用时请注意相关限制。 扫码二维码功能,需升级微信客户端至6.7.3。

微信小程序Camera相机地址

我们看下效果:

1、首先生成一个CanvasContext:

/** * 生命周期函数--监听页面加载 */ onLoad: function(options) { requireJs.adaptionIphoneX(this);  this.ctx = wx.createCameraContext()  },

2、相机的 wxml样式

<camera wx:if='{{isShowCamera}}' device - position="width" flash="off" style="width:{{windowWidth}}px; height:{{windowHeight}}px;">  <cover-view class='camerabgImage-view'>  <cover-image class='bgImage' src='{{isIphoneX==true?"../../myImage/vehicle/biankuang_x.png":"../../myImage/vehicle/biankuang.png"}}'> </cover-image>   <cover-view class='cameratop-view1'>中华人民共和国机动车行驶证</cover-view>  <cover-view class='cameratop-view2'>(行驶证主页)</cover-view>  <cover-view class='cameracenter-view' style='top:{{isIphoneX==true?"52%":"62%"}}'>请对准左下角发证机关印章</cover-view>   <!-- 拍照按钮 -->   <cover-view class='camerabotton-view' style='bottom:{{isIphoneX==true?"75px":"0px"}}'>  <cover-image class='cancelphoto' src='../../myImage/vehicle/quxiao.png' bindtap='cancelPhotoAction'></cover-image>  <cover-image class='takephoto' src='../../myImage/vehicle/paizhao.png' bindtap='takePhotoAction'></cover-image>    <cover-view class='skipphoto' bindtap='skipphotoAction'>{{skipphotoStatus==1?"跳过":""}}  </cover-view>    </cover-view>  </cover-view>  </camera>  <canvas wx:if='{{isShowImage}}' canvas-id="image-canvas" style='width:{{windowWidth}}px; height:{{windowHeight}}px;'></canvas>

3、相机的 wxss样式

.camerabgImage-view{ height: 100%; width: 100%; position:absolute;}.bgImage{ width: 100%; height: 100%; position: absolute;} .cameratop-view1{ margin-top: 174rpx;}.cameratop-view2{ margin-top: 220rpx;}.cameratop-view1, .cameratop-view2{ width: 100%; display: flex; justify-content: center; position: absolute;  font-family: PingFangSC-Medium; font-size: 36rpx; color: #FFFFFF; letter-spacing: 0; text-align: center;} .cameracenter-view{ height: 44rpx; width: 100%; position: absolute;  font-family: PingFangSC-Medium; font-size: 32rpx; color: #FFFFFF; letter-spacing: 0; text-align: center;} /* 底部 */.camerabotton-view{ height: 200rpx; width: 100%; position:absolute;  display: flex; justify-content: space-around; align-items: center;}.cancelphoto{ width: 50rpx; height: 50rpx;}.takephoto{ width: 132rpx; height: 132rpx;}.skipphoto{ font-family: PingFangSC-Regular; font-size: 32rpx; color: #FFFFFF; letter-spacing: 0; text-align: center;}            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表