diff --git a/src/uitls/preview.ts b/src/uitls/preview.ts index b0706c46b7cf953294f1d3e4515a822ab7eb99e1..4bb651e2b1abdbe9972d55fb294a7be7529a63a4 100644 --- a/src/uitls/preview.ts +++ b/src/uitls/preview.ts @@ -35,4 +35,51 @@ function intercpt(calssname: HTMLElement) { return canvas.toDataURL('image/png', 1.0) } +/** + * 截取本地视频某一帧 + * @ramerate +*/ +function ramerate(url: string, width: string, height: string) { + return new Promise(function (resolve, reject) { + const video = document.createElement('video') + video.currentTime = 8 // 指定帧数 + video.setAttribute('mut', 'true') + video.setAttribute('crossOrigin', 'anonymous') // 处理跨域 + video.setAttribute('crossOrigin', 'anonymous') // 处理跨域 + video.setAttribute('src', url) + video.setAttribute('x5-video-player-type', 'h5-page') + video.setAttribute('preload', 'metadata') + video.setAttribute('width', width) + video.setAttribute('height', height) + video.addEventListener('loadeddata', () => { + const canvas = document.createElement('canvas') as any + const width = video.width // canvas的尺寸和图片一样 + const height = video.height + canvas.width = width + canvas.height = height + canvas.getContext('2d').drawImage(video, 0, 0, width, height) // 绘制canvas + var dataURL = canvas.toDataURL('image/jpeg') // 转换为base64 + console.log(dataURL); + resolve(dataURL); + }) + }) +} +/** + * 截取本地图片第一帧转化为文件类型 + * @convert +*/ +function convert(url: any, name: string) { + // console.log(url); + var arr = url.split(","), + mime = arr[0].match(/:(.*?);/)[1], + bstr = atob(arr[1]), + n = bstr.length, + u8arr = new Uint8Array(n); + while (n--) { + u8arr[n] = bstr.charCodeAt(n); + } + console.log(new File([u8arr], name || 'png', { type: mime })); + return new File([u8arr], name || 'png', { type: mime }) +} + export {preview ,intercpt} \ No newline at end of file