记得我初入前端差不多一年, 公司 做了一个webapp,有上传 头像 功能, 当时 这个项目不是我在负责,测试的时候发现 苹果 用户拍照上传头像会翻转,当时几个前端的 同学 捯饬了一下午也没解决,结果问题转到我这里,还有 半 个小时下班;当时也是一脸懵逼,首先想到的是,这怎么判断它 是否 翻转了呢? 安卓 没问题啊,有些苹果手机相册里面的图片也没问题啊,js能有这种功能判断吗?上网查资料,果不其然,有!那就是exfe.js,继续研究,此时组长姐姐已经买好晚餐, 老板 也来慰问,最后弄到晚上9点多,算是解决了。
时隔两年,昨天又遇到这个问题,已经离开上家公司一年半了,现公司做一个万圣节 活动 ,里面也是上传图片,合成鬼脸图,早早两天前已经做好发给项目 经理 (我们这边是远程,少许几个 开发者 ),晚上快下班时,项目经理发消息“ios图片翻转,解决一下,今晚要上线,加个班”,心里一万个草泥马奔腾而过,1是我忘了ios有这个问题,2是已经发给你2天了,你快下班的时候跟我说有问题,加个班!我晚上安排要推掉!还是无偿!没有慰问餐,也没有歉意,还是很好 意思 的加个班,还是苦笑一声回复,“好吧,下次提前测一下”,这回毕竟遇见过,处理起来比较快,7点多 搞定 。后续又有什么部署的问题,不是我的问题了,又是快9点了。
只是几乎类似的场景,感慨一下。
上代码
ht ML 部分
<input ty PE ="file" accept="image/*" id="uploadImage" capture="c am era" onchange="selectFil ei mage(this);" /> < img alt="p rev iew" src="" id=" ;m yImage"/>
exfe.js来读取图片信息,我们上传的图片里面是有很多信息的
//获取照片 方向 角属性,用户旋转控制 EXIF.getData(file, function() { EXIF.getAllTags(this); orientation = EXIF.getTag(this, & # 39;Orientation'); console. LOG (Orientation); });
Orientation的值有1,3,6,8之类的,分别代表0°,180°,顺时针90°,逆时针90°
当我们 知道 了图片的旋转角度,我们就可以用canvas来处理他们了,最后得到我们想要的结果,这里摘抄了网上一段代码,如果有特殊功能,那就要自己写一些逻辑了,也就是判断角度,判断操作系统,然后用canvas重新 绘制 ,生成base64,最后对dom的操作,显示图片。
上代码
function selectFileImage(fileObj) { VAR file = fileObj.files['0']; //图片方向角 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.t arg et.result; image.onload = function() { var e xp ectWidth = 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 &am p; & 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('旋转处理'); sw IT ch(Orientation){ case 6://需要顺时针(向左)90度旋转 rotateImg(this,'left',canvas); br eak; case 8://需要逆时针(向右)90度旋转 rotateImg(this,'right',canvas); break; case 3://需要180度旋转 rotateImg(this,'right',canvas);//转两次 rotateImg(this,'right',canvas); break; } } base64 = canvas.toDataURL("image/jpeg", 1); }else if (navigator.userAgent.match(/And ROI d/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,'right',canvas); break; case 3://需要180度旋转 rotateImg(this,'right',canvas);//转两次 rotateImg(this,'right',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 == 'right') { step++; //旋转到原位置,即超过最大值 step > max_step && (step = min_step); } else { step--; step < min_step && (step = max_step); } //旋转角度以弧度值为参数 var deg ree = 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 拍照上传图片翻转问题(附代码)的详细内容,更多请关注其它相关文章!
总结
以上是 为你收集整理的 用exfe.js和canvas解决移动端 IOS 拍照上传图片翻转问题(附代码) 全部内容,希望文章能够帮你解决 用exfe.js和canvas解决移动端 IOS 拍照上传图片翻转问题(附代码) 所遇到的问题。
如果觉得 网站内容还不错, 推荐好友。
查看更多关于用exfe.js和canvas解决移动端 IOS 拍照上传图片翻转问题(附代码)的详细内容...