我是Windows Phone(和StackOverflow tbh)編程中的新手。目前,我正在處理一個任務,該任務涉及將存儲在Windows Phone存儲中的圖像(使用Exif信息)發送到Java服務器。到目前爲止,我已經成功地從Windows Phone客戶端發送字節流,並在Java端構建圖像,但Exif信息不知何故丟失。我相信這只是我在Windows Phone上發送它導致問題的方式。非常感謝您的幫助或指導! 這是我在Windows Phone客戶端代碼:發送圖像與從Windows Phone到Java服務器的Exif信息
// 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手機發送的一個,沒有任何Exif信息。任何人都可以指出我做錯了什麼,導致信息丟失?我猜測是因爲我在Windows Phone代碼中調用了SaveJpeg函數,重寫了圖像文件並丟失了所有信息,但我不知道如何將圖像轉換爲字節並進行流式處理。
很多幫助表示讚賞!謝謝。
那你試試? –
這可能有助於將代碼簡化爲一個更小,更簡單的測試用例來演示您的問題。 –
@RichardPena所以我創建了一個WriteableBitMap,將其轉換爲MemoryStream,然後轉換爲字節數組。據說Jave服務器接收字節數組並將其寫入文件。圖像在那裏,但我失去了Exif信息。這是我的問題.. – Yoshi3003