简体   繁体   English

将图片从PC发送到Android

[英]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. 我正在尝试编写一个在PC上创建图像并通过WiFi传输并将其显示在Android上的应用程序。 I have everything working except the last part. 除了最后一部分,我一切正常。 The Android and PC are sending messages back and forth. Android和PC正在来回发送消息。 The PC creates images, converts it to a byte array, sends it to the Android, Android receives it. PC创建图像,将其转换为字节数组,将其发送到Android,Android接收它。 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 我PC上的C#代码使用它来创建字节数组

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. 我的Android上的Java代码使用此代码将字节数组转换回图像。

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. BitmapFactory.decodeByteArray()始终返回NULL。 Am i creating the byte array correctly on the PC? 我可以在PC上正确创建字节数组吗? Should i be recreating the image differently on the Android? 我应该在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. 感谢zapl和FoamyGuy的输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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