赞
踩
在HarmonyOS中对图片的解码和编码是常见的操作,在HarmonyOS怎么进行编码和解码呢?今天写一个demo进行实现该功能实现,我们从以下几个步骤进行讲解该功能
1. 准备环节
2. 图片编码为Base64字符串
3. Base64解码为PixelMap,并显示在Image控件上
1.1 准备一张图片放在resources/rawfile文件夹下,图片如下
1.2 新建ImageAbilitySlice java类和xml布局, xml布局如下
- <?xml version="1.0" encoding="utf-8"?>
- <DirectionalLayout
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:height="match_parent"
- ohos:width="match_parent"
- ohos:orientation="vertical">
- <Text
- ohos:height="100vp"
- ohos:width="match_parent"
- ohos:background_element="#ffffff"
- ohos:text="图片编码"
- ohos:text_size="20vp"
- ohos:text_alignment="center"
- ohos:id="$+id:textRead"/>
- <Text
- ohos:id="$+id:getmPixelMapAndShowImage"
- ohos:height="100vp"
- ohos:width="match_parent"
- ohos:text="图片解码并显示"
- ohos:background_element="#ed6262"
- ohos:text_size="20vp"
- ohos:text_alignment="center"/>
-
- <Image
- ohos:height="match_parent"
- ohos:width="match_parent"
- ohos:id="$+id:ShowImage"/>
-
- </DirectionalLayout>
XML效果图如下
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。