Convert Java Byte[] to VB.NET MemoryStream
We are developing an Android Application (Java) that reads an image, encode the bytes in base64 to send them over HTTP (via GET) to a WebService written in VB.NET.
On .NET side, they are using t开发者_如何学JAVAhis :
Dim Pix As Image
Pix = Image.FromFile("C:\Users\Public\Pictures\Sample Pictures\Tree.jpg")
Dim ms As New MemoryStream
Pix.Save(ms, ImageFormat.Jpeg)
Dim ImByte() As Byte = ms.GetBuffer
ms.Close()
Sounds great.
How can I pass the correct string to them to correctly decode the image from Java encoding (unsigned) to .NET decoding (signed)?
Many thanks Nicolas.
First mistake: you're using GetBuffer()
which will potentially be too big. Use ToArray()
instead.
On the Java side, just use any Base64 decoder, such as the Apache Commons Codec one. Don't worry about the signedness of the bytes - Base64 effectively makes that a non-issue for you.
That's assuming the web service client doesn't perform this automatically for you, of course... if your web service "advertises" a byte array using base64, it may well just be automatic.
Side question: why bother loading the image as an image at all? Why not just use:
Dim ImByte() As Byte = File.ReadAllBytes("C:\Users\...\Tree.jpg")
?
精彩评论