HarmonyOS 如何给图片右下角增加文字水印

我看到官方demo 添加水印如下

import { image } from '@kit.ImageKit'

@Entry
@Component
struct Index {
  @State pixelMap: PixelMap | null = null
  aboutToAppear(): void {
    getContext(this).resourceManager.getMediaContent($r("app.media.startIcon")).then((data) => {
      let arrayBuffer = data.buffer.slice(data.byteOffset, data.byteLength + data.byteOffset)
      let imageSource: image.ImageSource = image.createImageSource(arrayBuffer);
      imageSource.getImageInfo((err, value) => {
        if (err) {
          return;
        }
        let opts: image.DecodingOptions =
          { editable: true, desiredSize: { height: value.size.height, width: value.size.width } };
        imageSource.createPixelMap(opts, (err,
          pixelMap) => {
          // 这个回调的pixelMap就是当前图片的pixelMap
          let offScreenCanvas = new OffscreenCanvas(value.size.width, value.size.height)
          let OffScreenContext = offScreenCanvas.getContext("2d")
          OffScreenContext.drawImage(pixelMap, 0, 0, offScreenCanvas.width, offScreenCanvas.height)
          OffScreenContext.fillStyle = '#30000000'
          OffScreenContext.font = "50px"
          OffScreenContext.fillText("水印水印水印水印水印", 20, 30)
          // 获取新的PixelMap
          this.pixelMap = OffScreenContext.getPixelMap(0, 0, offScreenCanvas.width, offScreenCanvas.height)
        })
      })
    })
  }
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Image(this.pixelMap).width(100).height(100)
    }.width('100%').height('100%')
  }
}

但是我发现我无法在图片右下角添加水印

我的思路是通过 measure.measureTextSize 去获取将要添加水印的文字大小, 但是这个方法返回的值,并不能满足我的需求.

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Excelsior_abit

示例参考:

import { image } from '@kit.ImageKit'

@Entry
@Component
struct Index {
  @State pixelMap: PixelMap | null = null
  aboutToAppear(): void {
    getContext(this).resourceManager.getMediaContent($r("app.media.startIcon")).then((data) => {
      let arrayBuffer = data.buffer.slice(data.byteOffset, data.byteLength + data.byteOffset)
      let imageSource: image.ImageSource = image.createImageSource(arrayBuffer);
      imageSource.getImageInfo((err, value) => {
        if (err) {
          return;
        }
        let opts: image.DecodingOptions = {
          editable: true,
          desiredSize: {
            height: value.size.height,
            width: value.size.width
          }
        };
        imageSource.createPixelMap(opts, (err, pixelMap) => {
          // 这个回调的pixelMap就是当前图片的pixelMap
          let offScreenCanvas = new OffscreenCanvas(value.size.width, value.size.height)
          let OffScreenContext = offScreenCanvas.getContext("2d")
          OffScreenContext.drawImage(pixelMap, 0, 0, offScreenCanvas.width, offScreenCanvas.height)
          OffScreenContext.fillStyle = '#30000000'
          OffScreenContext.font = "50px"
          OffScreenContext.fillText("水印水印水印水印水印", 70, 130)
          // 获取新的PixelMap
          this.pixelMap = OffScreenContext.getPixelMap(0, 0, offScreenCanvas.width, offScreenCanvas.height)
        })
      })
    })
  }

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Image(this.pixelMap).width(100).height(100)
    }
    .width('100%')
    .height('100%')
  }
}
分享
微博
QQ
微信
回复
2天前
相关问题
怎么让text文本排在image里面的右下角
726浏览 • 1回复 待解决
HarmonyOS 如何 app 添加水印
386浏览 • 1回复 待解决
HarmonyOS 如何Text增加描边
56浏览 • 1回复 待解决
图片处理(加动态水印
359浏览 • 1回复 待解决
如何文字添加下划线?
665浏览 • 1回复 待解决
HarmonyOS是否支持图片添加水印
345浏览 • 1回复 待解决
HarmonyOS 拍照后的图片水印
25浏览 • 1回复 待解决
HarmonyOS 水印相机、水印视频
25浏览 • 1回复 待解决