微信小程序原生 canvas画布截取视频帧保存为图片并进行裁剪

html页面:

视频尺寸过大会画布会撑开屏幕,要下滑

尺寸和视频链接是从上个页面点击传过来的,可自行定义

html 复制代码
<canvas id="cvs1" type="2d" style="width: {{videoWidth}}px;height: {{videoHeight}}px;"></canvas>
<video id="video" src="{{tempFilePath}}" bindtimeupdate="timeUpdate"></video>
<t-button class="block" bind:tap="getCanvas" theme="primary">立即截图</t-button>
<image src="{{cropurl}}" style="width: 100%;" mode="widthFix" />

js:

TypeScript 复制代码
 timeUpdate: function (e) {
    //实时播放进度 秒数
    var currentTime = parseInt(e.detail.currentTime)
    this.setData({
      'currentTime': currentTime,
    });
    // console.log("视频播放到第" + currentTime + "秒") //查看正在播放时间,以秒为单位
  },

 //绘制截图
  getCanvas() {
    const dpr = wx.getSystemInfoSync().pixelRatio
    wx.createSelectorQuery().select('#video').context(res => {
      console.log('select video', res)
      const video = this.video = res.context
      video.pause() // 暂停视频,防止继续播放
      video.seek(this.data.currentTime) // 将视频定位到当前时间
      // console.log("视频宽度高度", this.data.videoWidth, this.data.videoHeight)
      wx.createSelectorQuery().selectAll('#cvs1').fields({
          node: true,
          size: true
        })
        .exec((res) => {
          console.log('select canvas', res)
          const canvas = res[0][0].node
          const ctx1 = res[0][0].node.getContext('2d')
          res[0][0].node.width = this.data.videoWidth
          res[0][0].node.height = this.data.videoHeight
          //图片加载完成后绘制到画布上
          ctx1.drawImage(video, 0, 0, this.data.videoWidth, this.data.videoHeight);
          console.log("ctx1=>", ctx1)
          wx.canvasToTempFilePath({
            width: this.data.videoWidth,
            height: this.data.videoHeight,
            canvas: canvas,
            success: (res) => {
              console.log("图片路径", res.tempFilePath)
//一定要有这一步不然拿到的是空白图片
              this.setData({
                canvasurl: res.tempFilePath
              })
              //裁剪图片
              wx.cropImage({
                src: this.data.canvasurl, // 图片路径
                cropScale: '4:3', // 裁剪比例
                success: (res) => {
                  console.log('裁剪后图片', res)
                  this.setData({
                    cropurl: res.tempFilePath
                  })
                }
              })
            },
            fail: (err) => {
              console.log(err)
            }
          })
        })
      //   //base64
      //   // setTimeout(() => {
      //   //   this.setData({
      //   //     // 导出canvas的url(base64格式)
      //   //     canvasurl: canvas.toDataURL('image/png'),
      //   //     show: true
      //   //   })
    }).exec()

  },
相关推荐
Rabbit_QL2 小时前
【音频处理】从 AirPods 主动降噪到音频 Source Separation:同一个问题的两种工程解法
前端·人工智能·音视频
hello kitty w2 小时前
3.小程序方法的封装
javascript·小程序
蓝色心灵-海3 小时前
数字化家谱管理实践:基于微信小程序的家族信息整理方案(宗源智谱)族谱,家谱管理
小程序
WX:ywyy67983 小时前
从0到1:付费短剧小程序全栈定制开发实战与避坑指南
微信·小程序·短剧小程序开发·短剧系统开发·短剧app开发·短剧平台开发·短剧平台搭建
EasyGBS4 小时前
国标GB28181视频平台EasyGBS视频质量诊断插件(EasyVQD)详解
音视频·花屏·视频质量诊断·画面冻结·画面抖动·偏色检测·噪声检测
笨笨狗吞噬者4 小时前
【uniapp】小程序端解决分包的uni_modules打包后产物进入主包中的问题
前端·微信小程序·uni-app
AI-小柒4 小时前
Seedance 2.0(即梦 2.0)深度解析:AI 视频进入「导演级」可控时代
大数据·人工智能·网络协议·tcp/ip·http·音视频
YYDataV数据可视化5 小时前
【P2P音视频通信系统】之TURN 服务详解
音视频·webrtc·实时音视频·ai编程
xhyu615 小时前
【学习笔记】推荐系统 (5.排序:多目标模型、MMoE、融合预估分数、视频播放建模)
笔记·学习·音视频