HarmonyOS 请提供一个给拍摄/从相册选择图片添加水印,控制照片大小、质量的demo

Consulting description:现有业务拍摄的图片太大了,一张好几M,请提供一个能控制图片大小、质量、压缩率,并能给图片打水印的demo。

HarmonyOS
2024-08-13 16:04:31
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

可以参考此文档:(https://developer.huawei.com/consumer/cn/doc/harmonyos-samples-V5/samples-application-framework-0000001903082910-V5可查询到相机以及图片压缩的demo。

水印暂无官方示例代码:

1、使用浮层overlay:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-universal-attributes-overlay-V5#ZH-CN_TOPIC_0000001884917642__overlay

2、使用canvas绘制:

import measure from '@ohos.measure'; 
@Entry 
@Component 
struct Index { 
  @State message: string = 'Hello World' 
  @State show: boolean = false 
  @State watermarkColor: string = '#ff0000'; 
  @State waterTextSize: number = 14; 
  @State watermarkText: string = '这是一条水印' 
  build() { 
    Stack() { 
      Column() { 
        Text(this.message) 
          .fontSize(20) 
          .fontWeight(FontWeight.Bold) 
          .margin({ bottom: 10 }) 
          .textAlign(TextAlign.Center) 
          .width('100%') 
        Row() { 
          Button("颜色1") 
            .fontSize(11) 
            .onClick(() => { 
              this.watermarkColor = '#00ff00' 
            }) 
          Button("颜色2") 
            .fontSize(11) 
            .onClick(() => { 
              this.watermarkColor = '#0000ff' 
            }) 
        } 
      } 
      MiniWatermark({ 
        waterText: this.watermarkText, 
        waterTextSize: this.waterTextSize, 
        waterTextColor: this.watermarkColor 
      }) 
    } 
 
  } 
} 
@Component 
export struct MiniWatermark { 
  private settings: RenderingContextSettings = new RenderingContextSettings(true); 
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings); 
  private settings2: RenderingContextSettings = new RenderingContextSettings(true); 
  private context2: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings2); 
  @Prop @Watch('clearAndDraw') waterText: string; 
  @Prop @Watch('clearAndDraw') waterTextSize: number; 
  @Prop @Watch('clearAndDraw') waterTextColor: string; 
  @State image: string = ''; 
  @State realWidth: number = 0; 
  @State realHeight: number = 0; 
  aboutToAppear() { 
    this.initWH(); 
  } 
  clearAndDraw() { 
    this.initWH(); 
    this.drawText(); 
    this.drawWaterMark(); 
  } 
  private initWH() { 
    const textWidth = measure.measureText({ 
      textContent: this.waterText, 
      fontSize: `${this.waterTextSize}fp` 
    }); 
    this.realWidth = textWidth + vp2px(20); 
    const textHeight = textWidth * 0.6; 
    this.realHeight = textHeight + vp2px(20); 
  } 
  build() { 
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 
      Stack() { 
        Canvas(this.context2) 
          .width(this.realWidth + 'px') 
          .height(this.realHeight + 'px') 
          .backgroundColor(Color.Green) 
          .onReady(() => { 
            this.context2.rotate(30 * Math.PI / 180); 
            this.drawText(); 
          }) 
          .visibility(Visibility.Visible) 
        Canvas(this.context) 
          .width('100%') 
          .height('100%') 
          .backgroundColor(Color.Transparent) 
          .onReady(() => { 
            this.drawWaterMark(); 
          }) 
          .visibility(Visibility.Visible) 
      } 
      .width('100%') 
      .height('100%') 
      .hitTestBehavior(HitTestMode.Transparent) 
    }.hitTestBehavior(HitTestMode.Transparent) 
  } 
  private drawWaterMark() { 
    this.context.clearRect(0, 0, 6000, 6000); 
    // 绘制填充类文本 
    const img: ImageBitmap = new ImageBitmap(this.image); 
    const pattern = this.context.createPattern(img, 'repeat'); 
    if (pattern) { 
      this.context.fillStyle = pattern; 
    } 
    this.context.fillRect(0, 0, 6000, 6000); 
    this.context.restore(); 
  } 
  private drawText() { 
    this.context2.rotate(-30 * Math.PI / 180); 
    this.context2.clearRect(0, 0, 6000, 6000); 
    this.context2.rotate(30 * Math.PI / 180); 
    this.context2.font = `${fp2px(this.waterTextSize)}px`; 
    this.context2.fillStyle = this.waterTextColor; 
    this.context2.fillText(this.waterText, 10, 10); 
    this.context2.restore(); 
    this.image = this.context2.toDataURL(); 
  } 
  private drawColor() { 
    this.context2.fillStyle = this.waterTextColor; 
    this.context2.clearRect(0, 0, 6000, 6000); 
    this.context2.rotate(30 * Math.PI / 180); 
    this.context2.font = `${fp2px(this.waterTextSize)}px`; 
    this.context2.fillStyle = this.waterTextColor; 
    this.context2.fillText(this.waterText, 10, 10); 
    this.context2.restore(); 
    this.image = this.context2.toDataURL(); 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
分享
微博
QQ
微信
回复
2024-08-13 21:23:42


相关问题
HarmonyOS 如何 app 添加水印
1238浏览 • 1回复 待解决
HarmonyOS是否支持图片添加水印
1022浏览 • 1回复 待解决
HarmonyOS拍照后图片添加水印
702浏览 • 1回复 待解决
HarmonyOS windows级别添加水印
660浏览 • 1回复 待解决
如何使用canvas添加水印
2335浏览 • 1回复 待解决
HarmonyOS 请提供路由跳转Demo
1240浏览 • 1回复 待解决
HarmonyOS 拍照后图片加水印
705浏览 • 1回复 待解决
HarmonyOS PDF添加水印后展示白屏
716浏览 • 1回复 待解决
HarmonyOS 如何在app内全页面添加水印
814浏览 • 1回复 待解决
HarmonyOS 有没有对UI添加水印方法
706浏览 • 1回复 待解决
HarmonyOS 关于相册选择图片上传
775浏览 • 1回复 待解决