HarmonyOS 可以指定位置截图吗?指定坐标等等

有个图片剪裁的需求,图片上面盖了一个边框图,现在需要针对这个边框图截图,所以需要指定位置或者范围截图;查了资料目前的屏幕截屏和组件截图都不满足需求,麻烦帮忙解决下。

HarmonyOS
2024-12-26 14:51:57
782浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

可以将这个整张图片绘制到canvas中,之后在canvas上进行对应的裁剪,之后将canvas转化成一张图片,保存起来,简单测试代码,可以参考这个api:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-canvasrenderingcontext2d-V5#putimagedata

@Entry
@Component
struct Index {
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
  img:ImageBitmap = new ImageBitmap("https://emoji.cdn.bcebos.com/yige-aigc/index_aigc/final/toolspics/3.png")
  toDataURL: string = ''
  build() {
    Column(){
      Image("xxxxx").width(200).height(200)
      Canvas(this.context).onReady(()=>{
        const offContext = this.offCanvas.getContext("2d", this.settings)
        offContext.drawImage(this.img, 0,0, 200, 200)
        const imageData = offContext.getImageData(0,0,100,200)
        this.context.putImageData(imageData, 0,0)
        this.context.fillStyle = "#00ff00"
        this.context.fillRect(0,0,100,100)
        this.toDataURL = this.context.toDataURL("image/png", 0.92)
      }).width(400).height(400)
    }.width("100%").height("100%")
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
分享
微博
QQ
微信
回复
2024-12-26 17:25:35
相关问题
HarmonyOS List列表滚动到指定位置
1184浏览 • 1回复 待解决
HarmonyOS list如何动态滚动到指定位置
863浏览 • 1回复 待解决
鸿蒙Toast能指定弹出的位置
7001浏览 • 1回复 待解决
HarmonyOS ListItem位置指定问题
427浏览 • 1回复 待解决
HarmonyOS指定打开系统应用?
666浏览 • 1回复 待解决