ImageUtil.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. var ImageUtil = window.ImageUtil || {};
  2. ImageUtil.imageUtil =
  3. {
  4. compression:function(targetResult) {
  5. let image = new Image(); //新建一个img标签(还没嵌入DOM节点)
  6. image.src = targetResult;
  7. var imageQuality = localStorage.getItem("imageQuality");
  8. var imageWidth, imageHeight;
  9. if(imageQuality == 1){
  10. // 1080P 1920*1080
  11. imageWidth = 1920;
  12. imageHeight = 1080;
  13. }else if(imageQuality == 2){
  14. // 720P 1280x720
  15. imageWidth = 1280;
  16. imageHeight = 720;
  17. }else{
  18. imageWidth = 1024;
  19. imageHeight = 768;
  20. }
  21. return new Promise(function(resolve, reject) {
  22. image.onload = function () {
  23. let canvas = document.createElement('canvas');
  24. let context = canvas.getContext('2d');
  25. let data = '';
  26. if(image.width > imageWidth || image.height > imageHeight){
  27. canvas.width = imageWidth;
  28. canvas.height = imageHeight;
  29. context.drawImage(image, 0, 0, imageWidth, imageHeight);
  30. }else{
  31. canvas.width = image.width;
  32. canvas.height = image.height;
  33. context.drawImage(image, 0, 0, image.width, image.height);
  34. }
  35. data = canvas.toDataURL('image/jpeg');
  36. resolve(data);
  37. }
  38. image.onerror = function(){
  39. reject();
  40. }
  41. });
  42. },
  43. /**
  44. * DataURL转Png Blob对象
  45. */
  46. sussesDataToPngBlob:function (dataurl) {
  47. const arr = dataurl.split(',');
  48. const bstr = atob(arr[1]);
  49. let n = bstr.length;
  50. const u8arr = new Uint8Array(n);
  51. while (n--) {
  52. u8arr[n] = bstr.charCodeAt(n);
  53. }
  54. return new Blob([u8arr], {
  55. type: 'image/png'
  56. });
  57. },
  58. /**
  59. * stockInResult and stockOutResult 文件上传图片至服务器
  60. * @param file 图片文件
  61. * @param className 类名
  62. */
  63. uploadStockResultImage:function (file,className) {
  64. const _self = this;
  65. // 获取图片的base64
  66. const imgReader = new FileReader();
  67. const promise = new Promise(function (resolve, reject) {
  68. imgReader.onload = function (evt) {
  69. if (!/image\/\w+/.test(file.type)) {
  70. reject(new Error('请确认您选择的文件为图片类型。'));
  71. return;
  72. }
  73. const eImgBase64 = evt.target.result; // 给eImgBase64赋值
  74. if (eImgBase64 != null) {
  75. _self.compression(eImgBase64).then(function (successData) {
  76. if (successData == undefined) {
  77. return;
  78. }
  79. // 把图片上传到服务器
  80. const formData = new FormData();
  81. const uuid = Loading.loading.show();
  82. formData.append('className', className);
  83. const nameImg = new Date().getTime() + '.png';
  84. const blobImg = _self.sussesDataToPngBlob(successData);
  85. formData.append('uploadImage', blobImg, nameImg);
  86. $.ajax({
  87. url: Common.getApiURL('file/imageUpload'),
  88. type: 'post',
  89. beforeSend: function (request) {
  90. Common.addTokenToRequest(request);
  91. },
  92. data: formData,
  93. contentType: false,
  94. processData: false,
  95. success: function (data) {
  96. Loading.loading.hide(uuid);
  97. if (data.indexOf('success') >= 0) {
  98. const imageName = data.substring(data.indexOf(':') + 1);
  99. resolve(imageName);
  100. } else {
  101. reject(new Error('图片上传失败,' + data));
  102. }
  103. },
  104. error: function (XMLHttpRequest, textStatus, errorThrown) {
  105. Loading.loading.hide(uuid);
  106. reject(new Error(XMLHttpRequest.statusText));
  107. }
  108. });
  109. }, function (errorData) {
  110. console.log(errorData);
  111. reject(new Error('图片压缩失败,' + errorData));
  112. });
  113. }
  114. };
  115. });
  116. imgReader.readAsDataURL(file);
  117. return promise;
  118. }
  119. }