HarmonyOS 网络图片如何转换成PixelMap

如何通过网络图片的url转换成图片的PixelMap?

HarmonyOS
2024-12-27 16:43:00
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu
import http from '@ohos.net.http';
import image from '@ohos.multimedia.image';
import { BusinessError } from '@ohos.base';

@Entry
@Component
struct ImageToPixelMap {
  @State image: PixelMap | undefined = undefined;
  httpRequest() {
    http.createHttp().request(
      "https://www.huawei.com",
      (error: BusinessError, data: http.HttpResponse) => {
        if (error) {
          console.error(`http reqeust failed with. Code: ${error.code}, message: ${error.message}`);
        } else {
          console.error('http reqeust success.');
          let imageData: ArrayBuffer = data.result as ArrayBuffer;
          let imageSource: image.ImageSource = image.createImageSource(imageData);
          console.error(`http reqeust size = ${imageData.byteLength}`);
          class tmp {
            height: number = 100
            width: number = 100
          }
          let options: Record<string, number | boolean | tmp> = {
            'alphaType': 0, // 透明度
            'editable': false, // 是否可编辑
            'pixelFormat': 3, // 像素格式
            'scaleMode': 1, // 缩略值
            'size': { height: 100, width: 100 }
          } // 创建图片大小
          imageSource.createPixelMap(options).then((pixelMap: PixelMap) => {
            this.image = pixelMap
          })
        }
      }
    )
  }
  build() {
    Column(){
      Button("获取网络图片")
        .onClick(() => {
          this.httpRequest()
        })
      Image(this.image)
        .height(200).width(200)

    }.width('100%').height('100%')
  }
}
分享
微博
QQ
微信
回复
2024-12-27 19:20:19
相关问题
HarmonyOS 图片转换成ImageBitmap
1324浏览 • 1回复 待解决
HarmonyOS 页面转换成图片
831浏览 • 1回复 待解决
如何base64转换成图片
12188浏览 • 2回复 待解决
HarmonyOS 日期如何转换成毫秒
1010浏览 • 1回复 待解决
HarmonyOS 获取网络图片PixelMap
1072浏览 • 1回复 待解决
HarmonyOS 如何将汉字转换成拼音
1279浏览 • 1回复 待解决
HarmonyOS json转换成对象
1319浏览 • 1回复 待解决
HarmonyOS Object怎么转换成map
1079浏览 • 1回复 待解决
PixelMap类型怎么转换成Base64字符串
1981浏览 • 1回复 待解决
如何将Android的APP转换成鸿蒙的?
1876浏览 • 1回复 已解决
Map类型怎么转换成Json string
3454浏览 • 1回复 待解决
如何将文件转换成字符串
1414浏览 • 1回复 待解决