index.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. window.onload = function () {
  2. const canvas = document.getElementById('myCanvas');
  3. const saveImageBtn = document.getElementById('startRecording');
  4. const zip = new JSZip();
  5. let num = 0
  6. saveImageBtn.addEventListener('click', () => {
  7. // Convert canvas content to data URL
  8. num = 0
  9. saveImageBtn.disabled = true
  10. saveImgZip(num)
  11. });
  12. function saveImgZip() {
  13. const tempCanvas = document.createElement('canvas');
  14. const tempContext = tempCanvas.getContext('2d');
  15. const time = setInterval(() => {
  16. if (num < 10) {
  17. num++
  18. const addToZip = (canvas, filename) => {
  19. const dataURL = canvas.toDataURL('image/JPEG').split(',')[1];
  20. zip.file(filename, dataURL, { base64: true });
  21. };
  22. const scaledWidth = 720; // Change this to your desired width
  23. const scaledHeight = 1280; // Change this to your desired height
  24. tempCanvas.width = scaledWidth
  25. tempCanvas.height = scaledHeight
  26. tempContext.drawImage(canvas, 0, 0, canvas.width, canvas.height, 0, 0, scaledWidth, scaledHeight);
  27. addToZip(tempCanvas, `${num}.jpg`);
  28. } else {
  29. clearInterval(time)
  30. uploadZip()
  31. }
  32. }, 1000);
  33. }
  34. function uploadZip() {
  35. saveImageBtn.disabled = false
  36. console.log("1111",new Date().getTime())
  37. zip.generateAsync({ type: 'blob' })
  38. .then((blob) => {
  39. let obj = blob;
  40. obj.name = new Date().getTime()+".zip"
  41. postData("http://127.0.0.1:5000/uploadImage",obj,function (response) {
  42. console.log(response,"请求成功");
  43. })
  44. });
  45. }
  46. function postData(url, data, callback) {
  47. // var xhr = new XMLHttpRequest();
  48. // xhr.open("POST", url, true);
  49. // xhr.setRequestHeader("Content-Type", "application/json");
  50. // xhr.onreadystatechange = function () {
  51. // if (xhr.readyState === 4 && xhr.status === 200) {
  52. // callback(JSON.parse(xhr.responseText));
  53. // }
  54. // };
  55. var formData = new FormData();
  56. // // 将文件添加到 FormData 对象中
  57. // // let datas = new File(data,new Date().getTime() + ".zip")
  58. let fileBlob = new File(
  59. [data],
  60. new Date().getTime() + ".zip"
  61. );
  62. formData.append('file', fileBlob);
  63. // console.log(formData)
  64. // xhr.send(data);
  65. // 发送 POST 请求
  66. fetch(url, {
  67. method: 'POST',
  68. body: formData
  69. })
  70. .then(response => response.json())
  71. .then(data => {
  72. callback(data);
  73. })
  74. .catch(error => {
  75. callback(error);
  76. });
  77. }
  78. // 例子用法
  79. // var apiUrl = "https://example.com/api";
  80. // var requestData = {
  81. // key1: "value1",
  82. // key2: "value2",
  83. // };
  84. // postData(apiUrl, requestData, function (response) {
  85. // console.log(response);
  86. // });
  87. }