当前位置:首页 > VUE

vue实现图像标注

2026-02-17 19:27:01VUE

Vue 实现图像标注的方法

使用 HTML5 Canvas 实现基础标注

安装依赖(如需要):

npm install vue-drawing-canvas

基础代码示例:

vue实现图像标注

<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 示例:

vue实现图像标注

<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)
}

加载标注数据:

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实现granfana

vue实现granfana

Vue 实现类似 Grafana 的仪表盘 要在 Vue 中实现类似 Grafana 的仪表盘功能,需要结合数据可视化库、状态管理和 UI 组件。以下是关键步骤和推荐工具: 数据可视化库 使用 E…

实现 vue 框架

实现 vue 框架

Vue 框架的基本实现 Vue 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是实现 Vue 框架的基本步骤和核心概念。 核心概念 响应式数据绑定 Vue 的核心特性之一是响应式…

vue实现 单选

vue实现 单选

实现 Vue 单选功能 在 Vue 中实现单选功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 绑定单选按钮 通过 v-model 可以轻松实现单选功能,将单选按钮的值绑定到…

vue实现分栏

vue实现分栏

Vue实现分栏的常见方法 使用CSS Flexbox布局 通过Flexbox可以快速实现分栏效果,适合大多数响应式布局场景 <template> <div class="co…

vue实现ppt

vue实现ppt

Vue 实现 PPT 的功能 Vue 可以结合第三方库或自定义组件实现类似 PPT 的演示效果。以下是几种常见的方法: 使用第三方库 reveal.js reveal.js 是一个流行的演示框架,支…

vue实现白板

vue实现白板

Vue实现白板功能 使用Vue实现白板功能可以通过HTML5的Canvas API结合Vue的响应式特性来完成。以下是实现白板功能的关键步骤: 安装依赖 在Vue项目中安装必要的依赖,如vue-dr…