javascriptでhttp://hogehoge.Jpgのuint8arrayをpngに変換する必要に迫られまして、下記コードを書きました。
javascript
1 2 function loadImg(imgUrl) { 3 var xhr = new XMLHttpRequest(); 4 xhr.open('GET', imgUrl, true); 5 xhr.responseType = "arraybuffer"; 6 xhr.onload = function() { 7 var bytes = new Uint8Array(this.response); 8 console.log("uint8jpg:"+bytes); 9 var blob = new Blob([ bytes ], { type: "image/jpeg" }); 10 console.log('blob:'+blob); 11 window.createImageBitmap(blob) 12 .then(function(image) { 13 var width = image.width; 14 var height = image.height; 15 console.log('ok'+image); 16 var canvas = document.createElement('canvas'); 17 canvas.width = width; 18 canvas.height = height; 19 20 // Draw Image 21 var ctx = canvas.getContext('2d'); 22 ctx.drawImage(image, 0, 0); 23 var pngdata=canvas.toDataURL("image/png"); 24 console.log('w,h:'+width+','+height); 25 var BASE64_MARKER = ';base64,'; 26 var base64Index = pngdata.indexOf(BASE64_MARKER) + BASE64_MARKER.length; 27 var base64 = pngdata.substring(base64Index); 28 var raw = window.atob(base64); 29 var rawLength = raw.length; 30 var array = new Uint8Array(new ArrayBuffer(rawLength)); 31 for(i = 0; i < rawLength; i++) { 32 array[i] = raw.charCodeAt(i); 33 } 34 hogehoge...... 35 }) 36 37 }; 38 xhr.send(); 39 };
このコードなのですがfirefox,chromeでは動くがsaferiではだめだった。
理由を調べるとwindow.createImageBitmap(blob)でundefinedで落ちてた。
でさらに調べると
ImageBitmapがsafariではつかえないらしい。
https://developer.mozilla.org/en-US/docs/Web/API/ImageBitmap
ということは多分サファリでは他の関数で代用できるとおもわれますが
どうするのが正解か分かる人はいませんか?

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/09 10:59
2018/11/29 03:52
2018/12/04 03:06