vue-qrcode-reader 掃一掃二維碼插件
此插件必須訪問(wèn) https 才可訪問(wèn)攝像頭
1、安裝插件
因是 vue2 版本所以根據(jù)官網(wǎng)提示
https://github.com/gruhn/vue-qrcode-reader
How to make it work with Vue 2?
Support is dropped but you can downgrade to vue-qrcode-reader v3.* or lower.
# 為以下安裝
npm install --save vue-qrcode-reader@3
2、創(chuàng)建組件
- 在 src 下面的 components 創(chuàng)建 qrcode.vue
<template>
<div>
<div class="ku-scanner">
<qrcode-stream
v-show="qrcode"
:camera="camera"
:torch="torchActive"
@decode="onDecode"
@init="onInit"
>
<div class="ku-scanner-content">
<div class="ku-scanner-tooltip">
將二維碼/條碼放入框內(nèi),即自動(dòng)掃描
</div>
<div class="ku-scanner-section">
<div class="ku-scanner-section-animation-line"></div>
<div class="ku-scanner-section-angle"></div>
</div>
</div>
</qrcode-stream>
</div>
<div class="ku-scanner-error">錯(cuò)誤信息:{{ error }}</div>
<div class="ku-scanner-decode-result">掃描結(jié)果:{{ result }}</div>
<div>
<van-button type="primary" @click="openCamera">打開(kāi)相機(jī)</van-button>
<van-button type="info" @click="closeCamera">關(guān)閉相機(jī)</van-button>
<van-button type="default" @click="openFlash">打開(kāi)手電筒</van-button>
<van-button type="warning" @click="switchCamera">相機(jī)反轉(zhuǎn)</van-button>
</div>
</div>
</template>
<script>
// 引入
import { QrcodeStream } from "vue-qrcode-reader";
export default {
// 注冊(cè)
components: { QrcodeStream },
data() {
return {
result: "", // 掃碼結(jié)果信息
error: "", // 錯(cuò)誤信息
show: false,
qrcode: true,
torchActive: false,
camera: "front",
};
},
methods: {
onDecode(result) {
console.log(result);
this.result = result;
},
async onInit(promise) {
const { capabilities } = await promise;
const TORCH_IS_SUPPORTED = !!capabilities.torch;
try {
await promise;
} catch (error) {
if (error.name === "NotAllowedError") {
this.error = "ERROR: 您需要授予相機(jī)訪問(wèn)權(quán)限";
} else if (error.name === "NotFoundError") {
this.error = "ERROR: 這個(gè)設(shè)備上沒(méi)有攝像頭";
} else if (error.name === "NotSupportedError") {
this.error = "ERROR: 所需的安全上下文(HTTPS、本地主機(jī))";
} else if (error.name === "NotReadableError") {
this.error = "ERROR: 相機(jī)被占用";
} else if (error.name === "OverconstrainedError") {
this.error = "ERROR: 安裝攝像頭不合適";
} else if (error.name === "StreamApiNotSupportedError") {
this.error = "ERROR: 此瀏覽器不支持流API";
}
}
},
// 打開(kāi)相機(jī)
openCamera() {
this.camera = "rear";
this.qrcode = true;
this.show = true;
},
// 關(guān)閉相機(jī)
closeCamera() {
this.camera = "off";
this.qrcode = false;
this.show = false;
},
// 打開(kāi)手電筒
openFlash() {
switch (this.torchActive) {
case true:
this.torchActive = false;
break;
case false:
this.torchActive = true;
break;
}
},
// 相機(jī)反轉(zhuǎn)
switchCamera() {
// console.log(this.camera);
switch (this.camera) {
case "front":
this.camera = "rear";
console.log(this.camera);
break;
case "rear":
this.camera = "front";
console.log(this.camera);
break;
}
},
},
};
</script>
<style scoped lang="scss">
.ku-scanner-error {
font-weight: bold;
font-size: 14px;
color: red;
}
.ku-scanner-decode-result {
font-size: 14px;
color: #000;
}
.ku-scanner {
height: 282px;
.ku-scanner-content {
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;
width: 100%;
height: 281px;
position: relative;
background-color: rgba(18, 18, 18, 0);
// 提示信息
.ku-scanner-tooltip {
width: 100%;
height: 35px;
line-height: 35px;
font-size: 14px;
text-align: center; /* color: #f9f9f9; */
margin: 0 auto;
position: absolute;
top: 0;
left: 0;
color: #ffffff;
}
.ku-scanner-section {
width: 213px;
height: 213px;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
overflow: hidden;
border: 0.1rem solid rgba(0, 255, 51, 0.2);
// 裝飾角
&:after {
content: "";
display: block;
position: absolute;
width: 12px;
height: 12px;
border: 0.2rem solid transparent;
top: 0;
border-top-color: #00ff33;
right: 0;
border-right-color: #00ff33;
}
&:before {
content: "";
display: block;
position: absolute;
width: 12px;
height: 12px;
border: 0.2rem solid transparent;
top: 0;
border-top-color: #00ff33;
left: 0;
border-left-color: #00ff33;
}
// 裝飾角
.ku-scanner-section-angle {
&:after {
content: "";
display: block;
position: absolute;
width: 12px;
height: 12px;
border: 0.2rem solid transparent;
bottom: 0;
border-bottom-color: #00ff33;
right: 0;
border-right-color: #00ff33;
}
&:before {
content: "";
display: block;
position: absolute;
width: 12px;
height: 12px;
border: 0.2rem solid transparent;
bottom: 0;
border-bottom-color: #00ff33;
left: 0;
border-left-color: #00ff33;
}
}
// 掃描活動(dòng)線
.ku-scanner-section-animation-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;
}
}
}
}
// 掃描活動(dòng)線--上下 動(dòng)畫(huà)
@keyframes radar-beam {
0% {
transform: translateY(-100%);
}
100% {
transform: translateY(0);
}
}
</style>
- 解決 vue 項(xiàng)目本地訪問(wèn) https
安裝
npm install -D @vitejs/plugin-basic-ssl
# or
# yarn add -D @vitejs/plugin-basic-ssl
# or
# pnpm add -D @vitejs/plugin-basic-ssl
在 vite.config.js 或者 vite.config.ts 中配置:
import basicSsl from '@vitejs/plugin-basic-ssl' // 引入
server: {
// ...
proxy: {
// ...
},
https: true, // 安裝basicSsl后,這里可設(shè)置可不設(shè)置
},
plugins: [
// ...
basicSsl(),
]
重啟服務(wù)
npm run dev
# or
# yarn dev
# or
# pnpm run dev
啟動(dòng)后可訪問(wèn) https
? Local: https://localhost:8001/
? Network: https://10.100.251.23:8001/
? Network: https://192.168.3.87:8001/