微信camera拍照组件的使用(uniapp小程序)代码可直接复制看效果

news/2025/2/16 6:37:14/

微信camera官方文档:https://developers.weixin.qq.com/miniprogram/dev/component/camera.html

  1. html

整体效果 样式可以自行定义的一个拍照组件 未找到摄像头是因为台式机电脑没有摄像头 真机测试可以使用

<view class="tackpic"><view class="font-title" v-if="!setData.src"><view>对准文档 拍照扫描</view></view><view class="yulan-box" v-if="setData.src"><image :src="setData.src" mode=""></image><button class="successBtn" @click="submitForm">完成</button></view><camera device-position="back" flash="off" @error="cameraError"style="width: 100%;height: calc(100vh - 240rpx)"></camera><view v-if="setData.src==''" class="pic-bot"><view class="left" @click="takealbum"><u-icon name="photo" size="50" color="#fff"></u-icon></view><view class="center" @click="takePhoto"><view class="takeout"><view class="takein"></view></view></view></view></view>

2)方法

cameraError方法进入拍照页面时会提醒授权相机。如果用户点击不同意授权就会打回上一页,当再进入这个页面时会提醒让用户开启授权

uploadImgFile为uni自带的上传图片aip

takealbum方法点击时调用uniapp的从相册选择图 ()sourceType: 为‘album’从相册获取,为‘camera’时直接拍照,什么都不填写时默认两种选择

methods: {cameraError() {wx.showModal({title: '提示',content: '请开启摄像头权限,否则无法拍照',confirmText: '去开启',success(res) {if (res.confirm) {wx.openSetting({success(res) {if (res.authSetting["scope.camera"]) {// 重点是这里,再次允许授权后需要刷新一下页面,camera组件才会出来wx.redirectTo({url: '/pages_home/components/takePictures'})} else {wx.navigateBack({delta: 1})}}})} else if (res.cancel) {console.log('用户点击取消')wx.navigateBack({delta: 1})}}})},//添加防抖--提交submitFormFn() {let that = thisthat.uploadImgFile(this.setData.src, that)},submitForm() {if (this.timeout) {clearTimeout(this.timeout);}this.timeout = setTimeout(this.submitFormFn, 1000);},uploadImgFile(filePath, that) {uni.uploadFile({url: `${process.uniEnv.baseUrl}/baseVinCode/getVinCodeByImg`,filePath: filePath,name: 'file',formData: {file: filePath},header: {'Content-Type': 'multipart/form-data',},success: response => {let res = JSON.parse(response.data.toString("utf8"));const {data,code} = res;if (code == 200) {uni.redirectTo({url: `/pages_home/photoIdentification?imageSrc=${filePath}&vinCode=${data}`});} else {uni.showToast({title: res.msg,duration: 3000});}},complete: () => {uni.hideLoading()}});},takePhoto() {const ctx = wx.createCameraContext()ctx.takePhoto({quality: 'high',success: (res) => {console.log(res, '------42');
//res.tempImagePath获取点击拍照后的图片路径 然后赋值给image标签显示图片。点击拍照图片就会存入手机相册this.setData.src = res.tempImagePath}})},takePic() {},takealbum() {this.chooseImage('album')},chooseImage(sourceType) {const that = thisuni.chooseImage({count: 1,sizeType: ['original', 'compressed'],sourceType: [sourceType],success: (res) => {if (res.tempFiles[0]['size'] > 20 * 1024 * 1024) {uni.showToast({title: '图片大小不能超过20M',icon: 'none',duration: 3000});return;}uni.showLoading({title: '上传中...'})if (res.tempFiles[0]['size'] < 5 * 1024 * 1024) {that.uploadImgFile(res.tempFilePaths[0], that)} else {uni.compressImage({src: res.tempFilePaths[0],quality: 80,success: res => {that.uploadImgFile(res.tempFilePath, that)}})}}});},}

3)css样式

.successBtn {background-color: #05c160;padding: 4rpx 30rpx;color: #fff;height: 80rpx;position: absolute;bottom: 50rpx;right: 50rpx;display: flex;justify-content: center;align-items: center;border-radius: 10rpx;}.yulan-box {position: relative;width: 100%;height: 100%;}image {width: 100%;height: 100vh;}.tackpic {width: 100%;height: 100vh;background-color: #242424;.font-title {position: fixed;top: 3%;background-color: transparent;width: 100%;height: 50rpx;color: #fff;display: flex;align-items: center;justify-content: center;z-index: 999;view {padding: 20rpx 30rpx;border-radius: 10rpx;font-size: 12px;background-color: rgba(0, 0, 0, 0.3);}}.pic-bot.data-v-1c7472ae {width: 100%;height: 240rpx;background-color: #242424 !important;position: relative;}}.pic-bot {width: 100%;display: flex;.left {width: 30%;display: flex;align-items: center;justify-content: center;}.center {width: 40%;padding: 20rpx;display: flex;align-items: center;justify-content: center;.takeout {width: 120rpx;height: 120rpx;border-radius: 50%;background-color: transparent;border: 3px solid #fff;display: flex;align-items: center;justify-content: center;position: absolute;bottom: 120%;.takein {width: 80rpx;height: 80rpx;border-radius: 40rpx;background-color: #fff;}}}.right {width: 30%;}}

http://www.ppmy.cn/news/701033.html

相关文章

520微信代码轰炸

写一个脚本&#xff0c;在520那天发给你的小可爱。 # -*- coding : utf-8 -*- # Time : 2022/5/19 13:36 # Author : wkb import time,os import pyautogui,pyperclip time.sleep(5) for i in range(10):#pyautogui.click(662,748)pyperclip.copy("代码轰炸&#…

Python趣味代码(一):微信信息轰炸

1.安装模块 首先需要在电脑上安装好pyautogui、pyperclip两个模块 # Windos系统安装命令 pip install pyautogui pip install pyperclip# Mac系统安装命令 pip3 install pyautogui pip3 install pyperclip 2.功能实现 将整个流程分为三大块&#xff1a; 一、获取发信内容 二、…

uniapp微信小程序授权登录流程(代码直接复制可用)

1.写一个点击登录的按钮 <view tap"login">一键登录</view> 2.写点击事件 login() {wx.getUserInfo({success: (res) > {console.log(获取到openId, res);}})let that this;uni.getUserProfile({desc: 用于完善用户资料,lang: zh_CN,success: (res) …

爱心代码--C语言特供(可直接复制,亲测有效)

情人节到了&#xff0c;作为一名程序员&#xff0c;我们拥有属于我们的浪漫。 这里我总结了几种常见的爱心代码&#xff0c;简单易上手。 一.这是一种最为常见的爱心代码 #include<stdio.h> #include<Windows.h>int main() {float x, y, a;for (y 1.5; y > -1.…

微信小程序页面跳转方式+跳转小程序(直接复制代码可用)

一. 微信小程序跳转页面方法 1.跳转到 tabBar 页面 wx.switchTab({url: /index }) 2.跳转到其他页面&#xff08;非tabBar页&#xff09; //redirectTo方法&#xff08;会关闭当前页面&#xff09; wx.redirectTo({url: /page//页面路径 })//navigateTo方法&#xff08;不会…

微信8.0自动发送炸弹python脚本

import itchat import argparsedef get_arguments():parser argparse.ArgumentParser(description文献表情包发送)parser.add_argument(--name, typestr, default"xxx",help要发送的人的微信备注)parser.add_argument(--group, typestr, default"person",…

NumPy实现逻辑回归

说明&#xff1a;数据集 ex2data1.txt是吴恩达机器学习的作业的数据集。 # -*-coding:utf-8-*- import matplotlib.pyplot as plt import numpy as np import pandas as pdclass Logitstic_Regression:def __init__(self, learning_rate0.01, num_iterations75000, threshold0.…

Charles 乱码问题解决

❤️ 前段时间发现了一个 人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;分享一下给大家。&#x1f448;&#x1f3fb; 学习链接 Charles 乱码问题解决 乱码显示 解决方法 修改Charles.ini 文件&#xff08;可选&#xff09;&#xff1b; vmarg.5-Df…