HarmonyOS 鸿蒙Next图片上传竖图变横图
HarmonyOS 鸿蒙Next图片上传竖图变横图 图片上传后竖图会变横图,发现在支付宝中发送图片消息也会有一样的问题
图片自身EXIF中保留图片旋转角度orientation,所以image组件旋转展示
- 通过getImageProperty获取当前图片旋转角度
- 将piexMap旋转后展示
import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { BusinessError } from '@kit.BasicServicesKit';
import fs from '@ohos.file.fs';
import { common } from '@kit.AbilityKit';
import picker from '@ohos.file.picker';
import { fileIo } from '@kit.CoreFileKit';
import { promptAction } from '@kit.ArkUI';
import image from '@ohos.multimedia.image';
// 获取应用文件路径
let context = this as common.UIAbilityContext;
async function savePhotoToGallery(context: common.UIAbilityContext) {
let helper = photoAccessHelper.getPhotoAccessHelper(context);
try {
// onClick触发后5秒内通过createAsset接口创建图片文件,5秒后createAsset权限收回。
let uri = await helper.createAsset(photoAccessHelper.PhotoType.IMAGE, 'jpg');
// 使用uri打开文件,可以持续写入内容,写入过程不受时间限制
let file = await fileIo.open(uri, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
context.resourceManager.getMediaContent($r('app.media.123_2').id, 0)
.then(async value => {
let media = value.buffer;
// 写到媒体库文件中
await fileIo.write(file.fd, media);
await fileIo.close(file.fd);
promptAction.showToast({ message: '已保存至相册!' });
});
} catch (error) {
const err: BusinessError = error as BusinessError;
console.error(`Failed to save photo. Code is ${err.code}, message is ${err.message}`);
}
}
@Entry
@Component
struct xxxxx{
@State url: string = ''
@State pixelMap: image.PixelMap|undefined =undefined
build() {
Column() {
SaveButton({
text: SaveDescription.SAVE_TO_GALLERY,
buttonType: ButtonType.Capsule,
icon: SaveIconStyle.FULL_FILLED
})
.onClick(async (event: ClickEvent, result: SaveButtonOnClickResult) => {
if (result === SaveButtonOnClickResult.SUCCESS) {
const context: common.UIAbilityContext = this as common.UIAbilityContext;
// 免去权限申请和权限请求等环节,获得临时授权,保存对应图片
savePhotoToGallery(context);
} else {
promptAction.showToast({ message: '设置权限失败!' })
}
})
Button('上传')
.onClick(() => {
try {
const photoSelectOptions = new photoAccessHelper.PhotoSelectOptions();
photoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE; // 过滤选择媒体文件类型为IMAGE
photoSelectOptions.maxSelectNumber = 5; // 选择媒体文件的最大数目
let uris: Array<string> = [];
const photoViewPicker = new photoAccessHelper.PhotoViewPicker();
photoViewPicker.select(photoSelectOptions).then(async (photoSelectResult: photoAccessHelper.PhotoSelectResult) => {
uris = photoSelectResult.photoUris;
let file = fs.openSync(uris[0], fs.OpenMode.READ_ONLY);
console.info('file fd: ' + file.fd);
let imageSource: image.ImageSource = image.createImageSource(file.fd)
let options: image.ImagePropertyOptions = { index: 0, defaultValue: '9999' }
//查看当前图片旋转角度
let Orientation = await imageSource.getImageProperty(image.PropertyKey.ORIENTATION, options) //图片方向 Right-top
console.error('Orientation:'+Orientation)
if (Orientation==='Right-top') {
let decodingOptions: image.DecodingOptions = {
editable: true,
desiredPixelFormat: 3,
}
let pixelMap = imageSource.createPixelMapSync(decodingOptions)
//旋转九十度
await pixelMap.rotate(90)
this.pixelMap = pixelMap
} else {
console.error('其他旋转方向xxxxxxxxxxx')
}
console.info('photoViewPicker.select to file succeed and uris are:' + uris);
}).catch((err: BusinessError) => {
console.error(`Invoke photoViewPicker.select failed, code is ${err.code}, message is ${err.message}`);
})
} catch (e) {
console.error(JSON.stringify(e))
}
})
Image(this.pixelMap)
.onError((msg)=>{
console.error('onError:'+JSON.stringify(msg))
})
.onComplete((msg)=>{
console.log('onComplete:'+JSON.stringify(msg))
})
}
}
}
更多关于HarmonyOS 鸿蒙Next图片上传竖图变横图的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
针对“HarmonyOS 鸿蒙Next图片上传竖图变横图”的问题,这通常与图片的EXIF信息或应用处理图片的逻辑有关。在HarmonyOS中,图片上传时若未正确处理EXIF信息(尤其是方向标签),可能导致原本竖拍的图片显示为横图。
解决此问题的方法可能涉及以下几个方面:
-
检查并调整图片EXIF信息:在上传图片前,应用应检查图片的EXIF信息,特别是“Orientation”标签,根据该标签的值对图片进行旋转处理,确保图片显示正确。
-
应用端处理:在HarmonyOS应用中,可以通过图像处理库(如Skia)在上传前对图片进行预处理,确保图片方向正确。
-
服务器端处理:如果应用无法修改,考虑在服务器端接收图片时,检查并调整EXIF信息,再存储或展示图片。
-
用户指导:在应用中添加提示,指导用户在拍摄图片时注意方向,或在上传前使用图片编辑工具调整图片方向。
若以上方法无法直接应用于你的场景或问题依旧存在,可能是特定于你的应用或环境的复杂问题。此时,建议深入检查应用的图片处理逻辑,或考虑与HarmonyOS开发者社区分享问题详情,寻求更具体的帮助。如果问题依旧没法解决请联系官网客服,官网地址是 https://www.itying.com/category-93-b0.html,