调用imageSource.createPixelMap()报错“Create PixelMap error”

从相册获取到一张图片uri,代码如下:

const file = fs.openSync(uri, fs.OpenMode.READ_ONLY); 
const imageSource = image.createImageSource(file.fd); 
const pixelMap = await imageSource.createPixelMap({sampleSize: 2, rotate: 0});
  • 1.
  • 2.
  • 3.

此时报错“Create PixelMap error”,没有错误码。如果把sampleSize配置去掉则不报错。如何正确设置sampleSize,将图片缩小。

HarmonyOS
2024-03-18 22:29:32
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
jshyb

该问题是sampleSize取值错误导致的,sampleSize表示缩略图采样大小,当前只能取1。可通过DecodingOptions.desiredSize指定输出大小。参考代码如下:

import { BusinessError } from '@ohos.base'; 
import { image } from '@kit.ImageKit'; 
 
let decodingOptions: image.DecodingOptions = { 
  editable: true, 
  desiredPixelFormat: 3, 
  desiredSize: { width: 4, height: 6 } 
} 
const context: Context = getContext(this); 
const filePath: string = context.cacheDir + '/test.jpg'; 
const imageSource: image.ImageSource = image.createImageSource(filePath); 
// 创建pixelMap并进行简单的旋转和缩放 
imageSource.createPixelMap(decodingOptions).then((pixelMap: image.PixelMap) => { 
  console.log("Succeeded in creating PixelMap") 
}).catch((err: BusinessError) => { 
  console.error("Failed to create PixelMap") 
});
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

参考链接

DecodingOptions

分享
微博
QQ
微信
回复
2024-03-19 22:04:45
相关问题
ImageSource.getImageProperty() 报错
2525浏览 • 1回复 待解决
Native Image模块API-OH_PixelMap_CreatePixelMap
2601浏览 • 1回复 待解决
报错Error while Deploying HAP。
13601浏览 • 2回复 待解决
冷启动报错Error message
2970浏览 • 1回复 待解决