解决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文件中写入:

<img #imgElement src="../assets/icon/3.jpg" (load)="getInfo()" [ngStyle]="{'transform': 'rotate(' + rorateAngle + 'deg)'}">

5、ts文件中写入:

import { Component, ViewChild, ElementRef, OnInit } from '@angular/core'
import { EXIF } from 'exif-js'
旋转图片的方法:
getInfo() {
    console.log(this.imgElement.nativeElement)
    let that = this
    EXIF.getData(this.imgElement.nativeElement, function(){
      const imgInfo = EXIF.getAllTags(this)
      const imgRotate = EXIF.getTag(this, 'Orientation')
      console.log(imgInfo)
      console.log(imgRotate)

      switch (imgRotate) {
        // 顺时针旋转90度
        case 6:
         that.rorateAngle = 90
        break;
        // 逆时针旋转90度
        case 8:
          that.rorateAngle = -90
        break;
        case 3:
          that.rorateAngle = 180
        break;
      }
      console.log(that.rorateAngle)
   })
注意: 因为图片完全加载好才能获取到信息进行旋转,所以要把方法放到load中





  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值