vue移动端页面调用手机拍照_vue实现PC端调用摄像头拍照、移动端调用手机前置摄像头人脸录入、及图片旋转矫正、压缩上传base64格式/文件格式...
export default {
() {
return {}
},
methods: {
# // 压缩图片 and 旋转角度纠正 下方代码 # 需要自行去掉 个人只作为着色效果加上
compressImage (event) {
let _this = this
let file = event.target.files[0]
let fileReader = new FileReader()
let img = new Image()
let imgWidth = ''
let imgHeight = ''
# // 旋转角度
let Orientation = null
# // 缩放图片需要的canvas
let canvas = document.createElement('canvas')
let ctx = canvas.getContext('2d') # // 图片大小 大于2MB 则压缩
const isLt2MB = file.size < 2097152
# // 通过 EXIF 获取旋转角度 1 为正常 3 为 180° 6 顺时针90° 9 为 逆时针90°
EXIF.getData(file, function () {
EXIF.getAllTags(this)
Orientation = EXIF.getTag(this, 'Orientation')
})
# // 文件读取 成功执行
fileReader.onload = function (ev) {
# // 文件base64化,以便获知图片原始尺寸
img.src = ev.target.result
}
# // 读取文件
fileReader.readAsDataURL(file)
# // base64地址图片加载完毕后
img.onload = function () {
imgWidth = img.width
imgHeight = img.height
canvas.width = img.width
canvas.height = img.height
# // 目标尺寸
let targetWidth = imgWidth
let targetHeight = imgHeight
# // 不需要压缩 不需要做旋转处理
if (isLt2MB && imgWidth < 960 && imgHeight < 960 && !Orientation) return _this.XMLHttpRequest(file)
if (isLt2MB && imgWidth < 960 && imgHeight < 960 && +Orientation === 1) return _this.XMLHttpRequest(file)
# // 大于2MB 、img宽高 > 960 则进行压缩
if (!isLt2MB || imgWidth >= 960 || imgHeight >= 960) {
# // 最大尺寸
let maxWidth = 850
let maxHeight = 850
# // 图片尺寸超过 960 X 960 的限制
if (imgWidth > maxWidth || imgHeight > maxHeight) {
if (imgWidth / imgHeight > maxWidth / maxHeight) {
# // 更宽,按照宽度限定尺寸
targetWidth = maxWidth
targetHeight = Math.round(maxWidth * (imgHeight / imgWidth))
} else {
targetHeight = maxHeight
targetWidth = Math.round(maxHeight * (imgWidth / imgHeight))
}
}
# // canvas对图片进行缩放
canvas.width = targetWidth
canvas.height = targetHeight
# // 图片大小超过 2Mb 但未旋转 则只需要进行图片压缩
if (!Orientation || +Orientation === 1) {
ctx.drawImage(img, 0, 0, targetWidth, targetHeight)
}
}
# // 拍照旋转 需矫正图片
if (Orientation && +Orientation !== 1) {
switch (+Orientation) {
case 6: # // 旋转90度
canvas.width = targetHeight
canvas.height = targetWidth
ctx.rotate(Math.PI / 2)
# // 图片渲染
ctx.drawImage(img, 0, -targetHeight, targetWidth, targetHeight)
break
case 3: # // 旋转180度
ctx.rotate(Math.PI)
# // 图片渲染
ctx.drawImage(img, -targetWidth, -targetHeight, targetWidth, targetHeight)
break
case 8: # // 旋转-90度
canvas.width = targetHeight
canvas.height = targetWidth
ctx.rotate(3 * Math.PI / 2)
# // 图片渲染
ctx.drawImage(img, -targetWidth, 0, targetWidth, targetHeight)
break
}
}
# // base64 格式 我这是vuex 形式 重点是 canvas.toDataURL('image/jpeg', 1)
# // _this.$store.commit('SAVE_FACE_IMAGE_BASE64', canvas.toDataURL('image/jpeg', 1))
# // 调用接口上传
# // _this.upAppUserFaceByBase64()
# // 通过文件流格式上传
canvas.toBlob(function (blob) {
_this.XMLHttpRequest(blob)
}, 'image/jpeg', 1)
}
},
# // 上传base64方式
upAppUserFaceByBase64 () {
this.$store.dispatch('upAppUserFaceByBase64', {
baseFace: this.$store.state.faceImageBase64
}).then(res => {
# // 上传成功
}).catch(err => {
console.log(err)
})
},
# // 上传
XMLHttpRequest (params) {
# // 图片ajax上传
let action = '后台接口地址'
let xhr = new XMLHttpRequest()
let formData = new FormData()
formData.delete('multipartFile')
formData.append('multipartFile', params)
# // 文件上传成功回调
xhr.onprogress = this.updateProgress
xhr.onerror = this.updateError
# // 开始上传
xhr.open('POST', action, true)
xhr.send(formData)
},
# // 上传成功回调
updateProgress (res) {
# // res 就是成功后的返回
},
# // 上传失败回调
updateError (error) {
console.log(error)
}
}
}
vue移动端页面调用手机拍照_vue实现PC端调用摄像头拍照、移动端调用手机前置摄像头人脸录入、及图片旋转矫正、压缩上传base64格式/文件格式...相关推荐
- vue实现PC端调用摄像头拍照人脸录入、移动端调用手机前置摄像头人脸录入、及图片旋转矫正、压缩上传base64格式/文件格式
PC端调用摄像头拍照上传base64格式到后台,这个没什么花里胡哨的骚操作,直接看代码 (canvas + video) <template><div><!--开启摄像头 ...
- uniapp实现拍照并上传base64格式的图片(记录一下~)
html结构 -- <uni-list><uni-list-item class="pro" title="图片" :showArrow=&q ...
- 基于vue + axios + lrz.js 微信端图片压缩上传
业务场景 微信端项目是基于Vux + Axios构建的,关于图片上传的业务场景有以下几点需求: 1.单张图片上传(如个人头像,实名认证等业务) 2.多张图片上传(如某类工单记录) 3.上传图片时期望能 ...
- PC端、移动端手机竖拍原图压缩上传顺时针旋转90°的解决方案
问题背景 最近在做的项目中,不管是移动端还是后台系统都涉及到了手机照片压缩上传的问题,做完功能测试的时候发现图片回显的时候有些顺时针旋转了90°(竖拍照片,不管是ios还是android都存在这问题) ...
- 前端解决手机拍照旋转问题及图片压缩上传
1.依赖 jquery.js 2.html部分 <input class="addImage" name="loadFile" type="f ...
- axios获取图片显示_Vue.js+axios图片预览以及上传显示进度
在平时项目开发中,我们经常会对文件做一些上传操作,不仅仅要实现基本需求,也要兼顾用户体验,根据自己在工作中遇到的问题谈谈对图片上传的预览以及上传进度的优化. 1.搭建项目 基于Vue.js+axios ...
- vue本地上传并预览php,vue.js 实现图片本地预览 裁剪 压缩 上传功能
以下代码涉及 Vue 2.0 及 ES6 语法. 目标 纯 javascrpit 实现,兼容ie9及以上浏览器,在本地做好文件格式.长宽.大小的检测,减少浏览器交互. 现实是残酷的,为了兼容Ie9 还 ...
- 移动端实现图片压缩上传
移动端实现图片压缩上传 目前来说,HTML5的各种新API都在移动端的webkit上得到了较好的实现.根据查看caniuse,本demo里使用到的FileReader.Blob.Formdata对象均 ...
- vue+element-ui实现富文本(含有图片粘贴拖拽上传)
vue+element-ui实现富文本(含有图片粘贴拖拽上传) Just For Share | 仅仅分享 首先需要安装 cnpm i vue-quill-editor -D 富文本编辑器 cnpm ...
最新文章
- 实现数组(java)
- 【区块链-以太坊】5 Ubuntu下truffle ganache安装及使用
- 【PAT乙级】1089 狼人杀-简单版 (20 分)
- What are definitions of ​Model, Inference and Algorithm and its associations ?
- 开发环境入门 linux基础 (部分)while for 函数 计划任务
- 古时候中状元到底是一种怎样的体验?
- JS实现,页面显示数据加载,显示加载效果,加载完成显示数据
- linux主机如何安装杀毒软件,Linux 杀毒软件ClamAV安装部署
- MYSQL 视图 触发器 存储过程 事务 索引
- Linux文件属性的777权限
- 基于Spring Boot的个人博客系统的设计与实现 毕业设计-附源码271611
- postgresql 数据库 等保审计 遇到的问题与办法 (整理)
- phpcms 设置page页码 首页、末页、总数据
- 【小白】【新手向】Hexo+Github搭建个人博客
- ISCC2021 Web WP
- php怎么插入画笔,ps画笔怎么设置
- 计算机组成原理实验箱教程,计算机组成原理实验箱的技术参数和性能指标
- XP系统添加网络打印机步骤
- JAVA+MySQL综合笔记
- namedtuple的使用
热门文章
- android 盈利模式
- unity3d占用内存太大解决方法
- ae渲染出现错误是什么问题_After Effects错误:写入文件.....时发生渲染错误.输出模块失败.文件可能已损坏。(-1610153464)...
- 【Multisim仿真】运放电路:反相比例运算电路
- WSL2 Ubuntu18.04 apt-get update失败
- rpgmvp文件转换图片_干货|如何快速将图片转换成CAD文件格式
- importOrder
- 计网PPT 第六章 应用层
- File Upload(文件上传漏洞)
- Cesium创建任意位置和形状的水纹波浪效果