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

こ雲淡風輕ζ 提交于 2019-12-10 10:29:37

问题


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

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.

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.

[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.

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.


回答1:


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#. 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#.

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

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

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




回答2:


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:

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

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.

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.



来源:https://stackoverflow.com/questions/27285367/sending-image-with-byte-array-convertion-from-java-to-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!