当前位置:首页 > VUE

vue页面实现水印相机

2026-01-22 19:44:02VUE

vue页面实现水印相机

实现水印相机功能

在Vue中实现水印相机功能,可以通过结合HTML5的Canvas和拍照API完成。以下是一个完整的实现方案:

创建基础页面结构

<template>
  <div class="watermark-camera">
    <video ref="video" autoplay></video>
    <canvas ref="canvas"></canvas>
    <button @click="capture">拍照</button>
    <div v-if="photoTaken" class="result">
      <img :src="photoData" alt="水印照片"/>
      <button @click="download">下载照片</button>
    </div>
  </div>
</template>

初始化摄像头

export default {
  data() {
    return {
      photoData: null,
      photoTaken: false
    }
  },
  mounted() {
    this.initCamera()
  },
  methods: {
    async initCamera() {
      try {
        const stream = await navigator.mediaDevices.getUserMedia({ video: true })
        this.$refs.video.srcObject = stream
      } catch (err) {
        console.error('摄像头访问失败:', err)
      }
    }
  }
}

拍照并添加水印

methods: {
  capture() {
    const video = this.$refs.video
    const canvas = this.$refs.canvas
    const ctx = canvas.getContext('2d')

    // 设置canvas尺寸与视频相同
    canvas.width = video.videoWidth
    canvas.height = video.videoHeight

    // 绘制视频帧到canvas
    ctx.drawImage(video, 0, 0, canvas.width, canvas.height)

    // 添加水印
    ctx.font = '20px Arial'
    ctx.fillStyle = 'rgba(255, 255, 255, 0.7)'
    ctx.fillText('水印文字', 20, canvas.height - 20)

    // 转换为图片数据
    this.photoData = canvas.toDataURL('image/png')
    this.photoTaken = true
  }
}

下载功能实现

methods: {
  download() {
    const link = document.createElement('a')
    link.href = this.photoData
    link.download = 'watermark-photo.png'
    link.click()
  }
}

样式优化

.watermark-camera {
  position: relative;
  max-width: 800px;
  margin: 0 auto;
}

video, canvas {
  width: 100%;
  border: 1px solid #ddd;
}

button {
  margin-top: 10px;
  padding: 8px 16px;
  background: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.result {
  margin-top: 20px;
  text-align: center;
}

.result img {
  max-width: 100%;
  border: 1px solid #eee;
}

注意事项

  • 确保在组件销毁时关闭摄像头流
  • 水印内容可以自定义,包括文字、日期时间等
  • 考虑添加多个水印位置和样式的选项
  • 移动端可能需要处理横竖屏适配问题

扩展功能

// 添加时间水印
const now = new Date()
ctx.fillText(now.toLocaleString(), canvas.width - 200, canvas.height - 20)

// 添加图片水印
const img = new Image()
img.src = '/path/to/watermark.png'
img.onload = function() {
  ctx.drawImage(img, 10, 10, 100, 50)
}

以上实现提供了基础的Vue水印相机功能,可以根据实际需求进一步扩展和完善。

vue页面实现水印相机

标签: 水印相机
分享给朋友:

相关文章

vue实现水印组件

vue实现水印组件

Vue 水印组件实现 基础水印组件实现 创建一个基础水印组件,通过 props 接收文本、颜色、大小等参数: <template> <div class="watermark…

vue实现列表水印

vue实现列表水印

实现列表水印的方法 在Vue中为列表添加水印可以通过多种方式实现,以下是几种常见的方法: 使用CSS背景图 通过CSS的background-image属性为列表元素添加水印背景。水印可以是文字或图…

h5调用原生实现相机

h5调用原生实现相机

调用原生相机的方法 在H5页面中调用原生相机功能,通常需要通过JavaScript与原生应用(如iOS/Android)的WebView进行交互。以下是常见的实现方式: 使用<input t…

vue 水印 实现

vue 水印 实现

Vue 水印实现方法 使用 Canvas 绘制水印 在 Vue 中可以通过 Canvas 动态生成水印,并将其作为背景或覆盖层添加到页面中。 <template> <div…

vue实现水印组件

vue实现水印组件

Vue 水印组件的实现方法 使用Canvas绘制水印 在Vue组件中通过Canvas动态生成水印图案,将其作为背景添加到目标元素上。 <template> <div ref=…

vue实现pdf水印

vue实现pdf水印

添加PDF水印的实现方法 在Vue中实现PDF水印可以通过以下步骤完成,主要依赖第三方库如pdf-lib或pdf.js。 使用pdf-lib库添加水印 安装pdf-lib库: npm instal…