简体   繁体   中英

send images from pc to android

I'm trying to write an application that creates images on the PC and transmits over WiFi and displays it on an Android. I have everything working except the last part. The Android and PC are sending messages back and forth. The PC creates images, converts it to a byte array, sends it to the Android, Android receives it. The thing that doesn't work is converting the byte array back to an image. Here is my code.

The C# code on my PC uses this to create the byte array

public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
    MemoryStream ms = new MemoryStream();
    imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp );
    return ms.ToArray();
}

The Java code on my Android uses this code to convert the byte array back to an image.

try {
    //This line always returns NULL
    Bitmap bmp=BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    if (bmp != null) {
        //display image in UI
        imgViewer.setImageBitmap(bmp);
        imgViewer.invalidate();
    }
    else
    {
        Log.i(Consts.TAG, "image is null ");
    }                   
} catch (Exception e){
    Log.i(Consts.TAG, "ERROR decoding image " + e.toString());
}

BitmapFactory.decodeByteArray() always returns NULL. Am i creating the byte array correctly on the PC? Should i be recreating the image differently on the Android?

Thanks in advance,

Mike

I figured it out. I needed to convert the image like this. Now it works.

    public static byte[] ImageToByte(Image img)
    {
        ImageConverter converter = new ImageConverter();
        return (byte[])converter.ConvertTo(img, typeof(byte[]));
    }

Thanks to zapl and FoamyGuy for your input.

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