中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
微信扫码分享
import image from '@ohos.multimedia.image' @Entry @Component struct Index { @State message: string = '点击获取图片尺寸'; @State imagePixelMap: PixelMap | undefined = undefined; @State imageWidth: number = -1; @State imageHeight: number = -1; build() { Row() { Column() { Button(this.message) .fontSize(30) .width(350) .height(150) .fontWeight(FontWeight.Bold) .onClick(() => { getContext(this).resourceManager.getMediaContent($r("app.media.startIcon")).then((data) => { let arrayBuffer = data.buffer.slice(data.byteOffset, data.byteLength + data.byteOffset) let imageSource: image.ImageSource = image.createImageSource(arrayBuffer); imageSource.getImageInfo((err, value) => { //获取图片资源的尺寸 console.log(`图片的尺寸为:width:${value.size.width}height:${value.size.height}`) if (err) { return; } // 转PixelMap let opts: image.DecodingOptions = { editable: true, desiredSize: { height: value.size.height, width: value.size.width } // desiredSize: { height: 300, width: 200 } }; imageSource.createPixelMap(opts, (err, pixelMap) => { this.imagePixelMap = pixelMap this.imagePixelMap.getImageInfo().then((value) => { this.imageWidth = value.size.width this.imageHeight = value.size.height }) }) }) }) }) Image(this.imagePixelMap).width(this.imageWidth).height(this.imageHeight) Text(`图片尺寸信息:width: ${this.imageWidth}, height: ${this.imageHeight}`) } } } }