用EXIF数据上传图像到服务器

本文关键字:图像 服务器 EXIF 数据 | 更新日期: 2023-09-27 18:06:40

我没有尝试在设备或服务器上读取EXIF数据,但是数据需要存在于服务器上。

我目前通过将图像转换为byte[],然后转换为base64 Convert.ToBase64(byte[])并使用JSON发送图像到服务器-参见下面的代码。

但是当我在另一端获得文件时,它没有任何EXIF数据。如果我使用假SD卡从模拟器中获取图像,则EXIF数据存在。

有没有人知道如何上传图像,然后在另一端重建它,使EXIF数据保持完整?

图像到字节到base64到JSON

BitmapImage image = new BitmapImage();
image.SetSource(e.ChosenPhoto);
image = ResizeImage(image);
byte[] imageBytes;
using (MemoryStream ms = new MemoryStream())
{
    WriteableBitmap btmMap = new WriteableBitmap(image);
    System.Windows.Media.Imaging.Extensions.SaveJpeg(btmMap, ms, image.PixelWidth, Image.PixelHeight, 0, 100);
    image = null;
    imageBytes = ms.ToArray();
 }
 Base64Image imagestring = new Base64Image();
 imagestring.imagestring = Convert.ToBase64String(imageBytes);
 string json = JsonConvert.SerializeObject(imagestring);

JSON To base64 To byte[] To image

using (StreamReader sr = new StreamReader(inputStream))
{
    postData = sr.ReadToEnd();
}
JavaScriptSerializer deserializer = new JavaScriptSerializer();
Dictionary<string, object> jsonObjects = (Dictionary<string, object>)deserializer.DeserializeObject(postData);
string base64image = jsonObjects["imagestring"].ToString();
byte[] imagebytes = Convert.FromBase64String(base64image);
BitmapImage bitmapImage = new BitmapImage();
MemoryStream ms = new MemoryStream(imagebytes);
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnDemand;
bitmapImage.CreateOptions = BitmapCreateOptions.None;
bitmapImage.Rotation = Rotation.Rotate0;
bitmapImage.StreamSource = ms;
bitmapImage.EndInit();
bitmapImage.CreateOptions = BitmapCreateOptions.None;
WriteableBitmap wBmp = new WriteableBitmap(bitmapImage);
var encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(wBmp));

用EXIF数据上传图像到服务器

我不得不完全改变我的代码,以切断任何连接到位图或其他格式,只是通过它使用byte[]。看到代码:

应用程序:

ImageData imagedata = new ImageData();
byte[] imagebytes = new byte[e.ChosenPhoto.Length];
e.ChosenPhoto.Read(imagebytes, 0, int.Parse(e.ChosenPhoto.Length.ToString()));
imagedata.imagestring = Convert.ToBase64String(imagebytes);
string json = JsonConvert.SerializeObject(imagedata);

在服务器端:

JavaScriptSerializer deserializer = new JavaScriptSerializer();
deserializer.MaxJsonLength = 50000000;
Dictionary<string, object> jsonObjects = (Dictionary<string, object>)deserializer.DeserializeObject(postData);
string base64image = jsonObjects["imagestring"].ToString();              
byte[] imagebytes = Convert.FromBase64String(base64image);
Guid imagename = Guid.NewGuid();
if (!Directory.Exists(EM.ImagePath))
   Directory.CreateDirectory(EM.ImagePath);
using (FileStream sw = new FileStream(EM.ImagePath + imagename + ".jpg", FileMode.CreateNew))
{
   sw.Write(imagebytes, 0, imagebytes.Length);
}