ios html调起拍照,解决HTML5IOS拍照上传图片逆时针旋转90°问题(React)

一般情况下是 是使用  Exif.js 来读取图像的元数据  orientation(方向)

但是这里使用了antd-mobile 的ImagePicker 组件, onChange 函数直接返回了orientation参数,所以不需要使用exif.js了

1. 获取orientation参数和文件参数

onChange = (files, type) => {

if(type === 'add'){

this.setState({

loading:true,

orientation:files[0].orientation

})

this.transformBase64(files[0].file)

}else {

this.setState({loading:true})

this._Upload(this.state.index,'')

}

}

2.转换base64

//转换base64

transformBase64 (file){

const self = this;

const reader = new FileReader();

reader.readAsDataURL(file);

/* eslint-disable func-names */

// 箭头函数会改变this,所以这里不能用肩头函数

reader.onloadend = function () {

// this.result就是转化后的结果

const result = this.result;

// 将base64添加到图片标签上

const img = new Image();

img.src = result;

img.onload = function () {

// 获取旋转后的图片

const data = self.getRotateImg(img,self.state.orientation);

// 如果上传接口不支持base64,则这里需要将base64转为文档流

const file = self.dataURLtoFile(data);

// 调用接口,上传图片

self.transformFileToFormData(file)

}

};

}

3.将File append进 FormData

// 将File append进 FormData

transformFileToFormData (file) {

const formData = new FormData();

// 自定义formData中的内容

// type

formData.append('type', file.type);

// size

formData.append('size', file.size || "image/jpeg");

// name

formData.append('name', file.name);

// lastModifiedDate

formData.append('lastModifiedDate', file.lastModifiedDate);

// append 文件

formData.append('file', file);

// 上传图片

this.uploadImg(formData);

}

4.获取图片返回地址

uploadImg = (formData) => {

const xhr = new XMLHttpRequest();

// 进度监听

// xhr.upload.addEventListener('progress', (e)=>{console.log(e.loaded / e.total)}, false);

// 加载监听

// xhr.addEventListener('load', ()=>{console.log("加载中");}, false);

// 错误监听

xhr.addEventListener('error', ()=>{Toast.fail('图片上传失败', 1)

}, false);

xhr.onreadystatechange = () => {

if (xhr.readyState === 4) {

if(xhr.status === 500){

Toast.fail('图片上传失败', 1)

}

const result = JSON.parse(xhr.responseText);

if (result.status === 10000) {

this._Upload(this.state.status,`http:${result.url}`,result.filename)

} else {

if(this.state.status === 1){

this.setState({

haveAvatar:[]

})

}else if(this.state.status === 2){

this.setState({

haveNationalEmblem:[]

})

}

Toast.fail('图片上传失败', 1)

}

}

};

xhr.open('POST', '/api/file/file/upload' , true);

xhr.send(formData);

};

5.上传图片

_Upload = (status,src,filename) => {

this.setState({

loading:true

})

this.tag = 'faced_id_card'

if(status === 1){

this.tag = "faced_id_card"

}else if(status === 2){

this.tag = "back_id_card"

}

HttpUtils.postForm('/api/face/image/save', {

tag:this.tag,

scheme:'http:',

category:2,

filename

}).then(res => {

this.setState({

loading:false

})

if (res.status === 10000) {

if(src === ''){

Toast.info('删除成功',1)

if(status === 1 ){

this.setState({

haveAvatar:[]

})

}else if(status === 2 ){

this.setState({

haveNationalEmblem:[]

})

}

}else {

Toast.info('上传成功',1)

if(this.state.status === 1 ){

this.setState({

haveAvatar:[{

url:src,

id:Number((Math.random()*10000).toFixed(0))

}],

})

}else if(this.state.status === 2 ){

this.setState({

haveNationalEmblem:[{

url:src,

id:Number((Math.random()*10000).toFixed(0))

}],

})

}

}

} else {

Toast.fail(res.message,1.5)

}

}).catch(err => {

Toast.info('操作失败',1.5)

this.setState({

loading:false

});

})

}

来源:https://www.cnblogs.com/it-Ren/p/12802999.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值