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 相关文章推荐
javascript 兼容所有浏览器的DOM扩展功能
Aug 01 Javascript
javascript中的parseInt和parseFloat区别
Jul 12 Javascript
jquery插件开发之实现md5插件
Mar 17 Javascript
js的参数有长度限制吗?发现不能超过2083个字符
Apr 20 Javascript
js用Date对象的setDate()函数对日期进行加减操作
Sep 18 Javascript
js实现点击切换TAB标签实例
Aug 21 Javascript
javascript实现tab切换的两个实例
Nov 05 Javascript
JavaScript的设计模式经典之建造者模式
Feb 24 Javascript
Bootstrap3 多个模态对话框无法显示的解决方案
Feb 23 Javascript
Angular动态添加、删除输入框并计算值实例代码
Mar 29 Javascript
ES6学习教程之模板字符串详解
Oct 09 Javascript
node.js监听文件变化的实现方法
Apr 17 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 输出双引号&quot;与单引号'的方法
2010/05/09 PHP
php天翼开放平台短信发送接口实现方法
2014/12/22 PHP
学习php设计模式 php实现状态模式
2015/12/07 PHP
PHP编写RESTful接口的方法
2016/02/21 PHP
简单实现php上传文件功能
2017/09/21 PHP
PHP设计模式之单例模式定义与用法分析
2019/03/26 PHP
基于jQuery的ajax功能实现web service的json转化
2009/08/29 Javascript
5款JavaScript代码压缩工具推荐
2014/07/07 Javascript
显示今天的日期js代码(阳历和农历)
2014/09/30 Javascript
ECMAScript6中Map/WeakMap详解
2015/06/12 Javascript
基于JS实现的倒计时程序实例
2015/07/24 Javascript
js实现input密码框提示信息的方法(附html5实现方法)
2016/01/14 Javascript
jQuery中通过ajax的get()函数读取页面的方法
2016/02/29 Javascript
js+html5实现canvas绘制网页时钟的方法
2016/05/21 Javascript
Bootstrap CSS组件之分页(pagination)和翻页(pager)
2016/12/17 Javascript
vue.js将unix时间戳转换为自定义时间格式
2017/01/03 Javascript
Vue + Webpack + Vue-loader学习教程之相关配置篇
2017/03/14 Javascript
javascript实现table单元格点击展开隐藏效果(实例代码)
2017/04/10 Javascript
详解微信小程序 相对定位和绝对定位
2017/05/11 Javascript
JavaScript类型相关的常用操作总结
2019/02/14 Javascript
详解JavaScript函数callee、call、apply的区别
2019/03/08 Javascript
微信小程序中为什么使用var that=this
2019/08/27 Javascript
python算法学习之计数排序实例
2013/12/18 Python
举例讲解Python程序与系统shell交互的方式
2015/04/09 Python
Python中数组,列表:冒号的灵活用法介绍(np数组,列表倒序)
2018/04/18 Python
python 实现在Excel末尾增加新行
2018/05/02 Python
python的中异常处理机制
2018/08/30 Python
3分钟看懂Python后端必须知道的Django的信号机制
2020/07/26 Python
Selenium获取登录Cookies并添加Cookies自动登录的方法
2020/12/04 Python
Python截图并保存的具体实例
2021/01/14 Python
Maje德国官网:法国女性成衣品牌
2017/02/10 全球购物
新加坡第一大健康与美容零售商:屈臣氏新加坡(Watsons Singapore)
2020/12/11 全球购物
应急处置方案
2014/06/16 职场文书
2014年教研室工作总结
2014/12/06 职场文书
房地产销售员岗位职责
2015/04/11 职场文书
vue使用echarts实现折线图
2022/03/21 Vue.js