微信小程序实现自定义摄像头(在摄像头内添加提示信息)
摄像头组件(wxml)
<!-- 拍照功能 自定义摄像头
bindtap:takePhotoDepot----从图库获取
bindtap:takePhoto----拍照
bindtap:takeFrontBack---转换摄像头
-->
<view wx:if="{
{useCameraTakePhoto}}" class="scan_view">
<!-- 摄像头组件 -->
<camera device-position="back" mode="normal" flash="off" binderror="error" device-position="{
{phopo_camera?'front':'back'}}" style="width: 100%; height: 100vh;">
<!-- 摄像头返回按钮 -->
<cover-view class="camera_view">
<cover-view class="back" bindtap="closeCamera">
返回
</cover-view>
<cover-view style="overflow: hidden;margin-top: 80vh;">
<cover-image bindtap="takePhotoDepot" class="take_photo_depot"src="../../images/phopo.png"></cover-image>
<cover-image bindtap="takePhoto" class="take_photo" src="../../images/starCamera.png"></cover-image>
<cover-image bindtap="takeFrontBack" class="take_photo_frontBack" src="../../images/transition.png"></cover-image>
</cover-view>
</cover-view>
</camera>
</view>
唤醒摄像头组件(wxml)
<view class="conPhopo_camera" bindtap="changePhoto">
<text>拍摄照片</text>
</view>
自定义展示拍照后的图片(wxml)
<view style="width:100%; overflow: hidden;" wx:if="{
{tempFileList.length>0}}">
<!-- 图片缩小状态 -->
<view style="position: relative;width: 80rpx;height: 120rpx;float: left;margin-right: 10rpx; margin-top: 10rpx;" wx:for="{
{tempFileList}}" wx:key="index">
<view class="fileShow" wx:if="{
{item.type ==='image' && item.thumb}}">
<image style="display: inline-block;width: 80rpx;height: 120rpx; " src="data:image/png;base64,{
{item.thumb}}" alt="" bindtap="changeMinImage" data-item="{
{item}}" data-index="{
{index}}" data-src='{
{item.thumb}}' data-thumb='true' />
</view>
<view class="fileShow" wx:elif="{
{item.type ==='image' &&item.path}}">
<image style="display: inline-block;width: 80rpx;height: 120rpx; margin-top: 10rpx;" src="{
{item.path}}" alt="" bindtap="changeMinImage" data-item="{
{item}}" data-index="{
{index}}" data-src='{
{item.path}}' />
</view>
</view>
<!-- 点击缩小图片放大展示-->
<view wx:if="{
{isSrc}}" style="position: absolute;left:0;top: 0; width: 100vw;height: 100vh;background-color: rgba(0, 0, 0, 0.8);padding-top: 20%;">
<view wx:if="{
{thumb=='true'}}">
<image style="display: block; width: 80%;height: 80%;margin: 0 auto;opacity: 1; background-color: #09ce9a;" src="data:image/png;base64,{
{src.src}}" alt="" />
</view>
<view wx:elif="{
{thumb!='true'}}">
<image style="display: block; width: 80%;height: 80%;margin: 0 auto;opacity: 1; background-color: aqua;" src="{
{src.src}}" />
</view>
<i class="iconfont .icon-quxiao" bindtap="changeMaxImage" style="position: absolute;left: 20rpx;top: 20rpx;color: #fff;font-size: 44rpx;"></i>
</view>
</view>
接下来就是关键了,主要还得看js怎么实现
实现的方法
打开摄像头并且拍照
// 控制摄像头是否显示
changePhoto(e) {
const {
currentTab,
isVideoModel,
} = this.data;
let casePhotoList = this.data.casePhotoList;
let facePhotoList = this.data.facePhotoList;
let abnormalPhotoList = this.data.abnormalPhotoList;
let accessoryPhotoList = this.data.accessoryPhotoList;
const that = this;
if (currentTab == 2) {
// 摄像开始
wx.chooseVideo({
count: 1,
mediaType: ['video'],
sourceType: ['camera'],
success: (res) => {
// 添加formData
let formData = new FormData();
formData.append('type', currentTab);
let src = {
tempVideoPath: res.tempFilePath,
size: res.size
};
abnormalPhotoList.push(src);
that.setData({
abnormalPhotoList: abnormalPhotoList,
useCameraTakePhoto: false,
isVideoModel: !isVideoModel,
});
for (const iterator of abnormalPhotoList) {
formData.appendFile('files[]', iterator.tempVideoPath);
}
let tempFilesPath = abnormalPhotoList.map(item => ({
type: item.type ? item.type : 'video', // 文件类型
path: item.tempVideoPath, // 文件本地路径
size: item.size ? item.size : '', // 文件大小
}))
let {
videoTempFileList
}