31

私は現在、C++サーバーから送信された画像を表示するWebSocketアプリケーションに取り組んでいます。私はそのあたりでいくつかのトピックを見てきましたが、Firefoxでこのエラーを取り除くことができないようです:

画像が破損または切り捨てられました:data:image / png; base64、[一部のデータ]

BLOBを表示するために使用しているJavascriptコードは次のとおりです。

socket.onmessage = function(msg) {
    var blob = msg.data;

    var reader = new FileReader();
    reader.onloadend = function() {
        var string = reader.result;
        var buffer = Base64.encode(string);
        var data = "data:image/png;base64,"+buffer;

        var image = document.getElementById('image');
        image.src = data;
    };
    reader.readAsBinaryString(blob);
}

このトピックで見つけた赤い点の画像を使用しています:https ://stackoverflow.com/a/4478878/1464608 そしてBase64クラスはここからです:https ://stackoverflow.com/a/246813/ 1464608

しかし、私が取得したbase64の結果は一致せず、Firefoxは画像が破損しているというエラーを取得します。

私はこれが多くの情報ではないことを知っていますが、どこを見ればよいかわかりません:/どんな助けでも大歓迎です!!

4

6 に答える 6

37

最もクリーンな解決策は、base64エンコーダーを変更して、文字列ではなくUint8Arrayを直接操作することだと思います。

重要:このためには、WebソケットのbinaryTypeを「arraybuffer」に設定する必要があります。

onmessageメソッドは次のようになります。

socket.onmessage = function(msg) {
    var arrayBuffer = msg.data;
    var bytes = new Uint8Array(arrayBuffer);

    var image = document.getElementById('image');
    image.src = 'data:image/png;base64,'+encode(bytes);
};

変換されたエンコーダーは次のようになります( https://stackoverflow.com/a/246813/1464608に基づく):

// public method for encoding an Uint8Array to base64
function encode (input) {
    var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var output = "";
    var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
    var i = 0;

    while (i < input.length) {
        chr1 = input[i++];
        chr2 = i < input.length ? input[i++] : Number.NaN; // Not sure if the index 
        chr3 = i < input.length ? input[i++] : Number.NaN; // checks are needed here

        enc1 = chr1 >> 2;
        enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
        enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
        enc4 = chr3 & 63;

        if (isNaN(chr2)) {
            enc3 = enc4 = 64;
        } else if (isNaN(chr3)) {
            enc4 = 64;
        }
        output += keyStr.charAt(enc1) + keyStr.charAt(enc2) +
                  keyStr.charAt(enc3) + keyStr.charAt(enc4);
    }
    return output;
}
于 2012-06-18T23:12:33.997 に答える
18

あなたはそれをもっと簡単に書くかもしれません:

socket.onmessage = function(msg) {
   var arrayBuffer = msg.data;
   var bytes = new Uint8Array(arrayBuffer);
   var blob = new Blob([bytes.buffer]);

   var image = document.getElementById('image');

   var reader = new FileReader();
   reader.onload = function(e) {
       image.src = e.target.result;
   };
   reader.readAsDataURL(blob);
};
于 2014-11-21T12:58:57.440 に答える
14

ありがとう、それはうまくいっています!

だから私は私の最終的なjavascriptコードを共有すると思います:

var socket = new WebSocket('ws://'+host+':'+port, protocol);
socket.binaryType = 'arraybuffer';

try {
    socket.onopen = function() {
        document.getElementById('status').style.backgroundColor = '#40ff40';
        document.getElementById('status').textContent = 'Connection opened';
    }

    socket.onmessage = function(msg) {
        var arrayBuffer = msg.data;
        var bytes = new Uint8Array(arrayBuffer);

        var image = document.getElementById('image');
        image.src = 'data:image/png;base64,'+encode(bytes);
    }

    socket.onclose = function(){
        document.getElementById('status').style.backgroundColor = '#ff4040';
        document.getElementById('status').textContent = 'Connection closed';
    }
} catch(exception) {
    alert('Error:'+exception);
}

ブロブバージョンがなぜそんなにトリッキーなのか本当に理解していませんが、これはトリックをしました!

于 2012-06-19T14:06:49.770 に答える
4

他の回答のおかげで、websocketでjpeg画像を受け取り、新しいウィンドウに表示することができました。

socket.binaryType = "arraybuffer";                 
socket.onmessage = function (msg) {
    var bytes = new Uint8Array(msg.data);
    var blob = new Blob([bytes.buffer]);
    window.open(URL.createObjectURL(blob),'Name','resizable=1');
};
于 2017-02-10T09:34:02.117 に答える
3

別の選択肢

let urlObject;

socket.onmessage = function(msg) {
    const arrayBuffer = msg.data;
    const image = document.getElementById('image');

    if (urlObject) {
        URL.revokeObjectURL(urlObject) // only required if you do that multiple times
    }
    urlObject = URL.createObjectURL(new Blob([arrayBuffer]));

    image.src = urlObject;

};
于 2016-10-29T16:45:51.113 に答える
0

これは、Blobを使用すると非常に簡単です。

// Small red dot image
const content = new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 5, 0, 0, 0, 5, 8, 6, 0, 0, 0, 141, 111, 38, 229, 0, 0, 0, 28, 73, 68, 65, 84, 8, 215, 99, 248, 255, 255, 63, 195, 127, 6, 32, 5, 195, 32, 18, 132, 208, 49, 241, 130, 88, 205, 4, 0, 14, 245, 53, 203, 209, 142, 14, 31, 0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130]);

document.getElementById('my-img').src = URL.createObjectURL(
  new Blob([content.buffer], { type: 'image/png' } /* (1) */)
);
Should display a small red dot: <img id="my-img">

(1)MIMEタイプを指定しなくても動作します。

于 2020-11-01T18:33:51.543 に答える