应用在处理大尺寸图片时,直接解码整张图片可能导致内存占用过高和解码耗时较长。通过区域解码和下采样解码功能,可以有效优化这一场景:
适用场景:
| 场景 | 推荐方式 | 说明 |
|---|---|---|
| 图片局部放大查看 | 区域解码 | 只解码需要查看的区域。 |
| 大图预览(如4K图片在手机屏幕显示) | 下采样解码 | 降低分辨率,减少内存占用。 |
| 地图/全景图瓦片加载 | 组合使用 | 指定区域并指定输出尺寸。 |
区域解码通过设置解码参数cropRegion,指定需要解码的矩形区域。解码器仅解码该区域的数据。参数详情请参考Image_Region。
Native C版本区域解码需使用OH_DecodingOptions_SetCropRegion接口,而非SetDesiredRegion。SetDesiredRegion接口不生效。
参考使用Image_NativeModule完成图片解码创建OH_ImageSourceNative实例。
设置cropRegion参数执行区域解码。
// 区域解码示例。
napi_value DecodeRegion(napi_env env, napi_callback_info info)
{
OH_DecodingOptions *ops = nullptr;
OH_DecodingOptions_Create(&ops);
// 设置裁剪区域参数,使用SetCropRegion实现区域解码。
Image_Region region = {
.x = 0,
.y = 0,
.width = 1000,
.height = 1000
};
OH_DecodingOptions_SetCropRegion(ops, ®ion);
OH_PixelmapNative_Release(g_thisImageSource->resPixMap);
g_thisImageSource->resPixMap = nullptr;
Image_ErrorCode errCode = OH_ImageSourceNative_CreatePixelmap(g_thisImageSource->source,
ops, &g_thisImageSource->resPixMap);
OH_DecodingOptions_Release(ops);
ops = nullptr;
if (errCode != IMAGE_SUCCESS) {
OH_LOG_ERROR(LOG_APP, "DecodeRegion failed, errCode: %{public}d.", errCode);
return GetJsResult(env, errCode);
}
OH_LOG_INFO(LOG_APP, "DecodeRegion succeeded.");
return GetJsResult(env, errCode);
} 下采样解码通过设置解码参数desiredSize,指定期望输出的图片尺寸。解码器会根据期望尺寸输出PixelMap,减少最终的内存占用。
解码处理差异:
参数详情请参考OH_DecodingOptions。
参考图片开发指导(C/C++)创建OH_ImageSourceNative实例。
设置desiredSize参数执行下采样解码。
// 下采样解码示例。
napi_value DownsampleDecode(napi_env env, napi_callback_info info)
{
OH_DecodingOptions *ops = nullptr;
OH_DecodingOptions_Create(&ops);
// 设置期望输出大小参数。
Image_Size desiredSize = {
.width = 512,
.height = 512
};
OH_DecodingOptions_SetDesiredSize(ops, &desiredSize);
OH_PixelmapNative_Release(g_thisImageSource->resPixMap);
g_thisImageSource->resPixMap = nullptr;
Image_ErrorCode errCode = OH_ImageSourceNative_CreatePixelmap(g_thisImageSource->source,
ops, &g_thisImageSource->resPixMap);
OH_DecodingOptions_Release(ops);
ops = nullptr;
if (errCode != IMAGE_SUCCESS) {
OH_LOG_ERROR(LOG_APP, "DownsampleDecode failed, errCode: %{public}d.", errCode);
return GetJsResult(env, errCode);
}
OH_LOG_INFO(LOG_APP, "DownsampleDecode succeeded.");
return GetJsResult(env, errCode);
} | 策略 | 常量值 | 操作顺序 | 说明 |
|---|---|---|---|
| IMAGE_CROP_AND_SCALE_STRATEGY_SCALE_FIRST | 1 | 先缩放,再裁剪。 | - |
| IMAGE_CROP_AND_SCALE_STRATEGY_CROP_FIRST | 2 | 先裁剪,再缩放。 | 推荐使用,可减少解码峰值内存。 |
推荐使用IMAGE_CROP_AND_SCALE_STRATEGY_CROP_FIRST:先裁剪再缩放可精确控制裁剪区域,保证不同格式解码效果一致。
参数详情请参考Image_CropAndScaleStrategy。
参考图片开发指导(C/C++)创建OH_ImageSourceNative实例。
同时设置cropRegion、desiredSize和cropAndScaleStrategy参数。
// 区域解码与下采样组合使用示例。
napi_value CombinedDecode(napi_env env, napi_callback_info info)
{
OH_DecodingOptions *ops = nullptr;
OH_DecodingOptions_Create(&ops);
Image_Region region = {
.x = 1000,
.y = 500,
.width = 2000,
.height = 2000
};
OH_DecodingOptions_SetCropRegion(ops, ®ion);
Image_Size desiredSize = {
.width = 512,
.height = 512
};
OH_DecodingOptions_SetDesiredSize(ops, &desiredSize);
OH_DecodingOptions_SetCropAndScaleStrategy(ops, IMAGE_CROP_AND_SCALE_STRATEGY_CROP_FIRST);
OH_PixelmapNative_Release(g_thisImageSource->resPixMap);
g_thisImageSource->resPixMap = nullptr;
Image_ErrorCode errCode = OH_ImageSourceNative_CreatePixelmap(g_thisImageSource->source,
ops, &g_thisImageSource->resPixMap);
OH_DecodingOptions_Release(ops);
ops = nullptr;
if (errCode != IMAGE_SUCCESS) {
OH_LOG_ERROR(LOG_APP, "CombinedDecode failed, errCode: %{public}d.", errCode);
return GetJsResult(env, errCode);
}
OH_LOG_INFO(LOG_APP, "CombinedDecode succeeded.");
return GetJsResult(env, errCode);
}