This commit is contained in:
cun-nan 2024-05-28 09:07:24 +08:00
parent ed1c6f998e
commit 6a20bf7df3
5 changed files with 10789 additions and 0 deletions

View File

@ -0,0 +1,31 @@
## 1.3.02022-06-28
1. [修改] - 之前使用 canvas 在页面上展示画面,改为 video 标签在页面展示视频流画面。
2. [增强] - 现在只获取扫描框中的画面进行二维码识别,这样可有效的节省资源与提高识别效率。
3. [增强] - 提升调用摄像头的分辨率。
4. [增强] - 提升识别大二维码的能力,之前在低端机上识别大二维码容易识别不到。
5. [新增] - 新增错误事件返回,返回内部报错。错误说明请查看文档。
## 1.2.12022-06-01
新增闪光灯开关(只有在谷歌内核浏览器中显示,推荐微信内置浏览器)
新增https环境检测(总有人不看介绍,直接使用导致无法正常调用摄像头。)
更新文档
## 1.2.02022-06-01
新增闪光灯开关(只有在谷歌内核浏览器中显示,推荐微信内置浏览器)
新增https环境检测(总有人不看介绍,直接使用导致无法正常调用摄像头。)
## 1.1.02022-01-25
1.支持全屏 -
2.支持前置摄像头 -
3.支持高清调用摄像头 -
## 1.0.42021-12-24
更新插件说明
## 1.0.32021-12-21
希望大家多去我小程序中逛一些,就当支持作者了。谢谢大家,底下有小程序二维码。
## 1.0.22021-12-21
更新文档
## 1.0.12021-12-21
正式上线插件
## 1.0.02021-12-21
1.0.0

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,436 @@
<template>
<view class="canvasBox">
<template v-if="isUse">
<view class="box">
<view class="line"></view>
<view class="angle"></view>
</view>
<view class="box2" v-if="isUseTorch">
<view class="track" @click="openTrack">
<svg
t="1653920715959"
class="icon"
viewBox="0 0 1024 1024"
version="1.1"
xmlns="http://www.w3.org/2000/svg"
p-id="1351"
width="32"
height="32"
>
<path
d="M651.353043 550.479503H378.752795L240.862609 364.315031c-3.688944-4.897391-5.660621-10.876025-5.660621-17.045466v-60.040745c0-15.773416 12.847702-28.621118 28.621118-28.621118h502.459627c15.773416 0 28.621118 12.847702 28.621118 28.621118v59.977143c0 6.105839-1.971677 12.084472-5.660621 17.045466l-137.890187 186.228074zM378.752795 598.308571v398.024348c0 15.328199 12.402484 27.667081 27.667081 27.667081h217.266087c15.328199 0 27.667081-12.402484 27.66708-27.667081V598.308571H378.752795z m136.300124 176.942112c-14.564969 0-26.331429-11.76646-26.331428-26.331428v-81.283975c0-14.564969 11.76646-26.331429 26.331428-26.331429 14.564969 0 26.331429 11.76646 26.331429 26.331429v81.283975c0 14.564969-11.76646 26.331429-26.331429 26.331428zM512 222.608696c-17.554286 0-31.801242-14.246957-31.801242-31.801243V31.801242c0-17.554286 14.246957-31.801242 31.801242-31.801242s31.801242 14.246957 31.801242 31.801242v159.006211c0 17.554286-14.246957 31.801242-31.801242 31.801243zM280.932174 205.881242c-9.47677 0-18.889938-4.197764-25.122981-12.275279L158.242981 67.991056a31.864845 31.864845 0 0 1 5.597019-44.648944 31.864845 31.864845 0 0 1 44.648944 5.597018l97.502609 125.551305a31.864845 31.864845 0 0 1-5.597019 44.648944c-5.787826 4.579379-12.656894 6.741863-19.46236 6.741863zM723.987081 205.881242c-6.805466 0-13.674534-2.162484-19.462361-6.678261a31.794882 31.794882 0 0 1-5.597018-44.648944l97.566211-125.551304a31.794882 31.794882 0 0 1 44.648944-5.597019 31.794882 31.794882 0 0 1 5.597019 44.648944l-97.566211 125.551305c-6.360248 8.077516-15.709814 12.27528-25.186584 12.275279z"
fill="#ffffff"
p-id="1352"
></path>
</svg>
{{ trackStatus ? '关闭闪光灯' : '打开闪光灯' }}
</view>
</view>
<view class="mask1 mask" :style="'height:' + maskHeight + 'px;'"></view>
<view
class="mask2 mask"
:style="'width:' + maskWidth + 'px;top:' + maskHeight + 'px;height:' + canvasHeight + 'px'"
></view>
<view class="mask3 mask" :style="'height:' + maskHeight + 'px;'"></view>
<view
class="mask4 mask"
:style="'width:' + maskWidth + 'px;top:' + maskHeight + 'px;height:' + canvasHeight + 'px'"
></view>
</template>
<template v-else>
<slot name="error">
<view class="error">
<view class="on1">相机权限被拒绝请尝试如下操作</view>
<view>· 刷新页面后重试</view>
<view>· 在系统中检测当前App或浏览器的相机权限是否被禁用</view>
<view>· 如果依然不能体验建议在微信中打开链接</view>
</view>
</slot>
</template>
</view>
</template>
<script>
import jsQR from './jsQR.js'
export default {
props: {
continue: {
type: Boolean,
default: false // false true
},
exact: {
type: String,
default: 'environment' // environment user
},
size: {
type: String,
default: 'whole' // whole balf
},
definition: {
type: Boolean,
default: false // fasle true
}
},
data() {
return {
windowWidth: 0,
windowHeight: 0,
video: null,
canvas2d: null,
canvas2d2: null,
canvasWidth: 200,
canvasHeight: 200,
maskWidth: 0,
maskHeight: 0,
inter: 0,
track: null,
isUseTorch: false,
trackStatus: false,
isParse: false,
isUse: true
}
},
mounted() {
if (origin.indexOf('https') === -1) throw '请在 https 环境中使用摄像头组件。'
this.windowWidth = document.documentElement.clientWidth || document.body.clientWidth
this.windowHeight = document.documentElement.clientHeight || document.body.clientHeight
this.windowHeight = this.size === 'whole' ? this.windowHeight : this.windowHeight / 2
this.isParse = true
this.$nextTick(() => {
this.createMsk()
this.openScan()
})
},
destroyed() {
this.closeCamera()
},
methods: {
openScan() {
const width = this.transtion(this.windowHeight)
const height = this.transtion(this.windowWidth)
const videoParam = {
audio: false,
video: {
facingMode: { exact: this.exact },
width,
height
}
}
navigator.mediaDevices
.getUserMedia(videoParam)
.then(stream => {
this.video = document.createElement('video')
this.video.width = this.windowWidth
this.video.height = this.windowHeight
const canvas = document.createElement('canvas')
canvas.id = 'canvas'
canvas.width = this.transtion(this.canvasWidth)
canvas.height = this.transtion(this.canvasHeight)
canvas.style = 'display:none;'
//canvas.style = 'position: fixed;top: 0;z-index: 999;left:0'
this.canvas2d = canvas.getContext('2d')
//
const canvasBox = document.querySelector('.canvasBox')
canvasBox.append(this.video)
canvasBox.append(canvas)
canvasBox.style = `width:${this.windowWidth}px;height:${this.windowHeight}px;`
// canvas
const canvas2 = document.createElement('canvas')
canvas2.id = 'canvas2'
canvas2.width = this.canvasWidth
canvas2.height = this.canvasHeight
canvas2.style = 'position: absolute;top: 50%;left: 50%;z-index: 20;transform: translate(-50%, -50%);'
this.canvas2d2 = canvas2.getContext('2d')
canvasBox.append(canvas2)
this.video.srcObject = stream
this.video.setAttribute('playsinline', true)
this.video.play()
this.tick()
this.track = stream.getVideoTracks()[0]
setTimeout(() => {
this.isUseTorch = this.track.getCapabilities().torch || null
}, 500)
})
.catch(err => {
this.isUse = false
this.$emit('error', err)
})
},
closeCamera() {
this.isParse = false
if (this.video && this.video.srcObject) {
this.video.srcObject.getTracks().forEach(track => {
track.stop()
})
}
},
tick() {
if (!this.isParse) return
if (this.video.readyState === this.video.HAVE_ENOUGH_DATA) {
this.canvas2d.drawImage(
this.video,
this.transtion(this.maskWidth),
this.transtion(this.maskHeight),
this.transtion(200),
this.transtion(200),
0,
0,
this.transtion(this.canvasWidth),
this.transtion(this.canvasHeight)
)
const imageData = this.canvas2d.getImageData(
0,
0,
this.transtion(this.canvasWidth),
this.transtion(this.canvasHeight)
)
const code = jsQR(imageData.data, imageData.width, imageData.height, {
inversionAttempts: 'dontInvert'
})
this.canvas2d2.clearRect(0, 0, this.canvasWidth, this.canvasHeight)
if (code) {
this.drawLine(code.location.topLeftCorner, code.location.topRightCorner)
this.drawLine(code.location.topRightCorner, code.location.bottomRightCorner)
this.drawLine(code.location.bottomRightCorner, code.location.bottomLeftCorner)
this.drawLine(code.location.bottomLeftCorner, code.location.topLeftCorner)
if (code.data) {
this.getData(code.data)
}
}
}
requestAnimationFrame(this.tick)
},
drawLine(begin, end, color = '#FF3B58') {
this.canvas2d2.beginPath()
this.canvas2d2.moveTo(this.nutranstion(begin.x), this.nutranstion(begin.y))
this.canvas2d2.lineTo(this.nutranstion(end.x), this.nutranstion(end.y))
this.canvas2d2.lineWidth = 4
this.canvas2d2.strokeStyle = color
this.canvas2d2.stroke()
},
getData(data) {
this.$emit('success', data)
if (!this.continue) {
this.closeCamera()
}
},
openTrack() {
this.trackStatus = !this.trackStatus
this.track.applyConstraints({
advanced: [{ torch: this.trackStatus }]
})
},
createMsk() {
this.maskWidth = this.windowWidth / 2 - this.canvasWidth / 2
this.maskHeight = this.windowHeight / 2 - this.canvasHeight / 2
},
transtion(number) {
return this.definition ? number * 2.8 : number * 1.8
},
nutranstion(number) {
return this.definition ? number / 2.8 : number / 1.8
}
}
}
</script>
<style scoped>
page {
background-color: #333333;
}
.canvasBox {
width: 100vw;
height: 100vh;
position: relative;
background-image: linear-gradient(
0deg,
transparent 24%,
rgba(32, 255, 77, 0.1) 25%,
rgba(32, 255, 77, 0.1) 26%,
transparent 27%,
transparent 74%,
rgba(32, 255, 77, 0.1) 75%,
rgba(32, 255, 77, 0.1) 76%,
transparent 77%,
transparent
),
linear-gradient(
90deg,
transparent 24%,
rgba(32, 255, 77, 0.1) 25%,
rgba(32, 255, 77, 0.1) 26%,
transparent 27%,
transparent 74%,
rgba(32, 255, 77, 0.1) 75%,
rgba(32, 255, 77, 0.1) 76%,
transparent 77%,
transparent
);
background-size: 3rem 3rem;
background-position: -1rem -1rem;
z-index: 10;
background-color: #1110;
}
.box {
width: 200px;
height: 200px;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
overflow: hidden;
border: 0.1rem solid rgba(0, 255, 51, 0.2);
z-index: 11;
}
.line {
height: calc(100% - 2px);
width: 100%;
background: linear-gradient(180deg, rgba(0, 255, 51, 0) 43%, #00ff33 211%);
border-bottom: 3px solid #00ff33;
transform: translateY(-100%);
animation: radar-beam 2s infinite alternate;
animation-timing-function: cubic-bezier(0.53, 0, 0.43, 0.99);
animation-delay: 1.4s;
}
.box:after,
.box:before,
.angle:after,
.angle:before {
content: '';
display: block;
position: absolute;
width: 3vw;
height: 3vw;
z-index: 12;
border: 0.2rem solid transparent;
}
.box:after,
.box:before {
top: 0;
border-top-color: #00ff33;
}
.angle:after,
.angle:before {
bottom: 0;
border-bottom-color: #00ff33;
}
.box:before,
.angle:before {
left: 0;
border-left-color: #00ff33;
}
.box:after,
.angle:after {
right: 0;
border-right-color: #00ff33;
}
@keyframes radar-beam {
0% {
transform: translateY(-100%);
}
100% {
transform: translateY(0);
}
}
.msg {
text-align: center;
padding: 20rpx 0;
}
.box2 {
width: 300px;
height: 200px;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
z-index: 20;
}
.track {
position: absolute;
bottom: -100px;
left: 50%;
transform: translateX(-50%);
z-index: 20;
color: #fff;
display: flex;
flex-direction: column;
align-items: center;
}
.mask {
position: absolute;
z-index: 10;
background-color: rgba(0, 0, 0, 0.55);
}
.mask1 {
top: 0;
left: 0;
right: 0;
}
.mask2 {
right: 0;
}
.mask3 {
right: 0;
left: 0;
bottom: 0;
}
.mask4 {
left: 0;
}
.error {
color: #fff;
padding: 40rpx;
font-size: 24rpx;
background-color: #333333;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 550rpx;
border-radius: 20rpx;
}
.error .on1 {
font-size: 30rpx;
}
</style>

View File

@ -0,0 +1,83 @@
{
"id": "mumu-getQrcode",
"displayName": "H5调用摄像头识别二维码原生H5调用不需要任何sdk本地扫描识别不需要后端",
"version": "1.3.0",
"description": "在浏览器中调用手机摄像头进行扫码无须任何sdk支持市面上绝大部分手机。在安卓系统上可以打开闪光灯",
"keywords": [
"h5扫码",
"原生H5调用摄像头",
"二维码",
"闪光灯"
],
"repository": "",
"engines": {
"HBuilderX": "^3.1.0"
},
"dcloudext": {
"category": [
"前端组件",
"通用组件"
],
"sale": {
"regular": {
"price": "4.55"
},
"sourcecode": {
"price": "30.00"
}
},
"contact": {
"qq": "1139027508"
},
"declaration": {
"ads": "无",
"data": "无",
"permissions": "摄像头"
},
"npmurl": ""
},
"uni_modules": {
"dependencies": [],
"encrypt": [],
"platforms": {
"cloud": {
"tcb": "y",
"aliyun": "y"
},
"client": {
"Vue": {
"vue2": "y",
"vue3": "y"
},
"App": {
"app-vue": "n",
"app-nvue": "n"
},
"H5-mobile": {
"Safari": "y",
"Android Browser": "y",
"微信浏览器(Android)": "y",
"QQ浏览器(Android)": "y"
},
"H5-pc": {
"Chrome": "n",
"IE": "n",
"Edge": "n",
"Firefox": "n",
"Safari": "n"
},
"小程序": {
"微信": "n",
"阿里": "n",
"百度": "n",
"字节跳动": "n",
"QQ": "n"
},
"快应用": {
"华为": "u",
"联盟": "u"
}
}
}
}
}

View File

@ -0,0 +1,137 @@
## 插件简绍
### 实现原理
> 二维码识别功能使用的是jsQR这个库调用摄像头使用的 navigator.mediaDevices.getUserMedia 这个H5的api。通过canvas画布把摄像头获取到的数据展现到页面上同时循环监听jsQR解析。
### 使用环境
经测试发现大部分浏览器都可以正常使用微信QQ谷歌火狐safari少数的安卓自带浏览器无法使用浏览器内核版本过低。需要https环境才能使用本地测试可以在 manifest.json 中点击源码展示找到h5 ,添加:"devServer" : { "https" : true}
需要https环境才能使用
需要https环境才能使用
需要https环境才能使用
**小知识点苹果设备上不论什么浏览器都是safari套壳的不论是谷歌还是火狐都是safari套壳。这也就是代表在苹果上无需担心无法使用此插件。造成这样的现象是因为苹果有一则协议浏览器只能使用safari内核**
在安卓系统上可以打开闪光灯
#### 通过脚手架创建的 uni 项目,需要看这里
如果你是通过脚手架创建的 uni 的项目,需要自行安装 `jsQR` 依赖,并且修改组件中源码中的引入。通过 HBuilder 创建的项目不需要此操作。
```bash
# 安装 jsQR
npm install jsqr --save
# 修改组件源码对 jsQR 依赖
import jsQR from "jsqr"
```
### 插件使用
**插件已支持 uni_modules 支持组件easycom以下代码演示的是普通使用**
``` html
<!-- HTML -->
<mumu-get-qrcode @success='qrcodeSucess' @error="qrcodeError" ></mumu-get-qrcode>
```
``` javascript
// js
import mumuGetQrcode from '@/uni_modules/mumu-getQrcode/components/mumu-getQrcode/mumu-getQrcode.vue'
// 嫌路径长的话可以单独复制出来
export default {
components: {
mumuGetQrcode
},
methods: {
qrcodeSucess(data) {
uni.showModal({
title: '成功',
content: data,
success: () => {
uni.navigateBack({})
}
})
},
qrcodeError(err) {
console.log(err)
uni.showModal({
title: '摄像头授权失败',
content: '摄像头授权失败,请检测当前浏览器是否有摄像头权限。',
success: () => {
uni.navigateBack({})
}
})
}
}
}
```
### 相关API
##### 可传属性Props
| 参数 | 说明 | 类型 | 默认值 |
| ---------- | ------------------------------------------------- | ------- | ----------- |
| continue | 是否连续获取。false 监听一次 true 持续监听 | Boolean | false |
| exact | 选调用摄像头。environment 后摄像头 user 前摄像头 | String | environment |
| size | 扫码屏幕大小。whole 全屏 balf 半屏 | String | whole |
| definition | 调用摄像头清晰度。fasle 正常 true 高清 | Boolean | false |
##### 事件Events
| 事件名 | 说明 | 回调参数 |
| ------- | ------------------------------------------ | ------------------ |
| success | 检测到图中有二维码并读取到数据是回调 | 二维码数据 |
| error | 组件内部发送错误,通常是摄像头没有调用成功 | 错误信息,详情见下 |
**常见的错误信息:**
- `AbortError`[中止错误]
尽管用户和操作系统都授予了访问设备硬件的权利,而且未出现可能抛出`NotReadableError`异常的硬件问题,但仍然有一些问题的出现导致了设备无法被使用。
- `NotAllowedError`[拒绝错误]
用户拒绝了当前的浏览器实例的访问请求;或者用户拒绝了当前会话的访问;或者用户在全局范围内拒绝了所有媒体访问请求。
- `NotFoundError`[找不到错误]
找不到满足请求参数的媒体类型。
- `NotReadableError`[无法读取错误]
尽管用户已经授权使用相应的设备,操作系统上某个硬件、浏览器或者网页层面发生的错误导致设备无法被访问。
- `OverconstrainedError`[无法满足要求错误]
指定的要求无法被设备满足,此异常是一个类型为`OverconstrainedError`的对象,拥有一个`constraint`属性,这个属性包含了当前无法被满足的`constraint`对象,还拥有一个`message`属性,包含了阅读友好的字符串用来说明情况。
- `SecurityError`[安全错误]
在`getUserMedia()` 被调用的 [`Document`](https://developer.mozilla.org/zh-CN/docs/Web/API/Document) 上面,使用设备媒体被禁止。这个机制是否开启或者关闭取决于单个用户的偏好设置。
- `TypeError`[类型错误]
constraints 对象未设置[空],或者都被设置为`false`。
##### 插槽 slot
| 插槽名称 | 说明 | 默认值 |
| -------- | -------------------------------- | ------------------ |
| error | 当发送错误时,在页面上显示的内容 | 相机权限被拒绝提示 |
### 案例演示
![enter description here](https://h5plugin.mumudev.top/public/getQrcode/qrcode.png)
## 支持作者
![支持作者](https://student.mumudev.top/wxMP.jpg)