officialAccount/unpackage/dist/dev/mp-weixin/pages/camera/CustomCamera.js

83 lines
2.7 KiB
JavaScript

"use strict";
const common_vendor = require("../../common/vendor.js");
const _sfc_main = /* @__PURE__ */ common_vendor.defineComponent({
__name: "CustomCamera",
setup(__props) {
const video = common_vendor.ref();
const canvas = common_vendor.ref();
const started = common_vendor.ref(false);
let stream = null;
let rafID;
async function startCamera() {
var _a;
if (!((_a = navigator.mediaDevices) == null ? void 0 : _a.getUserMedia)) {
common_vendor.index.showToast({ title: "当前浏览器不支持实时相机", icon: "none" });
return;
}
try {
stream = await navigator.mediaDevices.getUserMedia({
video: { facingMode: { ideal: "environment" } },
audio: false
});
const v = video.value;
v.srcObject = stream;
await v.play();
drawFrame();
started.value = true;
} catch (err) {
common_vendor.index.__f__("error", "at pages/camera/CustomCamera.vue:72", err);
common_vendor.index.showToast({ title: "无法获取摄像头权限", icon: "none" });
}
}
function drawFrame() {
if (!started.value)
return;
const v = video.value, c = canvas.value;
const ctx = c.getContext("2d");
if (v.videoWidth && v.videoHeight) {
if (c.width !== window.innerWidth) {
c.width = window.innerWidth;
c.height = window.innerHeight;
}
ctx.drawImage(v, 0, 0, c.width, c.height);
}
rafID = requestAnimationFrame(drawFrame);
}
function shoot() {
const dataURL = canvas.value.toDataURL("image/jpeg", 0.9);
common_vendor.index.navigateBack();
common_vendor.index.$emit("photoTaken", dataURL);
}
function close() {
cleanup();
common_vendor.index.navigateBack();
}
function cleanup() {
cancelAnimationFrame(rafID);
if (stream)
stream.getTracks().forEach((t) => t.stop());
}
common_vendor.onBeforeUnmount(cleanup);
return (_ctx, _cache) => {
return common_vendor.e({
a: started.value
}, started.value ? {} : {}, {
b: started.value
}, started.value ? {} : {}, {
c: started.value ? 1 : 0,
d: started.value
}, started.value ? {
e: common_vendor.o(close),
f: common_vendor.o(shoot)
} : {}, {
g: !started.value
}, !started.value ? {
h: common_vendor.o(startCamera)
} : {});
};
}
});
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-84a6cb98"]]);
wx.createPage(MiniProgramPage);
//# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/camera/CustomCamera.js.map