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、运行程序

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只小熊猫呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值