@itmirror/uvc-camera-jssdk
Version:
封装uvc-camera安卓方法和上传功能的jssdk
2 lines (1 loc) • 8.93 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).uvcCamera=e()}(this,function(){"use strict";var n={origin:0,green:0,red:50,blue:100},o=["base64","file"];function i(t){return(i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function a(e){e=e.responseText||e.response;if(!e)return e;try{return JSON.parse(e)}catch(t){return e}}function f(o){if(o){var r=new window.XMLHttpRequest;o.onProgress&&r.upload&&(r.upload.onprogress=function(t){0<t.total&&(t.percent=t.loaded/t.total*100),o.onProgress(t)});var n=function(t){if(!t)return null;var e,n=[];for(e in t)if("object"===i(t[e]))for(var o in t[e])n.push(e+"."+o+"="+t[e][o]);else n.push(e+"="+t[e]);return n.join("&")}(o.data);o.files&&(n=new window.FormData,o.files.forEach(function(t){n.append(t.uid,t.raw,t.name)}),o.data&&Object.keys(o.data).forEach(function(e){var t=o.data[e];Array.isArray(t)?t.forEach(function(t){n.append("".concat(e,"[]"),t)}):n.append(e,o.data[e])})),r.onerror=function(t){o.onError(t)},r.onload=function(){if(r.status<200||300<=r.status)return o.onError((e=r,n="cannot ".concat((t=o).method," ").concat(t.action," ").concat(e.status,"'"),(n=new Error(n)).status=e.status,n.method=t.method,n.url=t.action,n),a(r));var t,e,n;o.onSuccess(a(r),r)},r.open(o.method,o.action,!0),o.withCredentials&&"withCredentials"in r&&(r.withCredentials=!0),r.timeout=o.timeout||6e4,r.ontimeout=function(){o.onError(new Error("request timeout."),a(r))};var t,e=o.headers||{};for(t in null!==e["X-Requested-With"]&&r.setRequestHeader("X-Requested-With","XMLHttpRequest"),e)e.hasOwnProperty(t)&&null!==e[t]&&r.setRequestHeader(t,e[t]);return r.send(n),{abort:function(){r.abort()}}}}for(var r,s=256,u=[];s--;)u[s]=(s+256).toString(16).substring(1);function c(){var t,e=0,n="";if(!r||256<s+16){for(r=Array(e=256);e--;)r[e]=256*Math.random()|0;e=s=0}for(;e<16;e++)t=r[s+e],n+=6==e?u[15&t|64]:8==e?u[63&t|128]:u[t],1&e&&1<e&&e<11&&(n+="-");return s++,n}function l(t){return Object.prototype.toString.call(t).replace(/\[|\]/g,"").split(" ")[1].toLowerCase()}window;var p,d,h,g={green:"hue-rotate(280deg)",red:"hue-rotate(120deg)",blue:"hue-rotate(30deg)"},t=0,m={init:function(t,e){d=t,p="[object Array]"===Object.prototype.toString.call(e)?e:[],h=p.length-1,d.style.filter=g.green},frame:function(){return h<=t?p[t=0]:p[t++]},takePicture:function(e){!function(t,r){if(!t)return;var e=window.devicePixelRatio||1,i=t.offsetWidth*e,a=t.offsetHeight*e,s=t.cloneNode(!0);s.setAttribute("xmlns","http://www.w3.org/1999/xhtml");var u=s.querySelector("img");u.width=i,u.height=a;var c=new Image;c.onload=function(){var t=document.createElement("canvas");t.width=i,t.height=a;var e=t.getContext("2d");e.drawImage(c,0,0,i,a),u.src=t.toDataURL();var n=new Image;n.onload=function(){e.clearRect(0,0,i,a),e.drawImage(n,0,0,i,a),r&&r(t.toDataURL("image/jpeg"))};var o=(new XMLSerializer).serializeToString(s).replace(/#/g,"%23").replace(/\n/g,"%0A");n.src='data:image/svg+xml;charset=utf-8,<svg xmlns="http://www.w3.org/2000/svg" width="'+i+'" height="'+a+'"><foreignObject x="0" y="0" width="100%" height="100%">'+o+"</foreignObject></svg>"},c.src=u.src}(d,function(t){e(t)})},setLight:function(t){t=g[t]||g.origin;d&&(d.style.filter=t)}},y="",v="/api/uploadImg",w="/api/uploadImgSuccess",b=!0,A=20,C=3,E="base64",P=window.IMP_UVC,S=+new Date+"-"+c(),j=0;function M(t){var e=0<arguments.length&&void 0!==t?t:{},n=o;return"string"===l(e.fileType)&&(t=e.fileType.toLowerCase(),e.fileType=n.includes(t)?t:null),"function"===l(e.buttonEvent)&&(D=e.buttonEvent),e.fileType=e.fileType||E,e.uploadMultiple="boolean"===l(e.uploadMultiple)?!!e.uploadMultiple:b,e.retry=Number(e.retry)||C,e.host=e.host||y,e.uploadAction=e.uploadAction||v,e.successAction=e.successAction||w,e.fps=Math.min(Number(e.fps)||A,20)||1,T=Math.min(50,1e3/e.fps),e}var T,e,I=!1;function L(o,t,r,i,n){var a=this,e=this.configs,s=e.host,u=e.uploadAction,c=e.retry,l=o[0].uid,t={action:s+u,files:o,data:t,timeout:(5<arguments.length&&void 0!==arguments[5]?arguments[5]:{}).timeout,method:"post",onProgress:function(t){var e=Object.keys(H);H[l].percent=t.percent/e.length,a.uploadPercent=e.reduce(function(t,e){return t+H[e].percent},0),n(a.uploadPercent)},onSuccess:function(e){var t,n;e.status?(delete H[l],Object.keys(H).length||(U.call(a,function(t){a.clear(),r(t)},i),F=!1)):(t=o.filter(function(t){return e.data.includes(t.name)}),(n=H[l])&&n.retry<c&&(n.option.files=t,n.retry++,f(n.option)))},onError:function(t,e){var n=H[l];if(n){if(n.retry>=c)return delete H[l],i(t,e),console.log(t,e),void(F=!1);n.retry++,f(n.option)}}};H[l]={retry:0,req:f(t),percent:0,option:t}}function O(){if(!N)throw new Error("Call uvcCamera.init first!")}var x,R=0;function U(e,n){var t=this.configs,o=t.host,r=t.successAction,i=t.retry,t={fileUid:S,fileCounts:j};P||(t.params={mode:"debug"});t={action:o+r,headers:{"Content-type":"application/x-www-form-urlencoded"},data:t,method:"post",onSuccess:function(t){R=0,x=null,(t.status?e:n)(t)},onError:function(t,e){if(x){if(i<=R)return R=0,x=null,void n(t,e);R++,f(x)}}};f(x=t)}function q(){}function B(){this.name="uvcCamera",this.files=[],this.uploadPercent=0}var N,_,k,D,H={},X=!1,z=!1,F=!1,V=!1;B.prototype.open=function(){var e=this,n=0<arguments.length&&void 0!==arguments[0]?arguments[0]:q;O(),X||(X=!0,P?(window.IMP_UVC_OPEN_CALLBACK=function(t){"success"===t.status?(z=!0,e.setLight("origin"),e.start(),n({status:!0})):("uvc未初始化"===t.errmsg&&P.init(),n({status:!1,errmsg:t.errmsg})),X=!1},P.open()):(X=!(z=!0),this.start(),n({status:!0})))},B.prototype.close=function(){P&&P.stop(),this.stop(),k&&(k.src=W),z=!1},B.prototype.getStatus=function(){return P?P.getStatus():{isInit:!!N,isConnected:!!z,isPreview:!!V}},B.prototype.start=function(){O(),z?V||(V=!0,function t(){O(),e=setTimeout(function(){k.src=P?"uvcpreview://".concat(+new Date):m.frame(),t()},T)}()):this.open()},B.prototype.stop=function(){e&&clearTimeout(e),V=!1},B.prototype.prev=function(){O(),this.files.pop(),this.start()},B.prototype.next=function(){O(),this.start()},B.prototype.setLight=function(){var t=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"origin";O();var e=n;P?(e=e[t]||e.origin,P.setGain(e)):m.setLight(t)},B.prototype.setBrightness=function(t){t="number"===l(t)?Math.max(100,Math.min(0,t)):80,P&&P.setBrightness(t)},B.prototype.setContrast=function(t){t="number"===l(t)?Math.max(100,Math.min(0,t)):80,P&&P.setContrast(t)},B.prototype.takePicture=function(){var o=this,e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:q;O(),this.stop();function n(t){var e=function(t,e){for(var t=t.split(","),n=window.atob(t[1]),o=n.length,r=new Uint8Array(o);o--;)r[o]=n.charCodeAt(o);return new File([r],e,{type:"image/jpeg"})}(t,i),n={status:"ready",name:i,size:e.size,raw:e};return o.files.push(n),"base64"===r?t:e}var t,r=this.configs.fileType,i=c();P?(t="data:image/jpg;base64,"+P.takePicture(),t=n(t),e(t)):m.takePicture(function(t){t=n(t);e(t)})},B.prototype.upload=function(t){var e=this,n=t.data,o=t.success,r=void 0===o?q:o,o=t.fail,i=void 0===o?q:o,o=t.progress,a=void 0===o?q:o,o=t.options,s=void 0===o?{}:o;P?(t=this.files,o=this.configs,!F&&t.length&&(F=!0,t.forEach(function(t){t.uid="".concat(S,"_").concat(j++),t.name=t.uid+".jpeg"}),o.uploadMultiple?L.call(this,t,n,r,i,a,s):t.forEach(function(t){L.call(e,[t],n,r,i,a,s)}))):U.call(this,function(t){e.clear(),r(t)},i)},B.prototype.abortUpload=function(){Object.keys(H).forEach(function(t){H[t]&&H[t].req.abort(),delete H[t]}),this.uploadPercent=0,F=!1},B.prototype.clear=function(){this.abortUpload(),this.files=[],S=+new Date+"-"+c(),j=0};var W="data:image/gif;base64,R0lGODlhAQABAIABAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAICTAEAOw==";return B.prototype.init=function(t){var e,n=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},o=2<arguments.length&&void 0!==arguments[2]?arguments[2]:q;if(N)return this;e=t,_=document.getElementById(e)||function(){I=!0;var t=document.createElement("div");return t.id=e,document.body.appendChild(t),t}();t=k||document.createElement("img");return k||(k=t,_.appendChild(k)),k.src=W,k.style.width="100%",k.style.height="100%",k.style.objectFit="cover",this.configs=M(n),P?(window.IMP_UVC_FUNCTION_BUTTON_CLICK=function(){D&&D()},P.init(),setTimeout(function(){"function"===l(o)&&o()},100)):(m.init(_,n.devData),"function"===l(o)&&o()),N=!0,console.log("uvcCamera has created."),this},B.prototype.update=function(){return this.configs=M(0<arguments.length&&void 0!==arguments[0]?arguments[0]:{}),console.log("uvcCamera has updated."),this},B.prototype.destroy=function(){this.stop(),this.clear(),P&&P.close(),N=X=z=!1,_.removeChild(k),I&&_.parentNode.removeChild(_),_=k=D=null},new B});