[鸿蒙/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

相关文章

Java图片压缩三种高效压缩方案详细解析

《Java图片压缩三种高效压缩方案详细解析》图片压缩通常涉及减少图片的尺寸缩放、调整图片的质量(针对JPEG、PNG等)、使用特定的算法来减少图片的数据量等,:本文主要介绍Java图片压缩三种高效... 目录一、基于OpenCV的智能尺寸压缩技术亮点:适用场景:二、JPEG质量参数压缩关键技术:压缩效果对比

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

C/C++错误信息处理的常见方法及函数

《C/C++错误信息处理的常见方法及函数》C/C++是两种广泛使用的编程语言,特别是在系统编程、嵌入式开发以及高性能计算领域,:本文主要介绍C/C++错误信息处理的常见方法及函数,文中通过代码介绍... 目录前言1. errno 和 perror()示例:2. strerror()示例:3. perror(

Python获取中国节假日数据记录入JSON文件

《Python获取中国节假日数据记录入JSON文件》项目系统内置的日历应用为了提升用户体验,特别设置了在调休日期显示“休”的UI图标功能,那么问题是这些调休数据从哪里来呢?我尝试一种更为智能的方法:P... 目录节假日数据获取存入jsON文件节假日数据读取封装完整代码项目系统内置的日历应用为了提升用户体验,

Go标准库常见错误分析和解决办法

《Go标准库常见错误分析和解决办法》Go语言的标准库为开发者提供了丰富且高效的工具,涵盖了从网络编程到文件操作等各个方面,然而,标准库虽好,使用不当却可能适得其反,正所谓工欲善其事,必先利其器,本文将... 目录1. 使用了错误的time.Duration2. time.After导致的内存泄漏3. jsO

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

使用C#代码在PDF文档中添加、删除和替换图片

《使用C#代码在PDF文档中添加、删除和替换图片》在当今数字化文档处理场景中,动态操作PDF文档中的图像已成为企业级应用开发的核心需求之一,本文将介绍如何在.NET平台使用C#代码在PDF文档中添加、... 目录引言用C#添加图片到PDF文档用C#删除PDF文档中的图片用C#替换PDF文档中的图片引言在当

详解C#如何提取PDF文档中的图片

《详解C#如何提取PDF文档中的图片》提取图片可以将这些图像资源进行单独保存,方便后续在不同的项目中使用,下面我们就来看看如何使用C#通过代码从PDF文档中提取图片吧... 当 PDF 文件中包含有价值的图片,如艺术画作、设计素材、报告图表等,提取图片可以将这些图像资源进行单独保存,方便后续在不同的项目中使

Java实现文件图片的预览和下载功能

《Java实现文件图片的预览和下载功能》这篇文章主要为大家详细介绍了如何使用Java实现文件图片的预览和下载功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... Java实现文件(图片)的预览和下载 @ApiOperation("访问文件") @GetMapping("

MyBatis 动态 SQL 优化之标签的实战与技巧(常见用法)

《MyBatis动态SQL优化之标签的实战与技巧(常见用法)》本文通过详细的示例和实际应用场景,介绍了如何有效利用这些标签来优化MyBatis配置,提升开发效率,确保SQL的高效执行和安全性,感... 目录动态SQL详解一、动态SQL的核心概念1.1 什么是动态SQL?1.2 动态SQL的优点1.3 动态S