canvas2d_video_processor.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. var canvasSource;
  2. var camera1Ctx;
  3. var camera2Ctx;
  4. var isCopying = false;
  5. var timer;
  6. const mergedViewSize = [1920, 1080];
  7. const camera1Size = [640, 480];
  8. const camera1CilpStartPoint = [1280, 600];
  9. const camera2Size = [640, 480];
  10. const camera2PartSize = [640, 160];
  11. const camera2CilpStartPoint = [0, 200];
  12. var camera1Width;
  13. var camera1Height;
  14. var camera1CilpX;
  15. var camera1CilpY;
  16. var camera2Width;
  17. var camera2Height;
  18. var camera2PartWidth;
  19. var camera2PartHeight;
  20. var camera2CilpX;
  21. var camera2CilpY;
  22. /// 拷贝视频流
  23. function copyVideoStream() {
  24. let gl = canvasSource.getContext("webgl", { preserveDrawingBuffer: true });
  25. let camera1Pixels = new Uint8Array(camera1Width * camera1Height * 4);
  26. let camera2PartPixels = new Uint8Array(
  27. camera2PartWidth * camera2PartHeight * 4 * 3
  28. );
  29. gl.readPixels(
  30. camera1CilpX,
  31. camera1CilpY,
  32. camera1Width,
  33. camera1Height,
  34. gl.RGBA,
  35. gl.UNSIGNED_BYTE,
  36. camera1Pixels
  37. );
  38. camera1Ctx.canvas.width = camera1Width;
  39. camera1Ctx.canvas.height = camera1Height;
  40. let camera1ImageData = camera1Ctx.createImageData(
  41. camera1Width,
  42. camera1Height
  43. );
  44. camera2Ctx.canvas.width = camera2Width;
  45. camera2Ctx.canvas.height = camera2Height;
  46. let camera2PartImageData = camera2Ctx.createImageData(
  47. camera2PartWidth * 3,
  48. camera2PartHeight
  49. );
  50. // 绘制摄像头1画面
  51. camera1ImageData.data.set(camera1Pixels);
  52. camera1Ctx.putImageData(camera1ImageData, 0, 0);
  53. // 绘制摄像头2画面
  54. // 分三次填充数据
  55. gl.readPixels(
  56. camera2CilpX,
  57. camera2CilpY,
  58. camera2PartWidth * 3,
  59. camera2PartHeight,
  60. gl.RGBA,
  61. gl.UNSIGNED_BYTE,
  62. camera2PartPixels
  63. );
  64. camera2PartImageData.data.set(camera2PartPixels);
  65. camera2Ctx.putImageData(camera2PartImageData, 0, camera2PartHeight * 2);
  66. camera2Ctx.putImageData(
  67. camera2PartImageData,
  68. -camera2PartWidth,
  69. camera2PartHeight
  70. );
  71. camera2Ctx.putImageData(camera2PartImageData, -camera2PartWidth * 2, 0);
  72. /// 清理变量
  73. camera1Pixels = null;
  74. camera2PartPixels = null;
  75. camera1ImageData = null;
  76. camera2PartImageData = null;
  77. }
  78. // Start copying the video stream
  79. function startCopying() {
  80. if (!isCopying) {
  81. isCopying = true;
  82. console.log("开始拷贝视频流");
  83. const video1 = document.getElementById("video1");
  84. canvasSource = video1;
  85. const camera1 = document.getElementById("camera1");
  86. const camera2 = document.getElementById("camera2");
  87. camera1Ctx = camera1.getContext("2d");
  88. camera2Ctx = camera2.getContext("2d");
  89. initSize();
  90. }
  91. }
  92. // Stop copying the video stream
  93. function stopCopying() {
  94. isCopying = false;
  95. timer = null;
  96. }
  97. function initSize() {
  98. const gl = canvasSource.getContext("webgl");
  99. const scale = gl.drawingBufferWidth / mergedViewSize[0];
  100. console.log("Scale", scale);
  101. camera1Width = parseInt(camera1Size[0] * scale);
  102. camera1Height = parseInt(camera1Size[1] * scale);
  103. camera1CilpX = parseInt(camera1CilpStartPoint[0] * scale);
  104. camera1CilpY = parseInt(camera1CilpStartPoint[1] * scale);
  105. camera2Width = parseInt(camera2Size[0] * scale);
  106. camera2Height = parseInt(camera2Size[1] * scale);
  107. camera2PartWidth = parseInt(camera2PartSize[0] * scale);
  108. camera2PartHeight = parseInt(camera2PartSize[1] * scale);
  109. camera2CilpX = parseInt(camera2CilpStartPoint[0] * scale);
  110. camera2CilpY = parseInt(camera2CilpStartPoint[1] * scale);
  111. }