如何使用网络服务拍摄图像?

时间:2019-03-27 12:22:19

标签: react-native

我有一个在线数据库,并将图像放在那里。 我使用fetch调用网络服务,但是由于图像,它给了我一个错误。如何处理呢?

 fetch('url', {
       method: 'POST',
       headers: new Headers({
        Accept: 'application/json',
        Content-Type': 'application/json', // <-- Specifying the Content-Type
       }),
      })
      .then((response) => { 
         console.log(response);
        });

这是我的东西。正如我所说,当调用网络服务时,由于未获取图像而给我一个错误

我在Web服务中使用以下序列化器:

public class Data
    {
        public string Ref { get; set; }

        public int CharacterID { get; set; }
        public string CharacterName { get; set; }
        public string Regions { get; set; }
        public string Rarity { get; set; }
        public char Role { get; set; }
        public string Origin { get; set; }
        public int Power { get; set; }
        public string Tag1 { get; set; }
        public string Tag2 { get; set; }
        public string Tag3 { get; set; }
        public string RareMaterial { get; set; }
        public string EpicMaterial { get; set; }
        public string UniqueMaterial { get; set; }
        public string Image { get; set; }


        [JsonConverter(typeof(ImageConverter))]
        public Image Images { get; set; }

    }

    public class ImageConverter : JsonConverter
    {
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var base64 = (string)reader.Value;
            // convert base64 to byte array, put that into memory stream and feed to image
            return Image.FromStream(new MemoryStream(Convert.FromBase64String(base64)));
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            var image = (Image)value;
            // save to memory stream in original format
            var ms = new MemoryStream();
            image.Save(ms, image.RawFormat);
            byte[] imageBytes = ms.ToArray();
            // write byte array, will be converted to base64 by JSON.NET
            writer.WriteValue(imageBytes);
        }

        public override bool CanConvert(Type objectType)
        {
            return objectType == typeof(Image);
        }
    }

如何在本机反序列中对此反序列化?

0 个答案:

没有答案