简体   繁体   中英

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 this :

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

?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM