当前位置: 首页 > news >正文

可可图片编辑 HarmonyOS(6)水印效果

可可图片编辑 HarmonyOS(6)水印效果

前言

可可图片编辑 也实现了水印效果,这个功能的实现比较取巧。

在预览阶段,直接通过层叠布局来实现水印效果。

在保存图片时,使用组件截图 componentSnapshot 的知识实现保存图片。

https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-arkui-componentsnapshot#componentsnapshotgetdeprecated

image-20250912220119958

componentSnapshot 介绍

componentSnapshot 是 HarmonyOS提供的一个强大功能,允许开发者获取应用中组件的截图,生成 PixelMap 格式的图片数据。这对于实现分享功能内容保存动态生成图片等场景非常有用。

基本使用步骤

1. 准备工作:添加组件标识

首先需要为想要截图的组件添加唯一标识:

// 在build方法中为组件添加id
build() {Column() {// 需要截图的组件Column() {Text('这是要截图的内容').fontSize(20)Image($r('app.media.icon')).width(100).height(100)}.id('targetComponent') // 添加唯一标识// 其他UI组件...}
}

2. 获取截图的基本方法

方法一:异步获取(推荐)
import { image } from '@kit.ImageKit';// 在按钮点击或其他事件中
async takeScreenshot() {try {// 获取UIContextconst uiContext = this.getUIContext();// 使用componentSnapshot获取截图const pixelMap = await uiContext.getComponentSnapshot().get('targetComponent', { scale: 1.0, // 缩放比例waitUntilRenderFinished: true // 等待渲染完成});// 处理截图结果this.handleScreenshotResult(pixelMap);} catch (error) {console.error('截图失败:', error);}
}// 处理截图结果
handleScreenshotResult(pixelMap: image.PixelMap) {// 可以显示在Image组件中this.screenshotImage = pixelMap;// 或者保存到文件// this.saveToFile(pixelMap);
}
方法二:同步获取
takeScreenshotSync() {try {const uiContext = this.getUIContext();const pixelMap = uiContext.getComponentSnapshot().getSync('targetComponent', {scale: 0.8,waitUntilRenderFinished: true});this.handleScreenshotResult(pixelMap);} catch (error) {console.error('同步截图失败:', error);}
}

3. 显示截图结果

// 在build方法中显示截图
build() {Column() {// 原始内容Column() {// ...原有内容}.id('targetComponent')// 截图结果显示Image(this.screenshotImage).width(200).height(200).margin(10).visibility(this.screenshotImage ? Visibility.Visible : Visibility.None)// 截图按钮Button('截图').onClick(() => this.takeScreenshot()).margin(10)}
}

image-20250912223003666

4. 完整示例代码

import { image } from '@kit.ImageKit';@Entry
@Component
struct ScreenshotExample {@State screenshotImage: image.PixelMap | undefined = undefined;// 异步截图方法async takeScreenshot() {try {const uiContext = this.getUIContext();const pixelMap = await uiContext.getComponentSnapshot().get('contentToCapture', {scale: 1.0,waitUntilRenderFinished: true});this.screenshotImage = pixelMap;} catch (error) {console.error('截图失败:', error);}}build() {Column({ space: 20 }) {// 目标截图区域Column() {Text('欢迎使用组件截图功能').fontSize(24).fontColor(Color.Blue)Text('这是可以截图的内容区域').fontSize(16).margin({ top: 10 })Image($r('app.media.logo')).width(120).height(120).margin({ top: 20 })}.id('contentToCapture').padding(20).border({ width: 2, color: Color.Gray })// 截图结果显示if (this.screenshotImage) {Image(this.screenshotImage).width(300).height(200).border({ width: 1, color: Color.Black })}// 操作按钮Button('截图').width(120).onClick(() => this.takeScreenshot())}.width('100%').padding(20)}
}

5. SnapshotOptions 重要参数说明

配置选项:

  • scale: number - 缩放比例(0.1-1.0),默认1.0
  • waitUntilRenderFinished: boolean - 是否等待渲染完成(推荐设为true)
  • region: Object - 指定截图区域
    • start: number - 起始x坐标
    • top: number - 起始y坐标
    • end: number - 结束x坐标
    • bottom: number - 结束y坐标

以往文章

  • 我的iMaHarmonyOS 知识库接入 鸿蒙6 API20 新特性了

    https://mp.weixin.qq.com/s/YsbFQyi5PsndpRUaA9h_dA?token=830743671&lang=zh_CN

  • 鸿蒙创新赛 HarmonyOS 6.0.0(20) 关键特性汇总

    https://mp.weixin.qq.com/s/ERmgSAIT8B8njFqtyPx9IQ

  • 可可图片编辑 HarmonyOS(4)图片裁剪-canvas

    https://mp.weixin.qq.com/s/2M3f_LNnt0YRo0qEnCnqpA

  • 可可图片编辑 HarmonyOS(3)应用间分享图片

    https://mp.weixin.qq.com/s/n8kkdougB4lqDHjYC7co8g

近期活动

最近想要想要考取 HarmonyOS 基础或者高级证书,或者快要获取的同学都可以点击这个链接,加入我的班级,考取成功有机会获得鸿蒙礼盒一份。

https://developer.huawei.com/consumer/cn/training/classDetail/7e230b074eaa41c587c71c1d1a9a6514?type=1%3Fha_source%3Dhmosclass&ha_sourceId=89000248

联系我

带你了解更多HarmonyOS相关的资讯。


文章转载自:

http://oyiB0v4Y.qbykf.cn
http://mOeFfzxV.qbykf.cn
http://OkyhziGb.qbykf.cn
http://hLgxCgez.qbykf.cn
http://sUu0r1hD.qbykf.cn
http://Vr1dTSys.qbykf.cn
http://jOl3AFNp.qbykf.cn
http://FoMbpRIZ.qbykf.cn
http://vFl31NSP.qbykf.cn
http://S7h6V0jS.qbykf.cn
http://9GNGrwr1.qbykf.cn
http://g7ZXWgN4.qbykf.cn
http://1G2hmZEf.qbykf.cn
http://ZraXE6Wc.qbykf.cn
http://C7mVZPCH.qbykf.cn
http://c8PH4h45.qbykf.cn
http://nlW6dfZK.qbykf.cn
http://SK6Av3Fe.qbykf.cn
http://Y2DSdE67.qbykf.cn
http://0mBotfdt.qbykf.cn
http://ARw6lQjO.qbykf.cn
http://lfJ7roc3.qbykf.cn
http://fgjv8Kdk.qbykf.cn
http://akKYPmOW.qbykf.cn
http://4SL45pUa.qbykf.cn
http://AvKk9o32.qbykf.cn
http://tY0ulYFu.qbykf.cn
http://D4RsxdCQ.qbykf.cn
http://d1My0EBm.qbykf.cn
http://DyBt4XQQ.qbykf.cn
http://www.dtcms.com/a/383520.html

相关文章:

  • 机器学习(四):支持向量机
  • 给定一个有序的正数数组arr和一个正数range,如果可以自由选择arr中的数字,想累加得 到 1~range 范围上所有的数,返回arr最少还缺几个数。
  • 《C++ 容器适配器:stack、queue 与 priority_queue 的设计》
  • Java 黑马程序员学习笔记(进阶篇8)
  • 无需标注的视觉模型 dinov3 自监督学习ssl
  • 多语言编码Agent解决方案(2)-后端服务实现
  • STM32F103C8T6通过SPI协议驱动74HC595数码管完全指南:从硬件原理到级联实现
  • 【系列文章】Linux中的并发与竞争[05]-互斥量
  • 海岛奇兵声纳活动的数学解答
  • 大模型入门实践指南
  • CSS 编码规范
  • Redis框架详解
  • Redis----缓存策略和注意事项
  • Redis的大key问题
  • 微服务学习笔记25版
  • 地址映射表
  • AI Agent 软件工程关键技术综述
  • 命令行工具篇 | grep, findstr
  • 6【鸿蒙/OpenHarmony/NDK】多线程调用 JS 总崩溃?用 napi_create_threadsafe_function 搞定线程安全交互
  • OpenTenBase分布式HTAP实战:从Oracle迁移到云原生数据库的完整指南
  • LabVIEW信号监测与分析
  • 【大模型算法工程师面试题】大模型领域新兴的主流库有哪些?
  • Java队列(从内容结构到经典练习一步到位)
  • Cherno OpenGL 教程
  • RT-DETRv2 中的坐标回归机制深度解析:为什么用 `sigmoid(inv_sigmoid(ref) + delta)` 而不是除以图像尺寸?
  • OpenCV入门教程
  • 深度学习-计算机视觉-目标检测三大算法-R-CNN、SSD、YOLO
  • 冰火两重天:AI重构下的IT就业图景
  • 从ENIAC到Linux:计算机技术与商业模式的协同演进——云原生重塑闭源主机,eBPF+WebAssembly 双引擎的“Linux 内核即服务”实践
  • 从 MySQL 迁移到 GoldenDB,上来就踩了一个坑。