简体   繁体   English

使用字节数组转换从Java发送图像到C#

[英]Sending image with byte array convertion, from java to c#

I am trying to send a .jpg file which is on my android device to my server computer. 我正在尝试将Android设备上的.jpg文件发送到服务器计算机。

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. 为此,我正在通过Java android应用程序将图片转换为字节数组,并将其作为参数发送到我的服务器计算机。 I`m doing this by a web service call. 我正在通过网络服务呼叫来做到这一点。

The first function is edited: 第一个功能被编辑:

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;

}

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. 我的Web服务是用ASP.NET(C#)语言编写的,并且有一个函数将字节数组作为参数并将其转换回服务器计算机上的映像。

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

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. 我已经调试了它,似乎问题是由于Image.FromStream()函数引起的。

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. 我非常愿意提供任何帮助。

Thanks. 谢谢。

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! 如果有人试图将图像发送到服务器并且双方都有不同的平台,则不要将图像转换为字节数组!

The reason is, in my case the image which is converted into byte array on Java differs from the byte array on C#. 原因是,就我而言,在Java上转换为字节数组的图像与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#. 在Java上创建的字节数组在C#上没有正确的格式。

Hence anyone wants data transferring from one language to another, use Base64 encoding. 因此,任何人都希望使用Base64编码将数据从一种语言传输到另一种语言。 Convert the image into Base64 string on one side and send it as string to the other language. 在一侧将图像转换为Base64字符串,然后将其作为字符串发送给另一种语言。 Since Base64 format is same on every language there wont be any problem to reproduce it. 由于每种语言的Base64格式都相同,因此重现它不会有任何问题。

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

This is the code where I get the image and convert it into Base64 string on Java android application, 这是我获取图像并将其转换为Java android应用程序上的Base64字符串的代码,

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

The code above takes the Base64 type string and converts back into an image. 上面的代码采用Base64类型的字符串并将其转换回图像。 This is written in C#. 这是用C#编写的。

With such an incomplete code example and such a vague problem description, it's difficult to know for sure what the problem is. 有了如此不完整的代码示例和模糊的问题描述,很难确定问题出在哪里。

However, reviewing the code you did post, I see one bug that would be significant if this is really the code you are using. 但是,回顾一下您发布的代码,我发现一个错误,如果它确实是您正在使用的代码,那将是很重要的。 In your Java methodConvertion() method, you have this statement: 在您的Java methodConvertion()方法中,您具有以下语句:

data = output.toByteArray();

The problem is that all that does is create a new byte[] object and assign the reference to your local variable named data . 问题是所有要做的就是创建一个新的byte[]对象,并将引用分配给名为data局部变量。 That object never leaves the method. 该对象永远不会离开方法。

Presumably you have some other code which, after calling methodConvertion() , sends the byte[] object that is referenced by the argument you passed to that method. 大概您还有一些其他代码,在调用methodConvertion()之后,这些代码将发送由传递给该方法的参数所引用的byte[]对象。 But that object is just going to be whatever it was before you called the method. 但是该对象将与调用该方法之前的状态一样。

You should instead change your Java code so that it looks like this: 您应该改为更改Java代码,使其看起来像这样:

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

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

        byte [] buffer = new byte [65536];
        int l;

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

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

        return output.toByteArray();

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

And then in the caller, you should check the return value and only proceed if the value is not null , reporting the error somehow otherwise. 然后在调用方中,您应该检查返回值,并且仅在返回值不为null继续操作,否则以某种方式报告错误。

If that doesn't address your question, you should edit the question so that it has a better code example , and so that you are much more specific about what's wrong. 如果那不能解决您的问题,则应编辑问题,使其具有更好的代码示例 ,以使您更清楚地了解问题所在。 What happens when you run the code, and how is that different from what you expected? 运行代码时会发生什么,这与您的预期有何不同? Be sure to clearly state any error messages, quoting them exactly, and including any stack traces from exceptions. 确保明确声明所有错误消息,准确引用它们,并包括来自异常的所有堆栈跟踪。

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

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