uniapp实现电子签名

news/2024/11/16 22:29:26/
引用页面
<template><view><signature :showCanvas="showCanvas" @closeCanvas="closeCanvas"></signature><u-button @click="sign">签名</u-button></view>
</template>
<script>
import signature from '@/components/signature.vue';
export default {components: {signature},data() {return {//打开canvas绘制签名showCanvas: false,//是否展示操作菜单completionSignPath: '' //签名}},methods: {//隐藏canvas签名组件closeCanvas(e) {this.showCanvas = false;if (!this.$isEmpty(e)) {this.completionSignPath = e}},sign() {this.showCanvas = true;}}
}
</script>
sagnature.vue
<template><view class="signature-box"><!-- 签名 --><view class="signature" v-show="showCanvas"><canvas class="mycanvas" canvas-id="mycanvas" @touchstart="touchstart" @touchmove="touchmove"@touchend="touchend"></canvas><view class="footer"><u-button @click="finish" type="primary" :plain="true" :ripple="true" ripple-bg-color="#909399">保存</u-button><u-button @click="clear" type="warning" :plain="true" :ripple="true" ripple-bg-color="#909399">清除</u-button><u-button @click="close" type="error" :plain="true" :ripple="true" ripple-bg-color="#909399">关闭</u-button></view></view><!-- 签完名后生成的图片 --><view v-show="SignatureImg" class="SignatureImg"><image :src="SignatureImg" mode=""></image></view><!-- 清除签完名后生成的图片 --><u-button v-show="SignatureImg" @click="obliterate" type="error" :plain="true" :ripple="true"ripple-bg-color="#909399" size="medium">清除签名</u-button></view>
</template>
<script>import { pathToBase64 } from '@/common/js/jssdk_image_tools.js'var x = 20;var y = 20;export default {data() {return {//绘图图像ctx: '',//路径点集合points: [],//签名图片SignatureImg: '',hasSign: false};},props: ['showCanvas'],methods: {//清除签名的图片obliterate() {if (this.SignatureImg) {this.SignatureImg = '';}this.close();},//关闭并清空画布close() {this.$emit('closeCanvas');this.clear();},//创建并显示画布createCanvas() {this.ctx = uni.createCanvasContext('mycanvas', this); //创建绘图对象this.ctx.setFillStyle('#000000')this.ctx.fillStyle = '#000000'//设置画笔样式this.ctx.lineWidth = 4;this.ctx.lineCap = 'round';this.ctx.lineJoin = 'round';console.log(this.ctx)},//触摸开始,获取到起点touchstart(e) {let startX = e.changedTouches[0].x;let startY = e.changedTouches[0].y;let startPoint = {X: startX,Y: startY};this.points.push(startPoint);//每次触摸开始,开启新的路径this.ctx.beginPath();},//触摸移动,获取到路径点touchmove(e) {let moveX = e.changedTouches[0].x;let moveY = e.changedTouches[0].y;let movePoint = {X: moveX,Y: moveY};this.points.push(movePoint); //存点let len = this.points.length;if (len >= 2) {this.draw(); //绘制路径}},// 触摸结束,将未绘制的点清空防止对后续路径产生干扰touchend() {this.points = [];},//绘制笔迹draw() {let point1 = this.points[0];let point2 = this.points[1];this.points.shift();this.ctx.moveTo(point1.X, point1.Y);this.ctx.lineTo(point2.X, point2.Y);this.ctx.stroke();this.ctx.draw(true);this.hasSign = true},//清空画布clear() {this.hasSign = falselet that = this;uni.getSystemInfo({success: function(res) {let canvasw = res.windowWidth;let canvash = res.windowHeight;that.ctx.clearRect(0, 0, canvasw, canvash);that.ctx.draw(true);}});},//完成绘画并保存到本地finish() {if (!this.hasSign) {uni.showToast({title: '签名为空不能保存',icon: 'none',duration: 2000})return}let that = this;uni.canvasToTempFilePath({canvasId: 'mycanvas',success: function(res) {if(!res || !res.tempFilePath) {console.log(res.tempFilePath);that.SignatureImg = res.tempFilePath;that.$emit('closeCanvas', that.SignatureImg);that.close();}else{//用来解决安卓真机获取到的是canvas图片的临时路径,转成base64格式pathToBase64(res.tempFilePath).then(re => {console.log(re);that.SignatureImg = re;that.$emit('closeCanvas', that.SignatureImg);that.close();})}}});}},mounted() {this.createCanvas();}};
</script>
<style lang="less" scoped>.signature-box {display: flex;flex-direction: column;align-items: center;background: #fff;// height: calc(100vh-44rpx);//签名模块.signature {position: fixed;top: 10px;left: 2%;z-index: 999;width: 96%;//canvas.mycanvas {width: 100%;// height: calc(100vh - 200upx);height: calc(100vh - 180rpx);background-color: #fff;border-radius: 10px 10px 0 0;}//底部按钮.footer {font-size: 14px;height: 150upx;display: flex;justify-content: space-around;align-items: center;background-color: #fff;border-radius: 0 0 10px 10px;border-top: 1px solid #a7a7a733;}}//生成的图片.SignatureImg {image {width: 750rpx;height: 750rpx;}}}
</style>
jssdk_image_tools.js(用来解决安卓真机获取到的是canvas图片的临时路径,转成base64格式)
function getLocalFilePath(path) {if (path.indexOf('_www') === 0 || path.indexOf('_doc') === 0 || path.indexOf('_documents') === 0 || path.indexOf('_downloads') === 0) {return path}if (path.indexOf('file://') === 0) {return path}if (path.indexOf('/storage/emulated/0/') === 0) {return path}if (path.indexOf('/') === 0) {var localFilePath = plus.io.convertAbsoluteFileSystem(path)if (localFilePath !== path) {return localFilePath} else {path = path.substr(1)}}return '_www/' + path
}function dataUrlToBase64(str) {var array = str.split(',')return array[array.length - 1]
}var index = 0
function getNewFileId() {return Date.now() + String(index++)
}function biggerThan(v1, v2) {var v1Array = v1.split('.')var v2Array = v2.split('.')var update = falsefor (var index = 0; index < v2Array.length; index++) {var diff = v1Array[index] - v2Array[index]if (diff !== 0) {update = diff > 0break}}return update
}export function pathToBase64(path) {return new Promise(function(resolve, reject) {if (typeof window === 'object' && 'document' in window) {if (typeof FileReader === 'function') {var xhr = new XMLHttpRequest()xhr.open('GET', path, true)xhr.responseType = 'blob'xhr.onload = function() {if (this.status === 200) {let fileReader = new FileReader()fileReader.onload = function(e) {resolve(e.target.result)}fileReader.onerror = rejectfileReader.readAsDataURL(this.response)}}xhr.onerror = rejectxhr.send()return}var canvas = document.createElement('canvas')var c2x = canvas.getContext('2d')var img = new Imageimg.onload = function() {canvas.width = img.widthcanvas.height = img.heightc2x.drawImage(img, 0, 0)resolve(canvas.toDataURL())canvas.height = canvas.width = 0}img.onerror = rejectimg.src = pathreturn}if (typeof plus === 'object') {plus.io.resolveLocalFileSystemURL(getLocalFilePath(path), function(entry) {entry.file(function(file) {var fileReader = new plus.io.FileReader()fileReader.onload = function(data) {resolve(data.target.result)}fileReader.onerror = function(error) {reject(error)}fileReader.readAsDataURL(file)}, function(error) {reject(error)})}, function(error) {reject(error)})return}if (typeof wx === 'object' && wx.canIUse('getFileSystemManager')) {wx.getFileSystemManager().readFile({filePath: path,encoding: 'base64',success: function(res) {resolve('data:image/png;base64,' + res.data)},fail: function(error) {reject(error)}})return}reject(new Error('not support'))})
}export function base64ToPath(base64) {return new Promise(function(resolve, reject) {if (typeof window === 'object' && 'document' in window) {base64 = base64.split(',')var type = base64[0].match(/:(.*?);/)[1]var str = atob(base64[1])var n = str.lengthvar array = new Uint8Array(n)while (n--) {array[n] = str.charCodeAt(n)}return resolve((window.URL || window.webkitURL).createObjectURL(new Blob([array], { type: type })))}var extName = base64.split(',')[0].match(/data\:\S+\/(\S+);/)if (extName) {extName = extName[1]} else {reject(new Error('base64 error'))}var fileName = getNewFileId() + '.' + extNameif (typeof plus === 'object') {var basePath = '_doc'var dirPath = 'uniapp_temp'var filePath = basePath + '/' + dirPath + '/' + fileNameif (!biggerThan(plus.os.name === 'Android' ? '1.9.9.80627' : '1.9.9.80472', plus.runtime.innerVersion)) {plus.io.resolveLocalFileSystemURL(basePath, function(entry) {entry.getDirectory(dirPath, {create: true,exclusive: false,}, function(entry) {entry.getFile(fileName, {create: true,exclusive: false,}, function(entry) {entry.createWriter(function(writer) {writer.onwrite = function() {resolve(filePath)}writer.onerror = rejectwriter.seek(0)writer.writeAsBinary(dataUrlToBase64(base64))}, reject)}, reject)}, reject)}, reject)return}var bitmap = new plus.nativeObj.Bitmap(fileName)bitmap.loadBase64Data(base64, function() {bitmap.save(filePath, {}, function() {bitmap.clear()resolve(filePath)}, function(error) {bitmap.clear()reject(error)})}, function(error) {bitmap.clear()reject(error)})return}if (typeof wx === 'object' && wx.canIUse('getFileSystemManager')) {var filePath = wx.env.USER_DATA_PATH + '/' + fileNamewx.getFileSystemManager().writeFile({filePath: filePath,data: dataUrlToBase64(base64),encoding: 'base64',success: function() {resolve(filePath)},fail: function(error) {reject(error)}})return}reject(new Error('not support'))})
}

http://www.ppmy.cn/news/698719.html

相关文章

Python骚操作!你还在用PS制作电子签名吗?

????加星标&#xff0c;不迷路哦&#xff01; 前言 在我们工作和生活中&#xff0c;你使用过电子签名吗&#xff1f; 众所周知&#xff0c;电子签名可以使用Photoshop制作&#xff0c;但是Photoshop安装过程复杂&#xff0c;占内存&#xff0c;杀鸡焉用牛刀&#xff1f;而且…

使用 Inkscape 制作电子签名

1&#xff1a;首先安装 Inkscape 软件&#xff0c;https://inkscape.org/&#xff0c;就是下面这个 2&#xff1a;找一张白纸&#xff0c;写上自己的名字&#xff0c;如 3&#xff1a;打开 Inkscape&#xff0c;将刚刚的图片打开 4&#xff1a;选中打开的图像&#xff0c;右键“…

【小技巧】PhotoShop + lllustrator制作电子签名矢量图

前言 有时候我们需要制作电子签名并打印&#xff0c;但是往往直接将签名拍照并插入Word中再打印出来的效果很不好&#xff0c;这时就需要将签名转换成矢量图。 准备工具 手机扫描软件&#xff0c;如Scanner[IOS]PhotoShopAdobe lllustrator 教程步骤 首先将写在纸上的签名扫…

python给pdf放置签名图片_利用python制作电子签名

有时候我们需要在文档里粘上电子签名&#xff0c;通常使用photoshop制作&#xff0c;但是通常photoshop软件还需要下载、安装&#xff0c;对于不经常使用的人来说&#xff0c;单独装这个软件没啥必要。 因此我们可以利用python对签名的图片进行修改&#xff0c;得到电子签名。主…

怎么制作电子签名?

随着科技的进步&#xff0c;电子签名应用的领域更广泛&#xff0c;尤其是电子签名的法律效力问题上&#xff0c;通过技术手段实现签名和签署人身份对应&#xff0c;利用不可被更改的内容形式&#xff0c;保障签名的电子文件是原文&#xff0c;广泛应用于电子商务的场景。 电子签…

PS2020制作电子签名

文章目录 1. 找一张白纸&#xff0c;手写签名。&#xff08;忽略我的丑字&#xff09;2. 打开PS&#xff0c;打开该图片3. 依次点击图像-->调整-->色阶4. 依次点击选择-->色彩范围5. 依次点击编辑-->定义画笔预设 1. 找一张白纸&#xff0c;手写签名。&#xff08;…

如何利用Photoshop制作电子签名

疫情期间经常用到电子签名&#xff0c;下面介绍一种用Photoshop制作手写签名的方法&#xff0c;类似方法&#xff0c;其他软件也可以尝试&#xff0c;如果在手机或者pad上真正手写保存&#xff0c;将更加流畅&#xff0c;此处为用鼠标书写。 1.打开任一版本的PS软件 2.新建一…

手写签名制作电子签名详细步骤

d怎么制作手写电子签名&#xff1f; - 知乎 (zhihu.com)https://zhuanlan.zhihu.com/p/157419337https://zhuanlan.zhihu.com/p/157419337对于有时需要进行签名再进行扫描或者拍照回传的问题&#xff0c;通常如果只是电子版的文件&#xff0c;完全可以使用电子签名来进行解决。…