[鸿蒙/Harmony] HarmonyOS Codelab挑战赛记录之图片的四种常见操作

本文主要是介绍[鸿蒙/Harmony] HarmonyOS Codelab挑战赛记录之图片的四种常见操作,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

上一期介绍了组件模块-Java 布局,这一期主要展示图片的四种常见操作

目录

1.HarmonyOS开发工具的安装

2.组件模块-Java 布局

3.基于图像模块实现图库图片的四种常见操作

 1.准备一张像素尺寸为1024*768的图片

放到ImageDemo\entry\src\main\resources\base\media目录下

2.设计布局样式。功能上我打算能使图片旋转、裁剪、缩放、镜像

所以我需要创建的布局里边有四个按钮,以及一个图片控件。我创建了一个ability_pixel_map的layout样式

相对来讲比较简单,

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_parent"ohos:width="match_parent"ohos:orientation="vertical"><DirectionalLayoutohos:height="200"ohos:width="match_parent"ohos:orientation="horizontal"ohos:layout_direction="locale"ohos:top_margin="20vp"><Buttonohos:id="$+id:whirl_image"ohos:height="50"ohos:width="200"ohos:text="旋转"ohos:text_size="18fp"/><Buttonohos:id="$+id:crop_image"ohos:height="50"ohos:width="200"ohos:text="裁剪"ohos:text_size="18fp"/><Buttonohos:id="$+id:scale_image"ohos:height="50"ohos:width="200"ohos:text="缩放"ohos:text_size="18fp"/><Buttonohos:id="$+id:mirror_image"ohos:height="50"ohos:width="200"ohos:text="镜像"ohos:text_size="18fp"/></DirectionalLayout><Imageohos:id="$+id:image"ohos:height="1024"ohos:width="1024"ohos:background_element="#000"/></DirectionalLayout>

设计效果(为了更直观看到Image,我给他设置了黑色背景)

3.在slice文件夹下继续创建一个类:PixelMapSlice 并且继承 AbilitySlice

4.编写一个图片转换的方法,可以将图片的资源id转换成PixelMap

(这里的代码主要展示对图片处理的核心操作)

    /*** 通过图片ID返回PixelMap ** @param resourceId 图片的资源ID * @return 图片的PixelMap*/private PixelMap getPixelMapFromResource(int resourceId) {InputStream inputStream = null;try {// 创建图像数据源ImageSource对象 inputStream = getContext().getResourceManager().getResource(resourceId);ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions();srcOpts.formatHint = "image/jpg";ImageSource imageSource = ImageSource.create(inputStream, srcOpts);// 设置图片参数。本例使用图片像素的尺寸为1024*768// 点击一次旋转按钮会进行90度的旋转,// 缩放是按照2:1的比例进行缩放,// 剪裁是保证宽度不变的情况下对高度进行400像素的剪裁,ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();// 旋转(通过改变whirlCount变量控制旋转)decodingOptions.rotateDegrees = 90 * whirlCount;// 缩放(通过改变isScale变量控制缩放)decodingOptions.desiredSize = new Size(isScale ? 512 : 0, isScale ? 384 : 0);// 剪裁(通过改变isCorp变量控制裁剪区域)decodingOptions.desiredRegion = new Rect(0, 0, isCorp ? 1024 : 0, isCorp ? 400 : 0);decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;return imageSource.createPixelmap(decodingOptions);} catch (IOException e) {HiLog.info(LABEL_LOG, "IOException");} catch (NotExistException e) {HiLog.info(LABEL_LOG, "NotExistException");} finally {if (inputStream != null) {try {inputStream.close();} catch (IOException e) {HiLog.info(LABEL_LOG, "inputStream IOException");}}}return null;}

5.完整代码

package com.huawei.codelab.slice;import com.huawei.codelab.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.PixelMapHolder;
import ohos.global.resource.NotExistException;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;import java.io.IOException;
import java.io.InputStream;/*** 图像主页面 */
public class PixelMapSlice extends AbilitySlice {private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "PixelMapSlice");Image image;PixelMap imagePixelMap;Button whirlImageBtn;Button cropImageBtn;Button scaleImageBtn;Button mirrorImageBtn;private int whirlCount = 0;private boolean isCorp = false;private boolean isScale = false;private boolean isMirror = false;private float scaleX = 1.0f;@Overridepublic void onStart(Intent intent) {super.onStart(intent);super.setUIContent(ResourceTable.Layout_ability_pixel_map);initView();}private void initView() {if (findComponentById(ResourceTable.Id_whirl_image) instanceof Button) {whirlImageBtn = (Button) findComponentById(ResourceTable.Id_whirl_image);}if (findComponentById(ResourceTable.Id_crop_image) instanceof Button) {cropImageBtn = (Button) findComponentById(ResourceTable.Id_crop_image);}if (findComponentById(ResourceTable.Id_scale_image) instanceof Button) {scaleImageBtn = (Button) findComponentById(ResourceTable.Id_scale_image);}if (findComponentById(ResourceTable.Id_mirror_image) instanceof Button) {mirrorImageBtn = (Button) findComponentById(ResourceTable.Id_mirror_image);}if (findComponentById(ResourceTable.Id_image) instanceof Image) {image = (Image) findComponentById(ResourceTable.Id_image);}whirlImageBtn.setClickedListener(new ButtonClick());cropImageBtn.setClickedListener(new ButtonClick());scaleImageBtn.setClickedListener(new ButtonClick());mirrorImageBtn.setClickedListener(new ButtonClick());}private class ButtonClick implements Component.ClickedListener {@Overridepublic void onClick(Component component) {int btnId = component.getId();switch (btnId) {case ResourceTable.Id_whirl_image:// 旋转图片 whirlCount++;isCorp = false;isScale = false;isMirror = false;imagePixelMap = getPixelMapFromResource(ResourceTable.Media_ic_sponge_baby);image.setPixelMap(imagePixelMap);break;case ResourceTable.Id_crop_image:// 剪裁图片 whirlCount = 0;isCorp = !isCorp;isScale = false;isMirror = false;imagePixelMap = getPixelMapFromResource(ResourceTable.Media_ic_sponge_baby);image.setPixelMap(imagePixelMap);break;case ResourceTable.Id_scale_image:// 缩放图片 whirlCount = 0;isCorp = false;isScale = !isScale;isMirror = false;imagePixelMap = getPixelMapFromResource(ResourceTable.Media_ic_sponge_baby);image.setPixelMap(imagePixelMap);break;case ResourceTable.Id_mirror_image:// 镜像图片 whirlCount = 0;isCorp = false;isScale = false;isMirror = true;imagePixelMap = getPixelMapFromResource(ResourceTable.Media_ic_sponge_baby);mirrorImage(imagePixelMap);image.setPixelMap(imagePixelMap);break;default:break;}}}private void mirrorImage(PixelMap pixelMap) {scaleX = -scaleX;image.addDrawTask(new Component.DrawTask() {@Overridepublic void onDraw(Component component, Canvas canvas) {if (isMirror) {isMirror = false;PixelMapHolder pmh = new PixelMapHolder(pixelMap);canvas.scale(scaleX,1.0f,(float) pixelMap.getImageInfo().size.width / 2,(float) pixelMap.getImageInfo().size.height / 2);canvas.drawPixelMapHolder(pmh,0,0,new Paint());}}});}/*** 通过图片ID返回PixelMap ** @param resourceId 图片的资源ID * @return 图片的PixelMap*/private PixelMap getPixelMapFromResource(int resourceId) {InputStream inputStream = null;try {// 创建图像数据源ImageSource对象 inputStream = getContext().getResourceManager().getResource(resourceId);ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions();srcOpts.formatHint = "image/jpg";ImageSource imageSource = ImageSource.create(inputStream, srcOpts);// 设置图片参数。本例使用图片像素的尺寸为1024*768// 点击一次旋转按钮会进行90度的旋转,// 缩放是按照2:1的比例进行缩放,// 剪裁是保证宽度不变的情况下对高度进行400像素的剪裁,ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();// 旋转 decodingOptions.rotateDegrees = 90 * whirlCount;// 缩放 decodingOptions.desiredSize = new Size(isScale ? 512 : 0, isScale ? 384 : 0);// 剪裁 decodingOptions.desiredRegion = new Rect(0, 0, isCorp ? 1024 : 0, isCorp ? 400 : 0);decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;return imageSource.createPixelmap(decodingOptions);} catch (IOException e) {HiLog.info(LABEL_LOG, "IOException");} catch (NotExistException e) {HiLog.info(LABEL_LOG, "NotExistException");} finally {if (inputStream != null) {try {inputStream.close();} catch (IOException e) {HiLog.info(LABEL_LOG, "inputStream IOException");}}}return null;}@Overridepublic void onActive() {super.onActive();}@Overridepublic void onForeground(Intent intent) {super.onForeground(intent);}
} 

6.运行应用。我们通过上期组件模块-Java 布局 MainAbilitySlice类的点击事件,更改TabListSlice为PixelMapSlice启动我们的布局

运行效果

 

点击这里可以去到HarmonyOS开发文档学习基于图像模块实现图库图片的四种常见操作

 

这篇关于[鸿蒙/Harmony] HarmonyOS Codelab挑战赛记录之图片的四种常见操作的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/282063

相关文章

Python中win32包的安装及常见用途介绍

《Python中win32包的安装及常见用途介绍》在Windows环境下,PythonWin32模块通常随Python安装包一起安装,:本文主要介绍Python中win32包的安装及常见用途的相关... 目录前言主要组件安装方法常见用途1. 操作Windows注册表2. 操作Windows服务3. 窗口操作

mysql表操作与查询功能详解

《mysql表操作与查询功能详解》本文系统讲解MySQL表操作与查询,涵盖创建、修改、复制表语法,基本查询结构及WHERE、GROUPBY等子句,本文结合实例代码给大家介绍的非常详细,感兴趣的朋友跟随... 目录01.表的操作1.1表操作概览1.2创建表1.3修改表1.4复制表02.基本查询操作2.1 SE

ModelMapper基本使用和常见场景示例详解

《ModelMapper基本使用和常见场景示例详解》ModelMapper是Java对象映射库,支持自动映射、自定义规则、集合转换及高级配置(如匹配策略、转换器),可集成SpringBoot,减少样板... 目录1. 添加依赖2. 基本用法示例:简单对象映射3. 自定义映射规则4. 集合映射5. 高级配置匹

在Spring Boot中集成RabbitMQ的实战记录

《在SpringBoot中集成RabbitMQ的实战记录》本文介绍SpringBoot集成RabbitMQ的步骤,涵盖配置连接、消息发送与接收,并对比两种定义Exchange与队列的方式:手动声明(... 目录前言准备工作1. 安装 RabbitMQ2. 消息发送者(Producer)配置1. 创建 Spr

深度解析Python装饰器常见用法与进阶技巧

《深度解析Python装饰器常见用法与进阶技巧》Python装饰器(Decorator)是提升代码可读性与复用性的强大工具,本文将深入解析Python装饰器的原理,常见用法,进阶技巧与最佳实践,希望可... 目录装饰器的基本原理函数装饰器的常见用法带参数的装饰器类装饰器与方法装饰器装饰器的嵌套与组合进阶技巧

c++中的set容器介绍及操作大全

《c++中的set容器介绍及操作大全》:本文主要介绍c++中的set容器介绍及操作大全,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录​​一、核心特性​​️ ​​二、基本操作​​​​1. 初始化与赋值​​​​2. 增删查操作​​​​3. 遍历方

Mysql常见的SQL语句格式及实用技巧

《Mysql常见的SQL语句格式及实用技巧》本文系统梳理MySQL常见SQL语句格式,涵盖数据库与表的创建、删除、修改、查询操作,以及记录增删改查和多表关联等高级查询,同时提供索引优化、事务处理、临时... 目录一、常用语法汇总二、示例1.数据库操作2.表操作3.记录操作 4.高级查询三、实用技巧一、常用语

MySQL追踪数据库表更新操作来源的全面指南

《MySQL追踪数据库表更新操作来源的全面指南》本文将以一个具体问题为例,如何监测哪个IP来源对数据库表statistics_test进行了UPDATE操作,文内探讨了多种方法,并提供了详细的代码... 目录引言1. 为什么需要监控数据库更新操作2. 方法1:启用数据库审计日志(1)mysql/mariad

springboot如何通过http动态操作xxl-job任务

《springboot如何通过http动态操作xxl-job任务》:本文主要介绍springboot如何通过http动态操作xxl-job任务的问题,具有很好的参考价值,希望对大家有所帮助,如有错... 目录springboot通过http动态操作xxl-job任务一、maven依赖二、配置文件三、xxl-

python 常见数学公式函数使用详解(最新推荐)

《python常见数学公式函数使用详解(最新推荐)》文章介绍了Python的数学计算工具,涵盖内置函数、math/cmath标准库及numpy/scipy/sympy第三方库,支持从基础算术到复杂数... 目录python 数学公式与函数大全1. 基本数学运算1.1 算术运算1.2 分数与小数2. 数学函数