2016-06-22 5 views
0

Intel XDKでHTML5アプリケーションを作成するので、計算はJavascriptで行われます。Javascript ByteBufferからbase64に文字列を戻しません

ケース:サーバーから(google)protobufメッセージを受信して​​います。オブジェクトを解析する。そこにはイメージがあります。 GonneはそれをHTMLに入れました。ねえ、あなたはそれのためにbase64を使うことができます... Androidでこれをしました。そこにはBitmapFactoryを使用することができます。

Bitmap bitmap = android.graphics.BitmapFactory.decodeStream(document.getDocBlob().newInput()); 

一部のGoogle-FUは、このようなものを見つけた後:ここで

var base64String = btoa(String.fromCharCode.apply(null, new Uint8Array(currentComment.Document.doc_blob.buffer))); 

var ByteBuffer = ProtoBuf.ByteBuffer; 
var base64String = ByteBuffer.btoa(currentComment.Document.doc_blob.buffer, currentComment.Document.doc_blob.littleEndian, currentComment.Document.doc_blob.noAssert); 

は思わぬ障害である:画像は表示されません:それは壊れたリンク画像を示しています。しかし、上記の最初の関数を使用すると、エラーはスローされません。私が間違っていると思うところは相殺されている。データ構造は次のようになります。

buffer: ArrayBuffer 
    byteLength: 148199 
    __proto__: ArrayBuffer 
limit: 69895 
littleEndian: true 
markedOffset: -1 
noAssert: false 
offset: 44278 
view: DataView 

HTMLへの設定は、それほどのように行われ、それが他のbase64で文字列でそれをテストしてきた、作品:

commentImage = document.getElementById("img-frame"); 
var source = "data:image/" + image_type + ";base64," + base64String; 

commentImage.setAttribute("height", currentComment.Document.doc_height); 
commentImage.setAttribute("width", currentComment.Document.doc_width); 
commentImage.setAttribute("src", source); 

答えて

0

これは、我々はそれがうまく行わどのようにされました:ブロブは、ブロブ全体であり、画像が開始されたポイントと終了すべきポイントがあります。

オプションA:ノーマルbtoa() - より高速

var base64StringA = btoa(String.fromCharCode.apply(null, new Uint8Array(currentComment.Document.doc_blob.buffer.slice(currentComment.Document.doc_blob.offset,currentComment.Document.doc_blob.limit)))); 

オプションB:Bytebuffer.btoa() - わずかに遅く(私はこれが原因かbtoa(依存しないように安全な選択肢だと思います)ウィンドウ内で定義されている、使用されているブラウザに依存している...)

var base64StringB = ByteBuffer.btoa(String.fromCharCode.apply(null, new Uint8Array(currentComment.Document.doc_blob.buffer.slice(currentComment.Document.doc_blob.offset, currentComment.Document.doc_blob.limit))), currentComment.Document.doc_blob.littleEndian, currentComment.Document.doc_blob.noAssert); 
関連する問題