當(dāng)前位置:首頁(yè) > IT技術(shù) > 移動(dòng)平臺(tái) > 正文

UI-APP給圖片加水印
2022-02-14 10:45:56

1.加文字水印

上傳圖片


<view class="cu-form-group">
                            <view class="grid col-4 grid-square flex-sub">
                                <canvas style="border: 1px solid green;position: absolute;left: -5000px;"
                                    :style="{'width':w,'height': h}" canvas-id="firstCanvas" ref="mycanvas"></canvas>
                                <view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage"
                                    :data-url="imgList[index]">
                                    <image :src="imgList[index]" mode="aspectFill"></image>
                                    <view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
                                        <text class='cuIcon-close'></text>
                                    </view>
                                </view>
                                <view class="solids" @tap="ChooseImage" v-if="imgList.length<1">
                                    <text class='cuIcon-cameraadd'></text>
                                </view>
                            </view>
                        </view>


data
data() {
 return {
  w: 0,
  h: 0,
  imgList: [],
},
方法
methods: {
ViewImage(e) {
 uni.previewImage({
  urls: this.imgList,
  current: e.currentTarget.dataset.url
 });
},
DelImg(e) {
 this.imgList.splice(e.currentTarget.dataset.index, 1)
},
ChooseImage() {
 var that = this
 uni.chooseImage({
  count: 1, //默認(rèn)9
  sizeType: ['original', 'compressed'], //可以指定是原圖還是壓縮圖,默認(rèn)二者都有
  sourceType: ['camera'], //拍照
  success: (res) => {
   console.log(res);
   //圖片數(shù)組
   let tempFilePath = res.tempFilePaths;
   let num = 0;
   let inter = setInterval(() => {
    that.setimg(tempFilePath[num]);
    num++;
    if (num == tempFilePath.length) {
     num = 0;
     clearInterval(inter);
    }
   }, 500)
  }
 });
},
},
setimg(e) {
 let date = "-我是水印-";
 let that = this;
 let ctx = uni.createCanvasContext('firstCanvas', this.$scope);
 uni.getImageInfo({
  src: e,
  success: (res) => {
   console.log(res);
   that.w = res.width / 2 + 'px';
   that.h = res.height / 2 + 'px';
   //初始化畫(huà)布
   setTimeout(() => { //使用定時(shí)是因?yàn)橹谱魉⌒枰獣r(shí)間,設(shè)置定時(shí)才不會(huì)出bug
    ctx.fillRect(0, 0, that.w, that.h);
    // //將圖片src放到cancas內(nèi),寬高為圖片大小
    ctx.drawImage(res.path, 0, 0, res.width / 2, res.height / 2);
    ctx.rotate(45 * Math.PI / 180);
    //對(duì)斜對(duì)角線以左部分進(jìn)行文字的填充
    for (let j = 1; j < 10; j++) { //用for循環(huán)達(dá)到重復(fù)輸出文字的效果,這個(gè)for循環(huán)代表縱向循環(huán)
     ctx.beginPath();
     ctx.setFontSize(20);
     ctx.setFillStyle("rgba(169,169,169,.6)");
     ctx.fillText(date, 0, 50 * j);
     for (let i = 1; i < 10; i++) { //這個(gè)for循環(huán)代表橫向循環(huán),
      ctx.beginPath();
      ctx.setFontSize(20);
      ctx.setFillStyle("rgba(169,169,169,.6)");
      ctx.fillText(date, 80 * i, 50 * j);
     }
    } //兩個(gè)for循環(huán)的配合,使得文字充滿斜對(duì)角線的左下部分
    // 對(duì)斜對(duì)角線以右部分進(jìn)行文字的填充邏輯同上
    for (let j = 0; j < 10; j++) {
     ctx.beginPath();
     ctx.setFontSize(20);
     ctx.setFillStyle("rgba(169,169,169,.6)");
     ctx.fillText(date, 0, -50 * j);
     for (let i = 1; i < 10; i++) {
      ctx.beginPath();
      ctx.setFontSize(20);
      ctx.setFillStyle("rgba(169,169,169,.6)");
      ctx.fillText(date, 80 * i, -50 * j);
     }
    }
    ctx.rotate(-45 * Math.PI / 180);
    ctx.draw(false, () => {
     uni.canvasToTempFilePath({ //將畫(huà)布中內(nèi)容轉(zhuǎn)成圖片,即水印與圖片合成
      canvasId: 'firstCanvas',
      success: (res) => {
       console.log(res);
       that.imgList.push(res.tempFilePath);
    //圖片上傳
       uni.uploadFile({
        url: baseObj.baseUrl +'/sysUploadFile/uploadFile',
        filePath: res.tempFilePath,
        name: 'multipartFile',
        success: (res) => {
         var url = JSON.parse(
           res.data)
          .result
         console.log(url)
         this.imgList[0] = url
        },
        fail: (e) => {
         console.log(e)
        }
       })
      }
     })
    })
   }, 500)
  }
 })
},

本文摘自 :https://www.cnblogs.com/

開(kāi)通會(huì)員,享受整站包年服務(wù)立即開(kāi)通 >