当前位置:首页 > VUE

vue实现图像标注

2026-02-17 19:27:01VUE

Vue 实现图像标注的方法

使用 HTML5 Canvas 实现基础标注

安装依赖(如需要):

npm install vue-drawing-canvas

基础代码示例:

<template>
  <div>
    <canvas 
      ref="canvas"
      @mousedown="startDrawing"
      @mousemove="draw"
      @mouseup="stopDrawing"
      @mouseleave="stopDrawing"
    ></canvas>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isDrawing: false,
      lastX: 0,
      lastY: 0
    }
  },
  methods: {
    startDrawing(e) {
      this.isDrawing = true
      const canvas = this.$refs.canvas
      const rect = canvas.getBoundingClientRect()
      this.lastX = e.clientX - rect.left
      this.lastY = e.clientY - rect.top
    },
    draw(e) {
      if (!this.isDrawing) return
      const canvas = this.$refs.canvas
      const ctx = canvas.getContext('2d')
      const rect = canvas.getBoundingClientRect()
      const currentX = e.clientX - rect.left
      const currentY = e.clientY - rect.top

      ctx.beginPath()
      ctx.moveTo(this.lastX, this.lastY)
      ctx.lineTo(currentX, currentY)
      ctx.stroke()

      this.lastX = currentX
      this.lastY = currentY
    },
    stopDrawing() {
      this.isDrawing = false
    }
  },
  mounted() {
    const canvas = this.$refs.canvas
    canvas.width = 800
    canvas.height = 600
  }
}
</script>

使用第三方库实现高级功能

推荐库:

  • vue-annotator:提供矩形、圆形、多边形等标注工具
  • fabric.js:功能强大的Canvas库,支持对象操作

fabric.js 示例:

<template>
  <canvas id="canvas" width="800" height="600"></canvas>
</template>

<script>
import { fabric } from 'fabric'

export default {
  mounted() {
    const canvas = new fabric.Canvas('canvas')
    const rect = new fabric.Rect({
      left: 100,
      top: 100,
      width: 50,
      height: 50,
      fill: 'transparent',
      stroke: 'red',
      strokeWidth: 2
    })
    canvas.add(rect)
  }
}
</script>

实现标注保存与加载

保存标注数据:

saveAnnotations() {
  const canvas = this.$refs.canvas
  const dataURL = canvas.toDataURL('image/png')
  localStorage.setItem('annotatedImage', dataURL)
}

加载标注数据:

vue实现图像标注

loadAnnotations() {
  const canvas = this.$refs.canvas
  const ctx = canvas.getContext('2d')
  const img = new Image()
  img.src = localStorage.getItem('annotatedImage')
  img.onload = () => {
    ctx.drawImage(img, 0, 0)
  }
}

添加标注类型选择

<template>
  <div>
    <select v-model="annotationType">
      <option value="rectangle">矩形</option>
      <option value="circle">圆形</option>
      <option value="line">直线</option>
    </select>
    <canvas ref="canvas" @click="addAnnotation"></canvas>
  </div>
</template>

<script>
export default {
  data() {
    return {
      annotationType: 'rectangle'
    }
  },
  methods: {
    addAnnotation(e) {
      const canvas = this.$refs.canvas
      const ctx = canvas.getContext('2d')
      const rect = canvas.getBoundingClientRect()
      const x = e.clientX - rect.left
      const y = e.clientY - rect.top

      if (this.annotationType === 'rectangle') {
        ctx.strokeRect(x, y, 100, 50)
      } else if (this.annotationType === 'circle') {
        ctx.beginPath()
        ctx.arc(x, y, 30, 0, Math.PI * 2)
        ctx.stroke()
      }
    }
  }
}
</script>

实现标注文本功能

addTextAnnotation(e) {
  const text = prompt('输入标注文本:')
  if (text) {
    const canvas = this.$refs.canvas
    const ctx = canvas.getContext('2d')
    const rect = canvas.getBoundingClientRect()
    const x = e.clientX - rect.left
    const y = e.clientY - rect.top

    ctx.font = '16px Arial'
    ctx.fillText(text, x, y)
  }
}

以上方法可以根据具体需求组合使用,构建完整的图像标注功能。对于更复杂的应用场景,建议使用专门的标注库如labelImgannotorious的Vue封装版本。

标签: 图像vue
分享给朋友:

相关文章

vue实现展开与收起

vue实现展开与收起

Vue 实现展开与收起功能 在 Vue 中实现展开与收起功能可以通过多种方式完成,以下是一些常见的方法: 使用 v-show 或 v-if 控制显示 通过 Vue 的指令 v-show 或 v-if…

vue交互实现

vue交互实现

Vue 交互实现方法 Vue.js 提供了多种方式实现用户交互,包括事件处理、表单绑定、动态渲染等。以下是常见的交互实现方法: 事件处理 通过 v-on 或 @ 指令绑定事件,触发方法或直接执行表达…

vue实现签章

vue实现签章

Vue 实现签章功能 签章功能通常包括手写签名、电子印章等。以下是基于 Vue 的实现方法: 使用 canvas 实现手写签名 安装依赖(如需要): npm install signature_p…

vue实现tree

vue实现tree

Vue 实现 Tree 组件 使用 Vue 实现 Tree 组件可以通过递归组件的方式来实现层级结构展示。以下是一个完整的实现方法: 基础递归组件实现 创建 Tree 组件文件 Tree.vue,使…

vue拼图实现

vue拼图实现

实现 Vue 拼图游戏的方法 使用 Vue 组件和动态数据绑定 创建一个 Vue 组件来管理拼图的状态和逻辑。通过 v-for 动态渲染拼图块,利用 v-bind 绑定样式和位置。拼图块的数据可以存储…

vue 实现循环

vue 实现循环

Vue 实现循环的方法 在 Vue 中,可以使用 v-for 指令来实现循环渲染列表数据。以下是几种常见的循环实现方式: 基本数组循环 <template> <ul>…