如何使用html5 canvas画布对本地视频进行截图并上传至服务器

前端代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>截图系统</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no">
    <meta http-equiv=”X-UA-Compatible” content=”IE=edge,chrome=1″/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
</head>
<body style="width: 100%;height: 100%">
<div align="center">
<video style="width: 1920px;height: 1080px" id="video" controls>Your browser can't support HTML5 Audio</video>
<p><input type="file" id="file" onchange="onInputFileChange()"><input class="cut" type="button" value="截图"  id="buttons_cut" onclick="cut()"></p>
<canvas id="canvas"></canvas>
</div>
<script>
    // 打开本地视频
    function onInputFileChange() {
        var file = document.getElementById('file').files[0];
        var url = URL.createObjectURL(file);
        console.log(url);
        document.getElementById("video").src = url;
    }



    // 使用canvas对视频进行截图
    function cut(){
       var canvas = document.getElementById('canvas');
       var video = document.getElementById('video');
       console.log('视频尺寸:'+video.style.width+'*'+video.style.height);
        canvas.width = video.videoWidth;
        canvas.height = video.videoHeight;
       console.log(canvas.width);
       console.log(canvas.height);
       var ctx = canvas.getContext('2d');
       ctx.drawImage(video, 0, 0,1920,1080);
       var base64 = canvas.toDataURL('images/png');
       upload(base64);

   }

   // 上传图片

    function upload(image) {
        var imgFile=dataURLtoFile(image,"img.png");
        var xhr=new XMLHttpRequest();
        var fd=new FormData();
        xhr.open('POST','/springStudy/user/multifiles');
        fd.append("multipartFile",imgFile);
        xhr.send(fd);
        console.log('上传json!')
    }

    //将图片Base64 转成文件

    function dataURLtoFile(dataurl, filename) {
        console.log("转文件")
        var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
            bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
        while(n--){
            u8arr[n] = bstr.charCodeAt(n);
        }
        return new File([u8arr], filename, {type:mime});
    }




</script>

</body>
</html>

spring mvc后端代码:

@RequestMapping(value = "/multifiles")
    public void receivefiles(HttpServletRequest request, HttpServletResponse response, @ModelAttribute UploadFile uploadFile){

        System.out.println("收到请求");
        MultipartFile multipartFile=uploadFile.getMultipartFile();
        String filename=multipartFile.getOriginalFilename();
        try{
            System.out.println(request.getServletContext().getRealPath("/image"));
            // path="G:tomcatupload"
            File file=new File(path,String.valueOf(System.currentTimeMillis())+filename);
            if(!file.exists()){

            }
            multipartFile.transferTo(file);
        }catch (IOException e){
            e.printStackTrace();
        }

    }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值