gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
vue 实现上传图片,在图片上手绘掩码
- User
vue 实现上传图片,在图片上手绘掩码
- Assistant
实现上传图片,在图片上手绘掩码的思路如下:
- 使用Vue框架实现文件上传功能。
- 拿到上传后的图片并显示在页面上。
- 在图片上添加画布,让用户手绘掩码。
- 使用Canvas API获取手绘的掩码。
- 将原图和掩码一起上传到服务器,或者在本地进行操作。
具体实现:
<template> <div> <input type="file" @change="handleFileChange"> <div class="img-container"> <img ref="img" :src="imgUrl"> <canvas ref="canvas" @mousedown="startDraw" @mousemove="drawing" @mouseup="endDraw"></canvas> </div> </div> </template> <script> export default { data() { return { imgUrl: '', // 图片地址 ctx: null, // 画布上下文 isDrawing: false, // 是否正在绘制 lastX: null, // 上一个画笔点的X坐标 lastY: null // 上一个画笔点的Y坐标 } }, mounted() { // 获取画布上下文 this.ctx = this.$refs.canvas.getContext('2d'); }, methods: { // 选择文件 handleFileChange(e) { const file = e.target.files[0]; this.imgUrl = URL.createObjectURL(file); }, // 开始绘制 startDraw(e) { this.isDrawing = true; this.lastX = e.offsetX; this.lastY = e.offsetY; }, // 绘制中 drawing(e) { if (!this.isDrawing) return; this.ctx.beginPath(); this.ctx.moveTo(this.lastX, this.lastY); this.ctx.lineTo(e.offsetX, e.offsetY); this.ctx.stroke(); this.lastX = e.offsetX; this.lastY = e.offsetY; }, // 停止绘制 endDraw() { this.isDrawing = false; }, // 获取掩码数据 getMaskData() { const maskData = this.ctx.getImageData(0, 0, this.$refs.canvas.width, this.$refs.canvas.height); return maskData.data; }, // 上传图片和掩码 async upload() { const imgData = new FormData(); imgData.append('img', this.$refs.canvas.toDataURL('image/png')); imgData.append('mask', this.getMaskData()); // 调用后端API上传数据 await this.$axios.post('/api/upload', imgData); } } } </script> - User
具体实现代码,将文中的$替换为!
- Assistant
下面是将文中的$替换为!后的代码:
<template> <div> <input type="file" @change="handleFileChange"> <div class="img-container"> <img ref="img" :src="imgUrl"> <canvas ref="canvas" @mousedown="startDraw" @mousemove="drawing" @mouseup="endDraw"></canvas> </div> <button @click="upload">上传</button> </div> </template> <script> export default { data() { return { imgUrl: '', // 图片地址 ctx: null, // 画布上下文 isDrawing: false, // 是否正在绘制 lastX: null, // 上一个画笔点的X坐标 lastY: null // 上一个画笔点的Y坐标 } }, mounted() { // 获取画布上下文 this.ctx = this.!refs!.canvas.getContext('2d'); }, methods: { // 选择文件 handleFileChange(e) { const file = e.target.files[0]; this.imgUrl = URL.createObjectURL(file); }, // 开始绘制 startDraw(e) { this.isDrawing = true; this.lastX = e.offsetX; this.lastY = e.offsetY; }, // 绘制中 drawing(e) { if (!this.isDrawing) return; this.ctx.beginPath(); this.ctx.moveTo(this.lastX, this.lastY); this.ctx.lineTo(e.offsetX, e.offsetY); this.ctx.stroke(); this.lastX = e.offsetX; this.lastY = e.offsetY; }, // 停止绘制 endDraw() { this.isDrawing = false; }, // 获取掩码数据 getMaskData() { const maskData = this.ctx.getImageData(0, 0, this.!refs!.canvas.width, this.!refs!.canvas.height); return maskData.data; }, // 上传图片和掩码 async upload() { const imgData = new FormData(); imgData.append('img', this.$refs.canvas.toDataURL('image/png')); imgData.append('mask', this.getMaskData()); // 调用后端API上传数据 await this.$axios.post('/api/upload', imgData); } } } </script>