antd-mobile 移动端使用ImagePicker手机上拍照后反显时图片会旋转90度问题

解决方案:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<div style={{padding:'10px 0'}}>
  <span className={s.fileStyle}>
     <img src={this.state.imgSrc}  alt="" className={s.imgs} id='imgBox'/>
        <ImagePicker
           length={1}
           files={this.state.files}
           onChange={this.onChange}
           onImageClick={(index, fs) => console.log(index, fs)}
           selectable={true}
           accept="image/gif,image/jpeg,image/jpg,image/png"
           disableDelete
     />
  </span>
</div>
//js代码
onChange = (files, type, index) => {
   console.log(files, type, index);
   if (type == 'add') {
     this.setState({
       files,
     });
     var file=files[files.length - 1].file;
     const windowURL = window.URL || window.webkitURL;//实现预览
     var u = navigator.userAgent;
     var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
     var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
     if(isiOS){
          console.log('ios');
          console.log(file.size/1024+'kb')
          const isLt400K = file.size /1024 < 200;
          if(!isLt400K){
            return this.getsmallpic(file)
          }else{
            this.setState({
              imgArr:files[files.length - 1].file,
              imgSrc:files[files.length - 1].url
            })
          }
        }else if(isAndroid){
          console.log('Android');
          var that=this;
          this.rotateImg(files[files.length - 1].file).then(blob => {
            document.getElementById('imgBox').src = URL.createObjectURL(blob) // 转换后的img
            this.blobToDataURL(blob, function(dataurl) {
              console.log(file.size/1024+'kb')
              const isLt400K = file.size /1024 < 200;
              if(!isLt400K){
                return that.getsmallpic(that.dataURLtoFile(dataurl,file.name))
              }else{
                this.setState({
                  imgArr:files[files.length - 1].file
                })
              }
            });
          });
        }else{
          console.log('pc')
          console.log(file.size/1024+'kb')
          const isLt400K = file.size /1024 < 200;
          if(!isLt400K){
            return this.getsmallpic(file)
          }else{
            this.setState({
              imgArr:files[files.length - 1].file,
              imgSrc:files[files.length - 1].url
            })
          }
        }
      }
    }
  blobToDataURL(blob, callback) {
    var a = new FileReader();
    a.onload = function(e) {
      callback(e.target.result);
    }
    a.readAsDataURL(blob);
    return a;
  }
  
//图片翻转
   rotateImg(file) {
    return new Promise((resolve, reject) => {
      let img = new Image();
      img.src = window.URL.createObjectURL(file);
      img.onload = () => {
        util.addScript('https://cdn.jsdelivr.net/npm/exif-js', () => {
          var EXIF=window.EXIF;
          // 获取图片源数据 上面已经引入EXIF全局变量
          EXIF.getData(img, function () {
            // 获取图片orientation值
            console.log(EXIF.getAllTags(this))
            let orientation = EXIF.getTag(this, "Orientation");
            let canvas = document.createElement("canvas");
            let ctx = canvas.getContext("2d");
            switch (orientation) {
              case 3: // 旋转180°
                canvas.width = img.width;
                canvas.height = img.height;
                ctx.rotate((180 * Math.PI) / 180);
                ctx.drawImage(img, -img.width, -img.height, img.width, img.height);
                break;
              case 6: // 旋转90°
                canvas.width = img.height;
                canvas.height = img.width;
                ctx.rotate((90 * Math.PI) / 180);
                ctx.drawImage(img, 0, -img.height, img.width, img.height);
                break;
              case 8: // 旋转-90°
                canvas.width = img.height;
                canvas.height = img.width;
                ctx.rotate((-90 * Math.PI) / 180);
                ctx.drawImage(img, -img.width, 0, img.width, img.height);
                break;
              default: // 没有源信息的图片和正常的图片是不需要旋转的
                canvas.width = img.width;
                canvas.height = img.height;
                ctx.drawImage(img, 0, 0, img.width, img.height);
                break;
            }
            // 处理完返回 (这里返回都是被压缩的,根据实际情况更改正常的图片处理方式)
            canvas.toBlob(file => resolve(file), 'image/jpeg', 0.8)
          })
        });
      }
    })
  }
 //图片压缩
  dataURLtoFile(dataurl, filename) {
      var arr = dataurl.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 new File([u8arr], filename, {type:mime});
    }
    // 图片压缩
    getsmallpic(file){
      return new Promise((resolve,reject)=>{
        //对图片进行压缩
        const reader = new FileReader()
        const image = new Image()
        image.onload = (imageEvent) => {
          const canvas = document.createElement('canvas');
          const context = canvas.getContext('2d');
          const width = image.width * 0.5
          const height = image.height * 0.5
          canvas.width = width;
          canvas.height = height;
          context.clearRect(0, 0, width, height);
          context.drawImage(image, 0, 0, width, height);
          const dataUrl = canvas.toDataURL(file.type);
          const blobData = this.dataURLtoFile(dataUrl,file.name);
          if(blobData){
            if(blobData.size /1024 < 400){
              Object.assign(blobData,{uid:blobData.lastModified});
              const windowURL = window.URL || window.webkitURL;//实现预览
              resolve(blobData)
              this.setState({
                imgSrc:windowURL.createObjectURL(blobData)
              })
              this.setState({
                imgArr:blobData
              })
            }else{
              this.getsmallpic(blobData)
            }
  
          }else{
            reject()
          }
  
        }
        reader.onload = (e => { image.src = e.target.result; });
        reader.readAsDataURL(file);
      })
    }
  
//css片段
  
.fileStyle{
  position: relative;
  display: block;
  width: 160px;
  height: 160px;
  border-radius: 50%;
  text-align: center;
  background: #f5f5f5;
  margin: 0 auto;
  border: 1px solid #0486FE;
  overflow: hidden;
}
.imgs {
  width: 160px !important;
  height: 160px !important;
  border-radius: 50%;
  position: relative;
}
.fileStyle input {
  position: absolute;
  left: 0;
  top: 0;
  z-index: 9;
  width: 160px;
  height: 160px;
  opacity: 0;
}
:global(.am-image-picker .am-image-picker-list .am-flexbox){
  width: 165px !important;
  height: 165px !important;
  border-radius: 50%;
  position: absolute;
  top:0;
  left: 0;
  background-color: transparent!important;
}
:global(.am-image-picker-list){
  padding: 0 0 0 0!important;
  margin-bottom: 0!important;
}
:global(.am-image-picker-upload-btn){
  border-radius: 50% !important;
  border: 0 !important;
}
:global(.am-image-picker-list .am-image-picker-item .am-image-picker-item-content){
  border-radius: 50% !important;
  border: 0 !important;
}
:global(.am-image-picker-list .am-image-picker-item){
  opacity: 0!important;
}

 

原文链接:https://blog.csdn.net/small_fox_dtt/article/details/106785966

posted @   执手听风吟  阅读(458)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示