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

广告:宝塔服务器面板,一键全能部署及管理,送你10850元礼包,点我领取~~~

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

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

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

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

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

上代码

html部分

<input type="file" accept="image/*" id="uploadImage" capture="camera" onchange="selectFileImage(this);" /><img alt="preview" src="" id="myImage"/>
登录后复制

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['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.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,'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(/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,'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 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 拍照上传图片翻转问题(附代码)的详细内容,更多请关注9543建站博客其它相关文章!

9543建站博客
一个专注于网站开发、微信开发的技术类纯净博客。

作者头像
admin创始人

肥猫,知名SEO博客站长,14年SEO经验。

上一篇:看看这些前端面试题,带你搞定高频知识点(四)
下一篇:页面中文本域的值怎么设置

发表评论

关闭广告
关闭广告