我正在使用这个库simple-vue-camera.让它正常工作,但我希望我可以使用它与脚本设置.我不能访问相机的快照方法..
我猜问题出在'const cameraComponent = this.$refs.camera'中,我无法通过refs访问Camera组件并获取其快照方法。
下面是使用常规组合API的代码:
<template>
<q-dialog ref="dialogRef" @hide="onDialogHide">
<q-card class="column q-dialog-plugin">
<div class="colunm">
<ModalBar :icon="'mdi-camera'" :title="$t('global.camera')" class="col" @close="onCancelClick" />
<q-separator />
<div>
<camera v-if="!fotoTirada" ref="camera" :resolution="{ width: 400, height: 400 }" autoplay></camera>
<img v-else :src="imageUrl" />
</div>
<q-separator />
<q-card-actions class="flex justify-between">
<q-btn color="secondary-2" label="Cancelar" @click="onDialogHide" />
<div>
<q-btn v-if="!fotoTirada" color="primary" label="Capturar" @click="takeSnapshot" />
<q-btn v-if="fotoTirada" label="Tirar outra foto" @click="fotoTirada = false" />
<q-btn v-if="fotoTirada" color="primary" label="Salvar" @click="onOKClick(imageUrl)" />
</div>
</q-card-actions>
</div>
</q-card>
</q-dialog>
</template>
<script>
import Camera from "simple-vue-camera";
import { defineComponent } from "vue";
import { useDialogPluginComponent } from 'quasar'
import ModalBar from "components/ModalBar.vue";
export default defineComponent({
components: {
Camera,
ModalBar
},
emits: [
...useDialogPluginComponent.emits
],
setup() {
const { dialogRef, onDialogHide, onDialogOK, onDialogCancel } = useDialogPluginComponent()
return {
dialogRef,
onDialogHide,
onOKClick(imageUrl) {
onDialogOK(imageUrl)
},
onCancelClick: onDialogCancel
}
},
data() {
return {
imageUrl: null,
fotoTirada: null,
};
},
methods: {
async takeSnapshot() {
const cameraComponent = this.$refs.camera;
const blob = await cameraComponent.snapshot({ width: 400, height: 400 });
this.imageUrl = URL.createObjectURL(blob);
const formData = new FormData();
formData.append("file", blob);
this.fotoTirada = true;
},
},
});
</script>
1条答案
按热度按时间tct7dpnv1#
我觉得用,试着用camera.value是很正常的,看这个例子