使用Exif信息从Windows Phone向Java Server发送图像

时间:2012-10-16 02:17:45

标签: image send windows-phone exif java-server

我是Windows Phone编程的新手(和StackOverflow tbh)。目前,我正在开展一项任务,涉及将存储在Windows Phone存储中的图像(使用Exif信息)发送到Java服务器。到目前为止,我已成功从Windows Phone客户端发送字节流并在Java端构建图像,但是,Exif信息以某种方式丢失。我相信这只是我在Windows Phone上发送导致问题的方式。非常感谢任何帮助或指导! 这是我在Windows Phone客户端上的代码:

// Windows Phone Client code (MainPage.xaml.cs)
// This function is called when an image is selected from
// the task PhotoChooserTask ptask, which brings
// a popup that allows the user to choose an image
// from the phone's storage
void ptask_Completed(object sender, PhotoResult e)
{
       if (e.TaskResult == TaskResult.OK && e.ChosenPhoto != null)
       {
           //Take JPEG stream and decode into a WriteableBitmap object
           App.CapturedImage = PictureDecoder.DecodeJpeg(e.ChosenPhoto);

           // Attempt to send the image
           WriteableBitmap pic = new WriteableBitmap(App.CapturedImage);
           MemoryStream stream = new MemoryStream();

           pic.SaveJpeg(stream, App.CapturedImage.PixelHeight, App.CapturedImage.PixelWidth, 0, 100);
           stream.Seek(0, SeekOrigin.Begin);
           client.Send(stream);

           // Close the socket connection explicitly
           client.Close();
       }
   }

这是SocketClient.cs中的代码

       public string Send(MemoryStream data)
       {
       byte[] msData = data.ToArray();

       if (_socket != null)
       {
           // Create SocketAsyncEventArgs context object
           SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();

           // Set properties on context object
           socketEventArg.RemoteEndPoint = _socket.RemoteEndPoint;
           socketEventArg.UserToken = null;

           socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e)
           {
               _clientDone.Set();
           });

           // Add the data to be sent into the buffer
           socketEventArg.SetBuffer(msData, 0, msData.Length);

           // Sets the state of the event to nonsignaled, causing threads to block
           _clientDone.Reset();

           // Make an asynchronous Send request over the socket
           _socket.SendAsync(socketEventArg);

       }
       else
       {
           response = "Socket is not initialized";
       }

       return response;
   }

在Java Server上,

public static void main(String[] args) {
    ServerSocket serverSocket;
    Socket client;
    try {
        serverSocket = new ServerSocket(PORT_NUMBER);
        while (true) {
            client = serverSocket.accept();

            // Extract exif info
            InputStream inputStream = client.getInputStream();
            InputStream stream = new BufferedInputStream(inputStream);

            // Create file from the inputStream
            File file = new File("image.jpg");
            try {
                OutputStream os = new FileOutputStream(file);
                byte[] buffer = new byte[4096];
                    for (int n; (n = stream.read(buffer)) != -1;) {
                        os.write(buffer, 0, n);
                    }
                }

输出图像与从Windows手机发送的图像相同,只是没有任何Exif信息。任何人都可以指出我做错了什么导致信息丢失?我猜是因为我在windows手机代码中调用了SaveJpeg函数,重写了图像文件,并丢失了所有信息,但我不知道如何将图像转换为字节并将其传输。

非常感谢!谢谢。

1 个答案:

答案 0 :(得分:1)

我找到了自己问题的答案。对于那些可能有这个问题的人。我只是使用:

Byte[] imageData = new byte[e.ChosenPhoto.Length];
e.ChosenPhoto.Position = 0;
e.ChosenPhoto.Read(imageData, 0, imageData.Length);

然后在我的发送函数中发送字节数组:

socketEventArg.SetBuffer(imageData, 0, imageData.Length);