失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 用exfe.js和canvas解决移动端 IOS 拍照上传图片翻转问题(附代码)

用exfe.js和canvas解决移动端 IOS 拍照上传图片翻转问题(附代码)

时间:2024-08-09 15:39:19

相关推荐

用exfe.js和canvas解决移动端 IOS 拍照上传图片翻转问题(附代码)

web前端|H5教程

canvas,ios,javascript

web前端-H5教程

本篇文章给大家带来的内容是关于用exfe.js和canvas解决移动端 IOS 拍照上传图片翻转问题,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

phpcms 生成广告源码,VSCode中文提示插件,ubuntu dd 硬盘,tomcat 外部启动,sqlite3找不到文件,海绵宝宝的身上爬虫子的图片,php点击验证码刷新,常州seo外链群发,苹果手机在线网站源码,政府 纯 html5 模板lzw

记得我初入前端差不多一年,公司做了一个webapp,有上传头像功能,当时这个项目不是我在负责,测试的时候发现苹果用户拍照上传头像会翻转,当时几个前端的同学捯饬了一下午也没解决,结果问题转到我这里,还有半个小时下班;当时也是一脸懵逼,首先想到的是,这怎么判断它是否翻转了呢?安卓没问题啊,有些苹果手机相册里面的图片也没问题啊,js能有这种功能判断吗?上网查资料,果不其然,有!那就是exfe.js,继续研究,此时组长姐姐已经买好晚餐,老板也来慰问,最后弄到晚上9点多,算是解决了。

工作日记源码,vscode 定位到方法,ubuntu 界面共享,tomcat应用内存泄漏,sqlite数据库连接查看,脚底下的爬虫是啥意思呀,php 获取url 正则,厦门seo的行价,打车网站代码,企业微信开发的模板下载lzw

时隔两年,昨天又遇到这个问题,已经离开上家公司一年半了,现公司做一个万圣节活动,里面也是上传图片,合成鬼脸图,早早两天前已经做好发给项目经理(我们这边是远程,少许几个开发者),晚上快下班时,项目经理发消息“ios图片翻转,解决一下,今晚要上线,加个班”,心里一万个草泥马奔腾而过,1是我忘了ios有这个问题,2是已经发给你2天了,你快下班的时候跟我说有问题,加个班!我晚上安排要推掉!还是无偿!没有慰问餐,也没有歉意,还是很好意思的加个班,还是苦笑一声回复,“好吧,下次提前测一下”,这回毕竟遇见过,处理起来比较快,7点多搞定。后续又有什么部署的问题,不是我的问题了,又是快9点了。

友价源码破解版,vscode怎么输出运行时间,ubuntu 不能ssh,tomcat主要看哪个日志文件,c 链接sqlite,地产网页设计,php服务器缓存,58天免插件,前端框架模式切换,爬虫类去除,php时间戳多少位,电商seo关键词,双色球网站源码,pc移动网页源码,织梦淘宝客模板,单页面应用网站源码,sql c 房屋中介管理系统,idc程序所在模板lzw

只是几乎类似的场景,感慨一下。

上代码

html部分

exfe.js来读取图片信息,我们上传的图片里面是有很多信息的

//获取照片方向角属性,用户旋转控制EXIF.getData(file, function() { EXIF.getAllTags(this); Orientation = EXIF.getTag(this, Orientation); console.log(Orientation);});

Orientation的值有1,3,6,8之类的,分别代表0°,180°,顺时针90°,逆时针90°

当我们知道了图片的旋转角度,我们就可以用canvas来处理他们了,最后得到我们想要的结果,这里摘抄了网上一段代码,如果有特殊功能,那就要自己写一些逻辑了,也就是判断角度,判断操作系统,然后用canvas重新绘制,生成base64,最后对dom的操作,显示图片。

上代码

function selectFileImage(fileObj) { var file = fileObj.files[]; //图片方向角 var Orientation = null; if (file) { //获取照片方向角属性,用户旋转控制 EXIF.getData(file, function() {EXIF.getAllTags(this);Orientation = EXIF.getTag(this, Orientation);console.log(Orientation) }); var oReader = new FileReader(); oReader.onload = function(e) {var image = new Image();image.src = e.target.result;image.onload = function() { var expectWidth = this.naturalWidth; var expectHeight = this.naturalHeight; if (this.naturalWidth > this.naturalHeight && this.naturalWidth > 800) { expectWidth = 800; expectHeight = expectWidth * this.naturalHeight / this.naturalWidth; } else if (this.naturalHeight > this.naturalWidth && this.naturalHeight > 1200) { expectHeight = 1200; expectWidth = expectHeight * this.naturalWidth / this.naturalHeight; } var canvas = document.createElement("canvas"); var ctx = canvas.getContext("2d"); canvas.width = expectWidth; canvas.height = expectHeight; ctx.drawImage(this, 0, 0, expectWidth, expectHeight); var base64 = null; //修复ios if (navigator.userAgent.match(/iphone/i)) { console.log(iphone); //如果方向角不为1,都需要进行旋转 if(Orientation != "" && Orientation != 1){ alert(旋转处理); switch(Orientation){case 6://需要顺时针(向左)90度旋转 rotateImg(this,left,canvas); break;case 8://需要逆时针(向右)90度旋转 rotateImg(this, ight,canvas); break;case 3://需要180度旋转 rotateImg(this, ight,canvas);//转两次 rotateImg(this, ight,canvas); break; } } base64 = canvas.toDataURL("image.jpeg" alt="用exfe.js和canvas解决移动端 IOS 拍照上传图片翻转问题(附代码)" title="用exfe.js和canvas解决移动端 IOS 拍照上传图片翻转问题(附代码)", 1); }else if (navigator.userAgent.match(/Android/i)) {// 修复android var encoder = new JPEGEncoder(); base64 = encoder.encode(ctx.getImageData(0, 0, expectWidth, expectHeight), 80); }else{ if(Orientation != "" && Orientation != 1){ switch(Orientation){case 6://需要顺时针(向左)90度旋转 rotateImg(this,left,canvas); break;case 8://需要逆时针(向右)90度旋转 rotateImg(this, ight,canvas); break;case 3://需要180度旋转 rotateImg(this, ight,canvas);//转两次 rotateImg(this, ight,canvas); break; } } base64 = canvas.toDataURL("image/jpeg", 1); } $("#myImage").attr("src", base64);}; }; oReader.readAsDataURL(file); } } //对图片旋转处理function rotateImg(img, direction,canvas) { //最小与最大旋转方向,图片旋转4次后回到原方向 var min_step = 0; var max_step = 3; if (img == null)return; //img的高度和宽度不能在img元素隐藏后获取,否则会出错 var height = img.height; var width = img.width; //var step = img.getAttribute(step); var step = 2; if (step == null) { step = min_step; } if (direction == ight) { step++; //旋转到原位置,即超过最大值 step > max_step && (step = min_step); } else { step--; step < min_step && (step = max_step); } //旋转角度以弧度值为参数 var degree = step * 90 * Math.PI / 180; var ctx = canvas.getContext(2d); switch (step) { case 0:canvas.width = width;canvas.height = height;ctx.drawImage(img, 0, 0);break; case 1:canvas.width = height;canvas.height = width;ctx.rotate(degree);ctx.drawImage(img, 0, -height);break; case 2:canvas.width = width;canvas.height = height;ctx.rotate(degree);ctx.drawImage(img, -width, -height);break; case 3:canvas.width = height;canvas.height = width;ctx.rotate(degree);ctx.drawImage(img, -width, 0);break; } }

如果觉得《用exfe.js和canvas解决移动端 IOS 拍照上传图片翻转问题(附代码)》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。