ionic2用canvas给照片加水印

ionic2的问题可以留言,我尽量解答

点击按钮调用相机拍照之后加当前时间的水印

attendanceClick() {
    console.log('aaaa');
    
    const options: CameraOptions = {
      quality: 100,
      destinationType: this.camera.DestinationType.DATA_URL,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE
    }
    this.camera.getPicture(options).then((imageData) => {
      // imageData is either a base64 encoded string or a file URI
      // If it's base64:
      let base64Image = 'data:image/jpeg;base64,' + imageData;
      //加水印
      var canvas = document.createElement('canvas');
      var cxt = canvas.getContext('2d');
      cxt.fillStyle = 'green';
      cxt.fillRect(10, 10, 100, 100);
      var img = new Image();
      img.src = base64Image;
      img.onload = () => {
        var date: string = new Date().toLocaleDateString();
        var time: string = new Date().toTimeString().substring(0,5);
        var datetime: string = date + " " + time;
        canvas.height = img.height;
        canvas.width = img.width;
        cxt.drawImage(img,0,0,img.width,img.height,0,0,img.width,img.height);
        cxt.save();
        cxt.font = 200 + "px Arial";
        cxt.textBaseline = 'middle';//更改字号后,必须重置对齐方式,否则居中麻烦。设置文本的垂直对齐方式
        cxt.textAlign = 'center';
        var tw = cxt.measureText(datetime).width;
        var ftop = canvas.height/2;
        var fleft = canvas.width/2;
        cxt.fillStyle="#ffffff";
        cxt.fillText(datetime,fleft,ftop);//文本元素在画布居中方式
        cxt.strokeStyle = 'yellow';
        cxt.strokeText(datetime,fleft,ftop);//文字边框

        this.dataSer.attendanceUp('xunjianA', canvas.toDataURL("image/png")).subscribe(
          data => {
            console.log('成功了');
            console.log(data);
          },
          err => {
            console.log('失败了');
            console.log(err);
          },
          () => console.log('Query Complete')
        );
      }

      
      //console.log(base64Image);
    }, (err) => {
      // Handle error
    });
  }
  

 

转载于:https://www.cnblogs.com/huihuihui/p/6930434.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值