调用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});

此时报错“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") 
});

参考链接

DecodingOptions

分享
微博
QQ
微信
回复
2024-03-19 22:04:45
相关问题
ImageSource.getImageProperty() 报错
249浏览 • 1回复 待解决
报错Error while Deploying HAP。
9930浏览 • 2回复 待解决
冷启动报错Error message
29浏览 • 0回复 待解决
neptune 烧写报错[upload] Error -1
4714浏览 • 2回复 待解决
undefined symbol napi_create_threadsafe_function
2272浏览 • 1回复 待解决
PixelMap怎么转Base64?(非Java)
541浏览 • 2回复 待解决