分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

JS获取照片拍摄的角度属性,用于旋转控制

发布时间:2023-09-06 01:53责任编辑:林大明关键词:暂无标签

在微信里ios手机上传竖拍照片会自动旋转90度,与拍摄时的角度不同,所以需要处理:

1、使用EXIF.js可以获取到照片的拍摄属性:

API 方法

名称说明
EXIF.getData(img, callback)

获取图像的数据

能兼容尚未支持提供 EXIF 数据的浏览器获取到元数据。

EXIF.getTag(img, tag)获取图像的某个数据
EXIF.getAllTags(img)获取图像的全部数据,值以对象的方式返回
EXIF.pretty(img)获取图像的全部数据,值以字符串的方式返回
//获取照片方向角属性,用户旋转控制 ????????????EXIF.getData(files, function() { ???????????????//alert(EXIF.getTag(this, ‘Orientation‘)); ?????????????????Orientation = EXIF.getTag(this, ‘Orientation‘); // ???????????????return; ????????????});

其中Orientation就是拍摄的角度信息;其他参数信息可以查看:http://code.ciaoca.com/javascript/exif-js/

注意:这里我的files是获取的文件格式的图片,files[0]获取的。

Orientation属性说明如下:

旋转角度参数
1
顺时针90°6
逆时针90°8
180°3

2、判断照片需要旋转多少角度并使用canvas对其旋转处理:

//修复ios ????????????????????????????if (navigator.userAgent.match(/iphone/i)) { ???????????????????????????????var img = new Image(); ???????????????????????????????img.src = resImg.src; ???????????????????????????????img.onload = function(){ ???????????????????????????????????var canvas=document.createElement("canvas"); ???????????????????????????????????var ctx=canvas.getContext("2d"); ???????????????????????????????????var imgWidth = canvas.width = this.width; ???????????????????????????????????????var imgHeight = canvas.height = this.height; ????????????????????????????????????//如果方向角不为1,都需要进行旋转 added by lzk ????????????????????????????????????if(Orientation && Orientation != 1){ ???????????????????????????????????????switch(Orientation){ ???????????????????????????????????????????case 6: ????// 旋转90度 ???????????????????????????????????????????????canvas.width = this.height; ???????????????????????????????????????????????????canvas.height = this.width; ???????????????????????????????????????????????????ctx.rotate(Math.PI / 2); ???????????????????????????????????????????????// (0,-imgHeight) 从旋转原理图那里获得的起始点 ???????????????????????????????????????????????ctx.drawImage(this, 0, -imgHeight, imgWidth, imgHeight); ???????????????????????????????????????????????break; ???????????????????????????????????????????case 3: ????// 旋转180度 ???????????????????????????????????????????????ctx.rotate(Math.PI); ???????????????????????????????????????????????????ctx.drawImage(this, -imgWidth, -imgHeight, imgWidth, imgHeight); ???????????????????????????????????????????????break; ???????????????????????????????????????????case 8: ????// 旋转-90度 ???????????????????????????????????????????????canvas.width = imgHeight; ???????????????????????????????????????????????????canvas.height = imgWidth; ???????????????????????????????????????????????????ctx.rotate(3 * Math.PI / 2); ???????????????????????????????????????????????????ctx.drawImage(this, -imgWidth, 0, imgWidth, imgHeight); ???????????????????????????????????????????????break; ???????????????????????????????????????} ???????????????????????????????????}else{ ???????????????????????????????????????ctx.drawImage(this, 0, 0, imgWidth, imgHeight); ???????????????????????????????????} ???????????????????????????????????var base64code=canvas.toDataURL("image/png",1); ???????????????????????????????????$(resImg).attr("src",base64code); ???????????????????????????????????var $blob = baseToBlobImg(base64code); ???????????????????????????????????if($(_self).attr(‘id‘) == ‘hiddenServerId‘){ ???????????????????????????????????????chooseImgList[0].serverImg = $blob //接收blob ???????????????????????????????????????????????????????????????????????}else{ ???????????????????????????????????????chooseImgList[1].serverImg = $blob //接收blob ???????????????????????????????????} ???????????????????????????????????????????????????????????????????????????????????????????????????????} ???????????????????????????????????????????????????????????}else{ ???????????????????????????????//非苹果手机压缩后上传 ???????????????????????????????var base64code = resImg.src; ???????????????????????????????var $blob = baseToBlobImg(base64code); ???????????????????????????????if($(_self).attr(‘id‘) == ‘hiddenServerId‘){ ???????????????????????????????????chooseImgList[0].serverImg = $blob //接收blob ???????????????????????????????????????????????????????????????}else{ ???????????????????????????????????chooseImgList[1].serverImg = $blob //接收blob ???????????????????????????????} ???????????????????????????}

3、将处理后的图片最后转换成bold类型文件上传:

/*将base64的图片转换为blod格式上传*/ ???function baseToBlobImg(base64code){ ???????var arr = base64code.split(‘,‘), mime = arr[0].match(/:(.*?);/)[1], ???????????bstr = atob(arr[1]), ???????????n = bstr.length, ???????????u8arr = new Uint8Array(n); ???????while(n--) { ???????????u8arr[n] = bstr.charCodeAt(n); ???????} ???????????????return obj = new Blob([u8arr], {type:mime}); ???} ?

可借鉴CSDN里林志Ke的帖子:利用exif.js解决ios手机上传竖拍照片旋转90度问题

JS获取照片拍摄的角度属性,用于旋转控制

原文地址:https://www.cnblogs.com/amor17/p/9013169.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved