|
@@ -10,41 +10,59 @@
|
|
|
</div>
|
|
|
|
|
|
<input v-show="false" id="fileInput" accept="image/*" type="file" />
|
|
|
+ <el-dialog
|
|
|
+ id="carmer-dialog"
|
|
|
+ v-model="visible"
|
|
|
+ :close-on-click-modal="false"
|
|
|
+ :title="null"
|
|
|
+ close-icon="null"
|
|
|
+ width="840px"
|
|
|
+ >
|
|
|
+ <div v-loading="isLoading">
|
|
|
+ <video
|
|
|
+ id="video"
|
|
|
+ :height="photoSize + 'px'"
|
|
|
+ :width="photoSize + 'px'"
|
|
|
+ autoplay="autoplay"
|
|
|
+ ></video>
|
|
|
+ <canvas
|
|
|
+ v-show="false"
|
|
|
+ id="canvas"
|
|
|
+ :height="photoSize + 'px'"
|
|
|
+ :width="photoSize + 'px'"
|
|
|
+ ></canvas>
|
|
|
+ <div class="bottom-btns">
|
|
|
+ <el-button class="cancelBtn" @click="closeMedia">关闭</el-button>
|
|
|
+ <el-button class="sureBtn" type="primary" @click="takePhoto"
|
|
|
+ >拍照
|
|
|
+ </el-button>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ </el-dialog>
|
|
|
</div>
|
|
|
-
|
|
|
- <el-dialog
|
|
|
- id="carmer-dialog"
|
|
|
- v-model="visible"
|
|
|
- :close-on-click-modal="false"
|
|
|
- :title="null"
|
|
|
- close-icon="null"
|
|
|
- >
|
|
|
- <video id="video" autoplay="autoplay" height="500px" width="500px"></video>
|
|
|
- <canvas id="canvas" height="500px" width="500px"></canvas>
|
|
|
- <img id="imgTag" alt="imgTag" src="" />
|
|
|
- <div class="bottom-btns">
|
|
|
- <el-button class="cancelBtn" @click="closeMedia">关闭</el-button>
|
|
|
- <el-button class="sureBtn" type="primary" @click="takePhoto"
|
|
|
- >拍照
|
|
|
- </el-button>
|
|
|
- </div>
|
|
|
- </el-dialog>
|
|
|
</template>
|
|
|
|
|
|
<script setup>
|
|
|
import { uploadFileApi } from "@/api/file";
|
|
|
import { Delete, RefreshRight } from "@element-plus/icons-vue";
|
|
|
|
|
|
+const isLoading = ref(false);
|
|
|
let mediaStreamTrack = null; // 视频对象(全局)
|
|
|
let video;
|
|
|
-
|
|
|
-const cameraEmit = defineEmits(["uploadFinish", "resetSelect", "deleteAll"]);
|
|
|
+let photoSize = 800;
|
|
|
+let realSize = 1200;
|
|
|
+const cameraEmit = defineEmits([
|
|
|
+ "uploadFinish",
|
|
|
+ "resetSelect",
|
|
|
+ "deleteAll",
|
|
|
+ "close",
|
|
|
+]);
|
|
|
|
|
|
const openMedia = async () => {
|
|
|
visible.value = true;
|
|
|
nextTick(() => {
|
|
|
let constraints = {
|
|
|
- video: { width: 500, height: 500 },
|
|
|
+ video: { width: photoSize, height: photoSize },
|
|
|
audio: false,
|
|
|
};
|
|
|
//获得video摄像头
|
|
@@ -83,18 +101,20 @@ function takePhoto() {
|
|
|
//获得Canvas对象
|
|
|
let video = document.getElementById("video");
|
|
|
let canvas = document.getElementById("canvas");
|
|
|
+ canvas.width = realSize; // 实际渲染像素
|
|
|
+ canvas.height = realSize; // 实际渲染像素
|
|
|
let ctx = canvas.getContext("2d");
|
|
|
- ctx.drawImage(video, 0, 0, 500, 500);
|
|
|
+ ctx.drawImage(video, 0, 0, realSize, realSize);
|
|
|
|
|
|
+ isLoading.value = true;
|
|
|
// toDataURL --- 可传入'image/png'---默认, 'image/jpeg'
|
|
|
let base64Data = document.getElementById("canvas").toDataURL();
|
|
|
let blob = dataURItoBlob(base64Data);
|
|
|
let file = new File([blob], `photo.png`, { type: "image/png" });
|
|
|
|
|
|
uploadFileApi(file).then((res) => {
|
|
|
- console.log("上传图片", res);
|
|
|
- cameraEmit("uploadFinish", res);
|
|
|
- closeMedia();
|
|
|
+ cameraEmit("uploadFinish", res.data.fileUrl);
|
|
|
+ isLoading.value = false;
|
|
|
});
|
|
|
}
|
|
|
|
|
@@ -111,6 +131,7 @@ function closeMedia() {
|
|
|
|
|
|
document.getElementById("video").srcObject = null;
|
|
|
visible.value = false;
|
|
|
+ cameraEmit("close");
|
|
|
}
|
|
|
|
|
|
const visible = ref(false);
|