3

我是 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 服务器上,

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 phone 发送的图像相同,只是没有任何 Exif 信息。任何人都可以指出我做错了什么导致信息丢失?我猜是因为我在 windows phone 代码中调用了 SaveJpeg 函数,重写了图像文件,并在那里丢失了所有信息,但我不知道如何将图像转换为字节并流式传输。

非常感谢您的帮助!谢谢你。

4

1 回答 1

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);
于 2012-10-16T23:21:44.793 回答