发送图像的字节数组皈依,从Java到C# [英] Sending image with byte array convertion, from java to c#

查看:229
本文介绍了发送图像的字节数组皈依,从Java到C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图发送一个.jpg文件这是我的Android设备到我的服务器计算机上。

I am trying to send a .jpg file which is on my android device to my server computer.

要做到这一点,我的图片转换为字节由Java的Android应用程序数组,并发送它作为参数传递到我的服务器计算机上。我真的由Web服务调用这样做。

To do this, I am converting the picture into a byte array by a java android application, and sending it as an argument to my server computer. I`m doing this by a web service call.

第一个函数编辑:

public static byte[] ImageConvertion(){

    File inputFile = new File("/storage/emulated/0/IFSpictures/icon-si_strapclamp.jpg");
    byte[] data;

    try{
        FileInputStream input = new FileInputStream(inputFile);
        ByteArrayOutputStream output = new ByteArrayOutputStream ();

        byte[] buffer = new byte[65535];

        int l;

        while ((l = input.read(buffer)) > 0)
            output.write (buffer, 0, l);

        input.close();
        output.close();

        data = output.toByteArray();
        return data;


    } catch (IOException e) {
        System.err.println(e);
        data=null;
    }
    return data;

}



我的网络服务是写在ASP.NET(C# )语言,还有就是需要的字节数组作为参数,然后将其转换回服务器计算机上的图像的功能。

My web-service is written in ASP.NET (C#) language, and there is a function that takes the byte array as an argument and converts it back into an image on server computer.

[WebMethod]
public void ByteArrayToPicture(byte[] imageData)
{
    using (var ms = new MemoryStream(imageData))
    {
        Image image = Image.FromStream(ms);
        image.Save(@"C:\newImage.jpg");
    }
}



不过,我couldn`t这样做是因为的Web服务端。我已经调试它了,似乎这个问题是因为Image.FromStream()函数。

However, I couldn`t do it because of the web-service side. I have debugged it that and it seems that the problem is because of the Image.FromStream() function.

我绝对不有与传递参数的任何问题。我认为,无论是,语言冲突或转换图像到字节和副诗句可能导致的问题。有没有人有任何想法或看到什么了吗?

I definitely don`t have any problems with passing the arguments. I think either, the language conflict or the conversion image to byte and vice-verse may be leading the problem. Does anyone has any idea or see something wrong?

我长久适当的任何帮助。

I muchly appropriate any help.

感谢。

推荐答案

对不起我的不完整的问题,但是我想给一些提示,不管是谁试图做同样的事情。

sorry for my incomplete question, however I want to give some tips whoever is trying to do the same thing.

如果有人试图将图像发送到服务器和两侧都有不同的平台,那就不要将图像转换成字节数组!

If anyone is trying to send an image to a server and both side has different platforms, then do not convert the image into byte array!

究其原因,在我的情况被转换成字节数组Java中的图像从C#中的字节数组不同。因此,根据我的研究是不可能收集关于服务器侧的图像。关于Java创建的字节数组不会对C#中的正确的格式。

The reason is, in my case the image which is converted into byte array on Java differs from the byte array on C#. Therefore according to my research it is not possible to gather the image on the server side. The byte array created on Java wont have the right format on C#.

因此,任何人想要的数据从一种语言转移到另一个,使用Base64编码。将图像转换成字符串的Base64一侧并将其作为字符串到另一种语言。由于Base64编码格式是在每一个语言相同并且不会有任何问题重现

Hence anyone wants data transferring from one language to another, use Base64 encoding. Convert the image into Base64 string on one side and send it as string to the other language. Since Base64 format is same on every language there wont be any problem to reproduce it.

我用下面的代码出售的问题:

I sold the problem with the following codes:

Bitmap ourbitmap = BitmapFactory.decodeStream(imageStream, null, options);
ByteArrayOutputStream baos = new ByteArrayOutputStream();  
ourbitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);   
byte[] b = baos.toByteArray(); 
test = Base64.encodeToString(b, Base64.DEFAULT); 

这是从哪里获得的图像,并将其转换为Base64编码字符串Java的Android应用程序中的代码,

This is the code where I get the image and convert it into Base64 string on Java android application,

byte[] imageBytes = Convert.FromBase64String(Base64ImageData); 
MemoryStream ms = new MemoryStream(imageBytes, 0,
imageBytes.Length);

ms.Write(imageBytes, 0, imageBytes.Length);
Image image = Image.FromStream(ms, true);
image.Save(@"D:\tmpImage.jpg");



上面的代码需要为Base64类型的字符串,并将其转换回图像。这是用C#。

The code above takes the Base64 type string and converts back into an image. This is written in C#.

这篇关于发送图像的字节数组皈依,从Java到C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆