ios 移动端拍照 旋转问题

2024-08-31 14:38
文章标签 问题 旋转 ios 移动 拍照

本文主要是介绍ios 移动端拍照 旋转问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这段时间开发时发现 ios14 会自动判断图片得方向  ios13及一下 不会 所以需要我们代码去判断

首先需要引入 exif-js

npm install exif-js --save    
photoChange(el) {this.$vux.loading.show({text: '图片上传中'})var file = el.target.files[0];//name: "dangqi1.png" || type: "image/png"var type = file.type.split('/')[0];if ( type === 'image' ){//将图片img转化为base64var that = this;var orientation = ''EXIF.getData(file, function () {orientation = EXIF.getTag(this, 'Orientation');});const reader = new FileReader();reader.readAsDataURL(file);reader.onload = function(evt){const base64 = evt.target.result;// 将图片旋转到正确的角度 并压缩console.log('方向',orientation)that.resetOrientation(base64, orientation, function (resultBase64) {that.b64toBlob(resultBase64, function (blob) {//调用接口上传图片that.upload(blob)});});}}else{alert('上传了非图片');}
}
resetOrientation(srcBase64, srcOrientation, callback) {var self = thisconst img = new Image();img.onload = function () {const width = img.width,height = img.height,canvas = document.createElement('canvas'),ctx = canvas.getContext('2d');// 判断图片尺寸压缩一定比率const big = (img.width > img.height) ? img.width : img.height;let rate = 1;if (big > 840) {rate = 840 / big;}canvas.width = width * rate;canvas.height = height * rate;// 安卓机不需要矫正图片var u = navigator.userAgent;var ios = 0var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //gif (!isAndroid) {ios = navigator.userAgent.match(/os\s+(\d+)/i)[1] - 0}if (isAndroid || ios < 14) {if(srcOrientation && srcOrientation !== 1){// 判断图片方向,压缩并矫正switch (srcOrientation) {// 当图片旋转180°时case 3:ctx.rotate(Math.PI);ctx.drawImage(this, -this.width * rate, -this.height * rate, this.width * rate, this.height * rate);break;// 当图片旋转90°时case 6:canvas.width = this.height * rate;canvas.height = this.width * rate;ctx.rotate(Math.PI / 2);// (0,-imgHeight) 从旋转原理图那里获得的起始点ctx.drawImage(this, 0, -this.height * rate, this.width * rate, this.height * rate);break;// 当图片旋转270°时case 8:canvas.width = this.height * rate;canvas.height = this.width * rate;ctx.rotate(3 * Math.PI / 2);ctx.drawImage(this, -this.width * rate, 0, this.width * rate, this.height * rate);break;default:ctx.drawImage(img, 0, 0, width, height, 0, 0, width * rate, height * rate);}}else {ctx.drawImage(img, 0, 0, width, height, 0, 0, width * rate, height * rate);}} else if (ios >= 14) {ctx.drawImage(img, 0, 0, width, height, 0, 0, width * rate, height * rate);}// 返回 base64callback(canvas.toDataURL('image/jpeg'));};img.src = srcBase64;},
b64toBlob(b64, onsuccess, onerror) {let img = new Image();img.onerror = onerror;img.onload = function onload() {const canvas = document.createElement('canvas');canvas.width = img.width;canvas.height = img.height;let width = img.width;let height = img.height;let ctx = canvas.getContext('2d');let rate = 1;ctx.drawImage(img, 0, 0, width, height, 0, 0, width * rate, height * rate);canvas.toBlob(onsuccess);};img.src = b64;},
upload(imgUrl){let self = thisvar formdata = new FormData();formdata.append('file',imgUrl);//下面是要传递的参数formdata.append('assetsNo',this.assetsNo);//下面是要传递的参数this.axios.post(this.apiUrl +'/xxxxxx',formdata,{headers: {'token': this.token,'Content-Type':'multipart/form-data'}//设置header信息}).then((response)=>{if(response.data.errCode==0){self.$vux.loading.hide()self.getInventoryInfo()}else{console.log(response)}}).catch((response)=>{console.log(response);})},

 

这篇关于ios 移动端拍照 旋转问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/1124191

相关文章

Python错误AttributeError: 'NoneType' object has no attribute问题的彻底解决方法

《Python错误AttributeError:NoneTypeobjecthasnoattribute问题的彻底解决方法》在Python项目开发和调试过程中,经常会碰到这样一个异常信息... 目录问题背景与概述错误解读:AttributeError: 'NoneType' object has no at

Spring的RedisTemplate的json反序列泛型丢失问题解决

《Spring的RedisTemplate的json反序列泛型丢失问题解决》本文主要介绍了SpringRedisTemplate中使用JSON序列化时泛型信息丢失的问题及其提出三种解决方案,可以根据性... 目录背景解决方案方案一方案二方案三总结背景在使用RedisTemplate操作redis时我们针对

Kotlin Map映射转换问题小结

《KotlinMap映射转换问题小结》文章介绍了Kotlin集合转换的多种方法,包括map(一对一转换)、mapIndexed(带索引)、mapNotNull(过滤null)、mapKeys/map... 目录Kotlin 集合转换:map、mapIndexed、mapNotNull、mapKeys、map

nginx中端口无权限的问题解决

《nginx中端口无权限的问题解决》当Nginx日志报错bind()to80failed(13:Permissiondenied)时,这通常是由于权限不足导致Nginx无法绑定到80端口,下面就来... 目录一、问题原因分析二、解决方案1. 以 root 权限运行 Nginx(不推荐)2. 为 Nginx

解决1093 - You can‘t specify target table报错问题及原因分析

《解决1093-Youcan‘tspecifytargettable报错问题及原因分析》MySQL1093错误因UPDATE/DELETE语句的FROM子句直接引用目标表或嵌套子查询导致,... 目录报js错原因分析具体原因解决办法方法一:使用临时表方法二:使用JOIN方法三:使用EXISTS示例总结报错原

Windows环境下解决Matplotlib中文字体显示问题的详细教程

《Windows环境下解决Matplotlib中文字体显示问题的详细教程》本文详细介绍了在Windows下解决Matplotlib中文显示问题的方法,包括安装字体、更新缓存、配置文件设置及编码調整,并... 目录引言问题分析解决方案详解1. 检查系统已安装字体2. 手动添加中文字体(以SimHei为例)步骤

SpringSecurity整合redission序列化问题小结(最新整理)

《SpringSecurity整合redission序列化问题小结(最新整理)》文章详解SpringSecurity整合Redisson时的序列化问题,指出需排除官方Jackson依赖,通过自定义反序... 目录1. 前言2. Redission配置2.1 RedissonProperties2.2 Red

nginx 负载均衡配置及如何解决重复登录问题

《nginx负载均衡配置及如何解决重复登录问题》文章详解Nginx源码安装与Docker部署,介绍四层/七层代理区别及负载均衡策略,通过ip_hash解决重复登录问题,对nginx负载均衡配置及如何... 目录一:源码安装:1.配置编译参数2.编译3.编译安装 二,四层代理和七层代理区别1.二者混合使用举例

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.

Java 线程安全与 volatile与单例模式问题及解决方案

《Java线程安全与volatile与单例模式问题及解决方案》文章主要讲解线程安全问题的五个成因(调度随机、变量修改、非原子操作、内存可见性、指令重排序)及解决方案,强调使用volatile关键字... 目录什么是线程安全线程安全问题的产生与解决方案线程的调度是随机的多个线程对同一个变量进行修改线程的修改操