当前位置:首页 > VUE

vue页面实现水印相机

2026-02-23 10:31:23VUE

实现水印相机功能

在Vue中实现水印相机功能,可以通过以下步骤完成。核心思路是利用Canvas绘制水印,并将Canvas内容转换为图片。

使用Canvas绘制水印

创建一个Canvas元素,设置其宽度和高度与图片一致。在Canvas上绘制图片和水印文字。

<template>
  <div>
    <input type="file" @change="handleFileUpload" accept="image/*">
    <canvas ref="canvas"></canvas>
    <button @click="downloadImage">下载带水印图片</button>
  </div>
</template>

<script>
export default {
  methods: {
    handleFileUpload(event) {
      const file = event.target.files[0];
      if (!file) return;

      const reader = new FileReader();
      reader.onload = (e) => {
        const img = new Image();
        img.onload = () => {
          this.drawWatermark(img);
        };
        img.src = e.target.result;
      };
      reader.readAsDataURL(file);
    },

    drawWatermark(img) {
      const canvas = this.$refs.canvas;
      const ctx = canvas.getContext('2d');

      canvas.width = img.width;
      canvas.height = img.height;

      ctx.drawImage(img, 0, 0, img.width, img.height);

      ctx.font = '30px Arial';
      ctx.fillStyle = 'rgba(255, 255, 255, 0.5)';
      ctx.textAlign = 'center';
      ctx.fillText('Watermark', canvas.width / 2, canvas.height / 2);
    },

    downloadImage() {
      const canvas = this.$refs.canvas;
      const link = document.createElement('a');
      link.download = 'watermarked-image.png';
      link.href = canvas.toDataURL('image/png');
      link.click();
    }
  }
};
</script>

添加自定义水印选项

允许用户自定义水印文字、颜色、大小和位置。

data() {
  return {
    watermarkText: '自定义水印',
    watermarkColor: 'rgba(255, 255, 255, 0.5)',
    watermarkSize: 30,
    watermarkPosition: { x: 0.5, y: 0.5 }
  };
},

methods: {
  drawWatermark(img) {
    // ...其他代码

    ctx.font = `${this.watermarkSize}px Arial`;
    ctx.fillStyle = this.watermarkColor;
    ctx.fillText(
      this.watermarkText,
      canvas.width * this.watermarkPosition.x,
      canvas.height * this.watermarkPosition.y
    );
  }
}

实现重复水印效果

在Canvas上重复绘制水印,覆盖整个图片。

drawRepeatedWatermark(img) {
  const canvas = this.$refs.canvas;
  const ctx = canvas.getContext('2d');

  canvas.width = img.width;
  canvas.height = img.height;

  ctx.drawImage(img, 0, 0, img.width, img.height);

  ctx.font = `${this.watermarkSize}px Arial`;
  ctx.fillStyle = this.watermarkColor;

  const stepX = 200;
  const stepY = 200;

  for (let x = 0; x < canvas.width; x += stepX) {
    for (let y = 0; y < canvas.height; y += stepY) {
      ctx.fillText(this.watermarkText, x, y);
    }
  }
}

添加旋转水印效果

通过旋转Canvas上下文,实现斜向水印效果。

drawRotatedWatermark(img) {
  const canvas = this.$refs.canvas;
  const ctx = canvas.getContext('2d');

  canvas.width = img.width;
  canvas.height = img.height;

  ctx.drawImage(img, 0, 0, img.width, img.height);

  ctx.font = `${this.watermarkSize}px Arial`;
  ctx.fillStyle = this.watermarkColor;

  ctx.save();
  ctx.translate(canvas.width / 2, canvas.height / 2);
  ctx.rotate(-45 * Math.PI / 180);

  const step = 200;
  for (let x = -canvas.width; x < canvas.width * 2; x += step) {
    for (let y = -canvas.height; y < canvas.height * 2; y += step) {
      ctx.fillText(this.watermarkText, x, y);
    }
  }

  ctx.restore();
}

使用第三方库简化实现

对于更复杂的水印需求,可以使用第三方库如watermarkjs

import watermark from 'watermarkjs';

methods: {
  async applyWatermarkWithLibrary() {
    const file = this.$refs.fileInput.files[0];
    if (!file) return;

    const watermarkedImage = await watermark([file])
      .image(watermark.text.lowerRight('Watermark', '40px Arial', '#fff', 0.5))
      .then(img => img.src);

    this.$refs.preview.src = watermarkedImage;
  }
}

响应式设计考虑

确保Canvas在不同设备上正确显示,需要根据屏幕尺寸调整Canvas大小。

vue页面实现水印相机

adjustCanvasSize() {
  const canvas = this.$refs.canvas;
  const container = this.$refs.container;

  canvas.style.width = '100%';
  canvas.style.height = 'auto';

  const ratio = canvas.width / canvas.height;
  const containerWidth = container.clientWidth;

  canvas.width = containerWidth;
  canvas.height = containerWidth / ratio;
}

以上方法提供了从基础到高级的水印相机实现方案,可以根据具体需求选择适合的实现方式。

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

相关文章

vue页面实现水印相机

vue页面实现水印相机

实现水印相机功能 在Vue中实现水印相机功能,可以通过结合HTML5的Canvas和拍照API完成。以下是一个完整的实现方案: 创建基础页面结构 <template> <div…

js 实现水印功能

js 实现水印功能

实现水印功能的几种方法 使用Canvas绘制水印 通过Canvas绘制文本或图片水印,生成Base64数据后设置为背景。 function createWatermark(text, options…

uniapp 调相机

uniapp 调相机

uniapp 调用相机的方法 在uniapp中调用相机功能可以通过以下方式实现,适用于拍照或拍摄视频: 使用uni.chooseImage方法 该方法允许从相机或相册中选择图片,适用于拍照场景。…

vue 水印 实现

vue 水印 实现

Vue 水印实现方法 使用 CSS 背景图实现 在 Vue 中可以通过 CSS 的 background-image 属性添加水印。创建一个包含水印文本的 Canvas 元素,将其转换为 Data U…

php水印实现

php水印实现

PHP 图片水印实现方法 使用 GD 库添加文字水印 安装 GD 库后,通过 imagettftext 函数实现文字水印。示例代码: $image = imagecreatefromjpeg('s…

php实现图片水印

php实现图片水印

添加文字水印 使用 imagettftext 函数可以在图片上添加自定义文字水印。需要确保服务器安装了 GD 库和 FreeType 支持。 // 加载原始图片 $image = imagecrea…