vue中使用带隐藏文本信息的图片、图片水印的方法


Posted in Javascript onApril 24, 2020

一.带隐藏文本信息的图片

通过RGB 分量值的小量变动,不影响对图片的识别。因此,我们可以在图片加入文字信息。

最终达到如下效果:

vue中使用带隐藏文本信息的图片、图片水印的方法

首先,在该组件中加入img用于显示图片

<canvas ref="canvas" v-show="0"></canvas>
<img :src="imageUrl" v-if="imageUrl"/>

调用方法

encryptionImg({
    width = '',
    height = '',
    content = '',
   }){
    let img = this.img
    const imgRatio = img.naturalWidth / img.naturalHeight;
    const ctxWidth = width || img.naturalWidth;
    const ctxHeight = height || ctxWidth / imgRatio;
    this.canvas.width = ctxWidth;
    this.canvas.height = ctxHeight;
    const ctx = this.ctx;
    ctx.font = '16px Microsoft Yahei';
    ctx.textAlign = 'left';
    ctx.textBaseline = 'top';
    ctx.fillText(content, 12, ctxHeight/2, ctxWidth);17     const textData = ctx.getImageData(0, 0, ctxWidth, ctxHeight);
    this.imageUrl = this.mergeData(textData.data, 'R',ctxWidth,ctxHeight);19    }

把文字和图片整合成一张图

mergeData(newData, color, width, height) {
    let img = this.img
    this.ctx.drawImage(img, 0, 0, width, height);
    this.originalData = this.ctx.getImageData(0, 0, width, height);
    var oData = this.originalData.data;
    var bit, offset;
    switch (color) {
     case 'R':
      bit = 0;
      offset = 3;
      break;
     case 'G':
      bit = 1;
      offset = 2;
      break;
     case 'B':
      bit = 2;
      offset = 1;
      break;
    }
    for (var i = 0; i < oData.length; i++) {
     if (i % 4 == bit) {
      if (newData[i + offset] === 0 && (oData[i] % 2 === 1)) {
       if (oData[i] === 255) {
        oData[i]--
       } else {
        oData[i]++
       }
      } else if (newData[i + offset] !== 0 && (oData[i] % 2 === 0)) {
       if (oData[i] === 255) {
        oData[i]--
       } else {
        oData[i]++
       }
      }
     }
    }
    this.ctx.putImageData(this.originalData, 0, 0);
    return this.canvas.toDataURL();
   },

调用下面方法,解开图片信息

decryptImg(){
    var data = this.originalData.data;
    for(var i = 0; i < data.length; i++){
     if(i % 4 == 0){
      if(data[i] % 2 == 0){
       data[i] = 0;
      } else {
       data[i] = 255;
      }
     } else if(i % 4 == 3){
      continue;
     } else {
      data[i] = 0;
     }
    }
    this.ctx.putImageData(this.originalData, 0, 0);
    this.imageUrl = this.canvas.toDataURL();
   },

二.图片水印

watermark({
    content = '',
    container = '',
    width = '',
    height = '',
    position = 'bottom-right',
    font = '16px 微软雅黑',
    fillStyle = 'rgba(255, 255, 255, 1)',
    zIndex = 11000,
   } = {}) {
    let img = this.img
    const imgRatio = img.naturalWidth / img.naturalHeight;
    const ctxWidth = width || img.naturalWidth;
    const ctxHeight = height || ctxWidth / imgRatio;
    this.canvas.width = ctxWidth;
    this.canvas.height = ctxHeight;
    const ctx = this.ctx;
    ctx.drawImage(img, 0, 0, ctxWidth, ctxHeight);
    ctx.shadowColor = 'rgba(0, 0, 0, 0.2)';
    ctx.shadowOffsetX = 2;
    ctx.shadowOffsetY = 2;
    ctx.shadowBlur = 2;
    ctx.font = font;
    ctx.fillStyle = fillStyle;
    if(position == 'center') {
     ctx.textAlign = 'center';
     ctx.textBaseline = 'middle';
     ctx.fillText(content, ctxWidth / 2, ctxHeight / 2, ctxWidth)
    }else if(position == 'bottom-right') {
     ctx.textAlign = 'right';
     ctx.textBaseline = 'alphabetic';
     ctx.fillText(content, ctxWidth-12, ctxHeight-12, ctxWidth)
    }
    const base64Url = this.canvas.toDataURL();
    if(container) {
     const div = document.createElement('div');
     div.setAttribute('style', ` width: ${ctxWidth}px; height: ${ctxHeight}px; z-index: ${zIndex}; 
     pointer-events: none; background-repeat: repeat; background-image: url('${base64Url}')`);
     container.insertBefore(div, null);
    }
    this.imageUrl = base64Url
   }

参考

http://www.alloyteam.com/2016/03/image-steganography/

到此这篇关于vue中使用带隐藏文本信息的图片、图片水印的文章就介绍到这了,更多相关vue 隐藏文本信息图片水印内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Javascript 相关文章推荐
jQuery 位置插件
Dec 25 Javascript
浅谈javascript 迭代方法
Jan 21 Javascript
Javascript实现飞动广告效果的方法
May 25 Javascript
关于javascript中dataset的问题小结
Nov 16 Javascript
javascript html5实现表单验证
Mar 01 Javascript
JS针对浏览器窗口关闭事件的监听方法集锦
Jun 24 Javascript
轮播的简单实现方法
Jul 28 Javascript
jQuery简单实现点击文本框复制内容到剪贴板上的方法
Aug 01 Javascript
jQuery常见面试题之DOM操作详析
Jul 05 jQuery
Vue2.0学习之详解Vue 组件及父子组件通信
Dec 12 Javascript
浅谈用Webpack路径压缩图片上传尺寸获取的问题
Feb 22 Javascript
Vue面试题及Vue知识点整理
Oct 07 Javascript
基于Vue实现微前端的示例代码
Apr 24 #Javascript
Vue通过getAction的finally来最大程度避免影响主数据呈现问题
Apr 24 #Javascript
小程序开发之模态框组件封装
Apr 23 #Javascript
详解Vue3中对VDOM的改进
Apr 23 #Javascript
微信小程序实现滑动操作代码
Apr 23 #Javascript
微信小程序图片右边加两行文字的代码
Apr 23 #Javascript
Vue中通过vue-router实现命名视图的问题
Apr 23 #Javascript
You might like
php中mysql连接和基本操作代码(快速测试使用,简单方便)
2014/04/25 PHP
PHP实现递归无限级分类
2015/10/22 PHP
PHP图像裁剪缩略裁切类源码及使用方法
2016/01/07 PHP
PHP实现伪静态方法汇总
2016/01/13 PHP
PHP页面输出时js设置input框的选中值
2016/09/30 PHP
php 类中的常量、静态属性、非静态属性的区别
2017/04/09 PHP
16个最流行的JavaScript框架[推荐]
2011/05/29 Javascript
30分钟就入门的正则表达式基础教程
2013/02/25 Javascript
JS实现根据出生年月计算年龄
2014/01/10 Javascript
JavaScript分页功能的实现方法
2015/04/25 Javascript
jQuery遍历DOM元素与节点方法详解
2016/04/14 Javascript
解析预加载显示图片艺术
2016/12/05 Javascript
Vue数据驱动模拟实现5
2017/01/13 Javascript
node.js基于express使用websocket的方法
2017/11/09 Javascript
Vue2.0用户权限控制解决方案
2017/11/29 Javascript
js实现AI五子棋人机大战
2020/05/28 Javascript
Python基础教程之浅拷贝和深拷贝实例详解
2017/07/15 Python
Python3.4解释器用法简单示例
2019/03/22 Python
python中的句柄操作的方法示例
2019/06/20 Python
python pytest进阶之fixture详解
2019/06/27 Python
Python实现Keras搭建神经网络训练分类模型教程
2020/06/12 Python
python 8种必备的gui库
2020/08/27 Python
Python 实现二叉查找树的示例代码
2020/12/21 Python
前端面试必备之CSS3的新特性
2017/09/05 HTML / CSS
庆国庆国旗下讲话稿2014
2014/09/21 职场文书
2014年优秀班主任工作总结
2014/12/16 职场文书
邀请书格式范文
2015/02/02 职场文书
2015关于重阳节的演讲稿
2015/03/20 职场文书
2015年党员岗位承诺书
2015/04/27 职场文书
致男子1500米运动员的广播稿
2019/11/08 职场文书
vue前端工程的搭建
2021/03/31 Vue.js
SQL Server——索引+基于单表的数据插入与简单查询【1】
2021/04/05 SQL Server
php7中停止php-fpm服务的方法详解
2021/05/09 PHP
Java获取e.printStackTrace()打印的信息方式
2021/08/07 Java/Android
什么是动态刷新率DRR? Windows11动态刷新率功能介绍
2021/11/21 数码科技
python实现手机推送 代码也就10行左右
2022/04/12 Python