HarmonyOS之图片操作

news/2024/7/21 9:09:20 标签: HarmonyOS, 鸿蒙开发, 图片操作

1、创建工程

HarmonyOS之Hello World

2、修改MainAbilitySlice类

package com.example.myapplication.slice;

import com.example.myapplication.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 MainAbilitySlice extends AbilitySlice {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "MainAbilitySlice");
    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;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        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 {
        @Override
        public 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_camera);
                    image.setPixelMap(imagePixelMap);
                    break;
                case ResourceTable.Id_crop_image:
                    // 剪裁图片
                    whirlCount = 0;
                    isCorp = !isCorp;
                    isScale = false;
                    isMirror = false;
                    imagePixelMap = getPixelMapFromResource(ResourceTable.Media_camera);
                    image.setPixelMap(imagePixelMap);
                    break;
                case ResourceTable.Id_scale_image:
                    // 缩放图片
                    whirlCount = 0;
                    isCorp = false;
                    isScale = !isScale;
                    isMirror = false;
                    imagePixelMap = getPixelMapFromResource(ResourceTable.Media_camera);
                    image.setPixelMap(imagePixelMap);
                    break;
                case ResourceTable.Id_mirror_image:
                    // 镜像图片
                    whirlCount = 0;
                    isCorp = false;
                    isScale = false;
                    isMirror = true;
                    imagePixelMap = getPixelMapFromResource(ResourceTable.Media_camera);
                    mirrorImage(imagePixelMap);
                    image.setPixelMap(imagePixelMap);
                    break;
                default:
                    break;
            }
        }
    }

    private void mirrorImage(PixelMap pixelMap) {
        scaleX = -scaleX;
        image.addDrawTask(
                new Component.DrawTask() {
                    @Override
                    public 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);

            // 设置图片参数
            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;
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

3、修改ability_main.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="match_content"
        ohos:width="match_content"
        ohos:layout_alignment="horizontal_center"
        ohos:text="HarmonyOS图像开发"
        ohos:text_size="80"
        ohos:top_margin="40vp"
        />

    <DirectionalLayout
        xmlns:ohos="http://schemas.huawei.com/res/ohos"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:layout_alignment="horizontal_center"
        ohos:orientation="horizontal"
        ohos:top_margin="20vp">

        <Button
            ohos:id="$+id:whirl_image"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:background_element="$graphic:background_button"
            ohos:padding="12vp"
            ohos:right_margin="5vp"
            ohos:text="旋转"
            ohos:text_size="20vp"
            ohos:top_margin="10vp">
        </Button>

        <Button
            ohos:id="$+id:crop_image"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:background_element="$graphic:background_button"
            ohos:left_margin="5vp"
            ohos:padding="12vp"
            ohos:text="剪裁"
            ohos:text_size="20vp"
            ohos:top_margin="10vp">
        </Button>

        <Button
            ohos:id="$+id:scale_image"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:background_element="$graphic:background_button"
            ohos:left_margin="5vp"
            ohos:padding="12vp"
            ohos:text="缩放"
            ohos:text_size="20vp"
            ohos:top_margin="10vp">
        </Button>

        <Button
            ohos:id="$+id:mirror_image"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:background_element="$graphic:background_button"
            ohos:left_margin="5vp"
            ohos:padding="12vp"
            ohos:text="镜像"
            ohos:text_size="20vp"
            ohos:top_margin="10vp"/>
    </DirectionalLayout>

    <Image
        ohos:id="$+id:image"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:image_src="$media:camera"
        ohos:layout_alignment="horizontal_center"
        ohos:top_margin="20vp">
    </Image>

</DirectionalLayout>

4、更改background_ability_main.xml

  • 重命名

background_button.xml

  • 修改文件内容
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
       ohos:shape="rectangle">
    <corners
        ohos:radius="40"/>
    <solid
        ohos:color="#e9e9e9"/>
</shape>

5、准备图片

将图片(camera.jpg)放入entry\src\main\resources\base\media目录下
在这里插入图片描述

6、运行程序

在这里插入图片描述
在这里插入图片描述


http://www.niftyadmin.cn/n/1229912.html

相关文章

系统程序员成长计划-动态数组(四)

转载时请注明出处和作者联系方式 文章出处&#xff1a;http://www.limodev.cn/blog 作者联系方式&#xff1a;李先静 <xianjimli at hotmail dot com> 有序数组的两个应用 前面我们学习了数组的排序方法&#xff0c;通常我们对数组排序不是为了排序而排序&#xff0c;而…

系统程序员成长计划-组合的威力(一)

系统程序员成长计划-组合的威力&#xff08;一&#xff09;转载时请注明出处和作者联系方式 文章出处&#xff1a;http://www.limodev.cn/blog 作者联系方式&#xff1a;李先静 <xianjimli at hotmail dot com> 在《设计模式-可复用面向对象软件的基础》的序言里提到软件…

HarmonyOS之页面开发

软件下载地址&#xff1a;https://developer.harmonyos.com/cn/develop/deveco-studio#download 当前使用版本号&#xff1a;deveco-studio-2.1.0.303 1、使用XML方式开发页面 使用DependentLayout布局&#xff0c;通过Text和Button组件来实现 1.1、创建项目 过程与HarmonyOS之…

Oracle数据库重复数据删除的三种情况

在对数据库进行操作过程中我们可能会遇到这种情况&#xff0c;表中的数据可能重复出现&#xff0c;使我们对数据库的操作过程中带来很多的不便&#xff0c;那么怎么删除这些重复没有用的数据呢&#xff1f; 重复数据删除技术可以提供更大的备份容量&#xff0c;实现更长时间的数…

优化数据库大幅度提高Oracle的性能

几个简单的步骤大幅提高Oracle性能--我优化数据库的三板斧。 数据库优化的讨论可以说是一个永恒的主题。资深的Oracle优化人员通常会要求提出性能问题的人对数据库做一个statspack&#xff0c;贴出数据库配置等等。还有的人认为要抓出执行最慢的语句来进行优化。但实际情况是…

HarmonyOS系统架构

概述 HarmonyOS采用分层架构&#xff0c;共四层&#xff0c;从下往上依次为&#xff1a;内核层、系统服务层、框架层和应用层。 系统功能按照“系统>子系统>功能/模块”逐级展开&#xff0c;在多设备部署场景下&#xff0c;支持根据实际需求裁剪某些非必要的子系统或功能…

性能调校

什么是性能调优呢&#xff1f;一般是当用户抱怨“太慢了”、“性能不足”、“软硬件需要升级了”等问题时&#xff0c;提供较佳的性能。但不是要解决用户所说的“这系统毁了”、“它不会工作了”等问题&#xff0c;这可能需要的是备援回滚、提高系统可获得性&#xff08;HA hig…