关于HarmonyOS鸿蒙Next照片的压缩咨询
关于HarmonyOS鸿蒙Next照片的压缩咨询 是否有提供压缩照片至指定大小的api 目前相机照片尺寸比较大,而上传至服务器有文件大小限制,需要压缩一下再上传。
2 回复
若是压缩图片尺寸宽高大小,可以参考以下文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/image-transformation-V5,并参考以下demo:
async dealWithPic(targetWidth: number, targetHeight: number, targetPixelSize: number) {
let path = getContext().filesDir + '/pic.png'
// path为已获得的沙箱路径
const imageSource: image.ImageSource = image.createImageSource(path);
let decodingOptions: image.DecodingOptions = {
editable: true
}
//创建pixelMap
this.pixelMap = await imageSource.createPixelMap(decodingOptions);
console.log("6666661" + this.pixelMap.getPixelBytesNumber());
//指定压缩宽、高、大小
// this.packingDetail(500, 500, 100)
if (this.pixelMap) {
console.info("zhaohao before scaling pixelmap." + this.pixelMap.getPixelBytesNumber());
let imageInfo = await this.pixelMap.getImageInfo();
let a = this.pixelMap
//计算压缩比
let scaleX: number = targetWidth / imageInfo.size.width;
let scaleY: number = targetHeight / imageInfo.size.height;
this.pixelMap.scale(scaleX, scaleY, (err: BusinessError) => {
if (err) {
console.error("zhaohao Failed to scale pixelmap.");
return;
} else {
console.log("zhaohao in scaling pixelmap." + a.getPixelBytesNumber());
}
});
}
let imagePackerApi = image.createImagePacker();
let packOpts: image.PackingOption = { format: "image/jpeg", quality: 100 };
imagePackerApi.packing(this.pixelMap, packOpts).then((data: ArrayBuffer) => {
console.info("zhaohao in 100 quality data" + data.byteLength)
this.compressPictures(data, targetPixelSize, 99);
});
}
更多关于关于HarmonyOS鸿蒙Next照片的压缩咨询的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
HarmonyOS鸿蒙Next中的照片压缩主要依赖于其内置的图像处理引擎和算法。系统会自动对照片进行优化和压缩,以减少存储空间占用并提升加载速度。具体来说,鸿蒙Next采用了智能压缩技术,能够在保证图像质量的同时,有效减少文件大小。压缩过程会根据照片的分辨率、色彩深度等因素进行动态调整,确保在不同设备上显示效果一致。此外,鸿蒙Next还支持用户手动设置压缩级别,以满足个性化需求。压缩后的照片仍保留较高的清晰度,适合日常使用和分享。