2016-06-22 6 views
0

Intel XDK에서 HTML5 앱을 만들므로 자바 스크립트로 계산됩니다.Javascript ByteBuffer to base64 문자열을 반환하지 않습니다.

사례 : 서버에서 (google) protobuf 메시지 받기. 객체를 파싱합니다. 거기에 이미지가 있습니다, jpg. Gonne은 이것을 HTML에 넣었습니다. 이봐, 당신은 base64를 사용할 수 있습니다. 안드로이드에서 이것을했습니다. 거기 당신은 BitmapFactory 사용할 수 있습니다

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

일부 구글 - 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); 
관련 문제