2016-06-22 61 views
0

在Intel XDK中製作HTML5應用程序,因此計算是在Javascript中完成的。Javascript ByteBuffer以base64字符串不返回圖像

案例:從服務器獲取(谷歌)protobuf消息。解析爲對象。我們在那裏有一個圖像,JPG。貢訥把它放入HTML中。嘿,你可以使用base64 ...在Android中完成了這個;在那裏,你可以使用BitmapFactory:

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

後一些谷歌福發現這樣的東西:

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);