faceFusionAPI.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. *
  3. * @authors Eric Hsiao
  4. *
  5. */
  6. const FaceFusionAPI = function () {
  7. // private menbers
  8. const API__DOMAIN = (window.location.host === 'www.pxfreshdelivery.tw') ? 'https://www.pxfreshdelivery.tw/' : 'https://demo.pxfreshdelivery.tw/';
  9. let templateData;
  10. let photoData;
  11. let faceFusionAlpha = 0.29;
  12. let callback = function () {};
  13. let errorCallBack = function () {};
  14. // private methods
  15. function init() {
  16. console.log('faceFusion is loaded.');
  17. }
  18. function start(_templateURL, _photoData, _faceFusionAlpha, _callback, _errorCallBack) {
  19. photoData = _photoData;
  20. faceFusionAlpha = _faceFusionAlpha;
  21. callback = _callback;
  22. errorCallBack = _errorCallBack;
  23. templateData = getBase64Image(_templateURL);
  24. // console.log('faceFusionAPI 啟動');
  25. // faceFusion();
  26. }
  27. function faceFusion() {
  28. const templateData_BASE64 = templateData.replace(/^data:image\/\w+;base64,/, ''); // 去掉base64標頭
  29. const photoData_BASE64 = photoData.replace(/^data:image\/\w+;base64,/, ''); // 去掉base64標頭
  30. console.log('faceFusionAPI 發送中...');
  31. $.ajax({
  32. type: 'POST',
  33. url: `${API__DOMAIN}web/faceMerge`,
  34. data: {
  35. version: '4.0',
  36. alpha: faceFusionAlpha,
  37. image_template: {
  38. image: templateData_BASE64,
  39. image_type: 'BASE64',
  40. },
  41. image_target: {
  42. image: photoData_BASE64,
  43. image_type: 'BASE64',
  44. },
  45. },
  46. dataType: 'json',
  47. success(response) {
  48. console.log(response);
  49. if (response.error_msg == 'SUCCESS') {
  50. const _merge_image = `data:image/png;base64,${response.result.merge_image}`;
  51. // $('.faceFusion').attr('src', _merge_image);
  52. callback(_merge_image);
  53. } else {
  54. console.log('faceFusionAPI 失敗重送');
  55. errorCallBack('融合失敗, 是否要再試一次?');
  56. // if (confirm('融合失敗, 是否要再試一次?') ) {
  57. // faceFusion();
  58. // }
  59. // setTimeout(function () {
  60. // faceFusion();
  61. // }, 1000);
  62. }
  63. },
  64. error(XMLHttpRequest, textStatus, errorThrown) {
  65. console.log(`Status: ${textStatus}, Error: ${errorThrown}`);
  66. },
  67. });
  68. }
  69. // constructor
  70. function getBase64Image(imgURL, width, height) {
  71. // width、height調用時傳入具體像素值,控制大小 ,不傳則默認圖像大小
  72. const img = new Image();
  73. img.onload = function () {
  74. const canvas = document.createElement('canvas');
  75. canvas.width = width || img.width;
  76. canvas.height = height || img.height;
  77. const ctx = canvas.getContext('2d');
  78. ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
  79. const dataURL = canvas.toDataURL('image/jpg', 1.0);
  80. templateData = dataURL;
  81. // console.log('dataURL', dataURL);
  82. console.log('faceFusionAPI 啟動');
  83. faceFusion();
  84. };
  85. img.setAttribute('crossOrigin', 'Anonymous');
  86. img.src = imgURL;
  87. }
  88. {
  89. // $(document).ready(function () {
  90. init();
  91. // });
  92. }
  93. // public
  94. return {
  95. start(_templateURL, _photoData, _faceFusionAlpha, _callback, _errorCallBack) {
  96. start(_templateURL, _photoData, _faceFusionAlpha, _callback, _errorCallBack);
  97. },
  98. };
  99. };