解决ionic在手机上拍照图片旋转的问题

ionic项目使用手机拍照后图片在安卓手机上显示是旋转了90度的,这个时候需要借助外部js-----exif.js


1、先下载 exif.js 到项目中,用 npm 命令行:


npm install exif-js --save


2、然后在index.html引入:


<script src="exif.js"></script>


3、将exif.js文件复制到项目根目录下www文件夹中:

{项目目录}\www


4、html文件中写入:

[html]  view plain  copy
  1. <img #imgElement src="../assets/icon/3.jpg" (load)="getInfo()" [ngStyle]="{'transform': 'rotate(' + rorateAngle + 'deg)'}">  
5、ts文件中写入:

[javascript]  view plain  copy
  1. import { Component, ViewChild, ElementRef, OnInit } from '@angular/core'  
  2. import { EXIF } from 'exif-js'  
旋转图片的方法:
[javascript]  view plain  copy
  1. getInfo() {  
  2.     console.log(this.imgElement.nativeElement)  
  3.     let that = this  
  4.     EXIF.getData(this.imgElement.nativeElement, function(){  
  5.       const imgInfo = EXIF.getAllTags(this)  
  6.       const imgRotate = EXIF.getTag(this'Orientation')  
  7.       console.log(imgInfo)  
  8.       console.log(imgRotate)  
  9.   
  10.       switch (imgRotate) {  
  11.         // 顺时针旋转90度  
  12.         case 6:  
  13.          that.rorateAngle = 90  
  14.         break;  
  15.         // 逆时针旋转90度  
  16.         case 8:  
  17.           that.rorateAngle = -90  
  18.         break;  
  19.         case 3:  
  20.           that.rorateAngle = 180  
  21.         break;  
  22.       }  
  23.       console.log(that.rorateAngle)  
  24.    })  
注意: 因为图片完全加载好才能获取到信息进行旋转,所以要把方法放到load中
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值