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

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

前言

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

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

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

developer.huawei.com/consumer/cn...

componentSnapshot 介绍

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

基本使用步骤

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

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

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

2. 获取截图的基本方法

方法一:异步获取(推荐)
typescript 复制代码
import { image } from '@kit.ImageKit';

// 在按钮点击或其他事件中
async takeScreenshot() {
  try {
    // 获取UIContext
    const 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);
}
方法二:同步获取
typescript 复制代码
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. 显示截图结果

typescript 复制代码
// 在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)
  }
}

4. 完整示例代码

typescript 复制代码
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坐标

以往文章

相关推荐
QQ1__8115175153 小时前
Spring boot名城小区物业管理系统信息管理系统源码-SpringBoot后端+Vue前端+MySQL【可直接运行】
前端·vue.js·spring boot
钛态3 小时前
前端微前端架构:大项目的救命稻草还是自找麻烦?
前端·vue·react·web
一粒黑子3 小时前
【实战解析】阿里开源 PageAgent:纯前端 GUI Agent,一行JS让网页支持自然语言操控
前端·javascript·开源
独角鲸网络安全实验室3 小时前
2026微信小程序抓包全解析:从实操落地到合规风控,解锁前端调试新范式
前端·微信小程序·小程序·抓包·系统代理绕过·https证书严格校验·进程隔离
紫微AI3 小时前
前端文本测量成了卡死一切创新的最后瓶颈,pretext实现突破了
前端·人工智能·typescript
GISer_Jing3 小时前
AI前端(From豆包)
前端·aigc·ai编程
IT枫斗者3 小时前
前端部署后如何判断“页面是不是最新”?一套可落地的版本检测方案(适配 Vite/Vue/React/任意 SPA)
前端·javascript·vue.js·react.js·架构·bug
测试修炼手册3 小时前
[测试技术] 深入理解 JSON Web Token (JWT)
前端·json
AI老李3 小时前
2026 年 Web 前端开发的 8 个趋势!
前端
里欧跑得慢3 小时前
15. Web可访问性最佳实践:让每个用户都能平等访问
前端·css·flutter·web