简体   繁体   English

旋转HTML5视频并保存画布

[英]Rotating HTML5 Video and saving Canvas

I have a HTML5 Video using jpeg_camera ( https://github.com/amw/jpeg_camera ) 我有一个使用jpeg_camera( https://github.com/amw/jpeg_camera )的HTML5视频

The Output of hte video on the html looks like this: HTML上的HTE视频的输出如下所示:

<video autoplay="" src="blob:http://localhost:49209/41c42143-5d0e-4525-8fe8-cc1e9f50a8e6" style="transform: scaleX(-1); position: relative; width: 601px; height: 338px; left: -31px; top: 0px;"></video>

When a tablet rotates, I capture the orientationchange and resize event and rotate the video as required. 平板电脑旋转时,我捕获了directionchange和resize事件并根据需要旋转视频。

The problem is when the snapshot is taken, the video is back to the original orientation, I'm trying to rotate the canvas in javascript before it is saved. 问题是当拍摄快照时,视频又回到了原始方向,我试图在保存之前使用javascript旋转画布。

this is what I have (but it is unworking). 这就是我所拥有的(但是它不起作用)。 The code is inside the jpgvideo library. 该代码位于jpgvideo库内部。

  JpegCameraHtml5.prototype._engine_capture = function(snapshot, mirror, quality, scale, rotate) {
    var canvas, context, crop;
    crop = this._get_capture_crop();
    canvas = document.createElement("canvas");
    canvas.width = Math.round(crop.width * scale);
    canvas.height = Math.round(crop.height * scale);
    context = canvas.getContext("2d");
    context.drawImage(this.video, crop.x_offset, crop.y_offset, crop.width, crop.height, 0, 0, Math.round(crop.width * scale), Math.round(crop.height * scale));
    if (rotate != undefined) {
        context.rotate(rotate*Math.PI/180);  //This was my attempt but it doesn't work
    }
    snapshot._canvas = canvas;
    snapshot._mirror = mirror;
    return snapshot._quality = quality;
  };

Can anyone recommend a way to save the canvas rotated? 谁能推荐一种保存旋转画布的方法?

The rotation transformation has to happen before the image/video is drawn, and it will happen at origin meaning you will want to translate origin first to the center of the canvas: 旋转转换必须在绘制图像/视频之前进行,并且转换将在原点进行,这意味着您将需要先将原点平移到画布的中心:

  • Translate origin from upper-left of canvas to center which is where the rotation will happen 将原点从画布的左上角平移到中心,这将发生旋转
  • Rotate 旋转
  • Translate back as upper-left corner of image is also drawn from origin 向后平移,因为图像的左上角也从原点绘制

Modified code: 修改后的代码:

context = canvas.getContext("2d");
context.setTransform(1,0,0,1,0,0);                            // reset transforms if any
if (rotate) {
    context.translate(canvas.width / 2, canvas.height / 2);   // to center
    context.rotate(rotate * Math.PI / 180);                   // rotate
    context.translate(-canvas.width / 2, -canvas.height / 2); // and back
}
context.drawImage(this.video, 
  crop.x_offset, crop.y_offset, crop.width, crop.height, 
  0, 0, Math.round(crop.width * scale), Math.round(crop.height * scale));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM