Ver código fonte

add GoogleCloud

姜青儀 2 anos atrás
pai
commit
4fc4aaa7e6

+ 58 - 0
GoogleCloud/Data/Vision/ImgesRequest.cs

@@ -0,0 +1,58 @@
+using Newtonsoft.Json;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace KdanCommon.GoogleCloud.Data.Vision
+{
+    public partial class ImgesRequest
+    {
+        [JsonProperty("requests")]
+        public ImageRequest[] Requests { get; set; }
+
+        public ImgesRequest(List<string> inputContents)
+        {
+            Requests = new ImageRequest[inputContents.Count];
+            for (int i = 0; i < inputContents.Count; i++)
+            {
+                Requests[i] = new ImageRequest()
+                {
+                    Image = new Image()
+                    {
+                        Content = inputContents[i]
+                    },
+                    Features = new Feature[]
+                    {
+                        new Feature()
+                        {
+                            Type = "TEXT_DETECTION"
+                        }
+                    }
+                };
+            }
+        }
+    }
+
+    public partial class ImageRequest
+    {
+        [JsonProperty("image")]
+        public Image Image { get; set; }
+
+        [JsonProperty("features")]
+        public Feature[] Features { get; set; }
+    }
+
+    public partial class Feature
+    {
+        [JsonProperty("type")]
+        public string Type { get; set; }
+    }
+
+    public partial class Image
+    {
+        [JsonProperty("content")]
+        public string Content { get; set; }
+    }
+}

+ 147 - 0
GoogleCloud/Data/Vision/ImgesResponse.cs

@@ -0,0 +1,147 @@
+using Newtonsoft.Json;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace KdanCommon.GoogleCloud.Data.Vision
+{
+    public partial class ImagesResponse
+    {
+        [JsonProperty("responses")]
+        public ImageResponse[] Responses { get; set; }
+    }
+
+    public partial class ImageResponse
+    {
+        [JsonProperty("textAnnotations")]
+        public TextAnnotation[] TextAnnotations { get; set; }
+
+        [JsonProperty("fullTextAnnotation")]
+        public FullTextAnnotation FullTextAnnotation { get; set; }
+    }
+
+    public partial class FullTextAnnotation
+    {
+        [JsonProperty("pages")]
+        public Page[] Pages { get; set; }
+
+        [JsonProperty("text")]
+        public string Text { get; set; }
+    }
+
+    public partial class Page
+    {
+        [JsonProperty("property")]
+        public WordProperty Property { get; set; }
+
+        [JsonProperty("width")]
+        public long Width { get; set; }
+
+        [JsonProperty("height")]
+        public long Height { get; set; }
+
+        [JsonProperty("blocks")]
+        public Block[] Blocks { get; set; }
+    }
+
+    public partial class Block
+    {
+        [JsonProperty("boundingBox")]
+        public Bounding BoundingBox { get; set; }
+
+        [JsonProperty("paragraphs")]
+        public Paragraph[] Paragraphs { get; set; }
+
+        [JsonProperty("blockType")]
+        public string BlockType { get; set; }
+    }
+
+    public partial class Bounding
+    {
+        [JsonProperty("vertices")]
+        public Vertex[] Vertices { get; set; }
+    }
+
+    public partial class Vertex
+    {
+        [JsonProperty("x")]
+        public long X { get; set; }
+
+        [JsonProperty("y")]
+        public long Y { get; set; }
+    }
+
+    public partial class Paragraph
+    {
+        [JsonProperty("boundingBox")]
+        public Bounding BoundingBox { get; set; }
+
+        [JsonProperty("words")]
+        public Word[] Words { get; set; }
+    }
+
+    public partial class Word
+    {
+        [JsonProperty("boundingBox")]
+        public Bounding BoundingBox { get; set; }
+
+        [JsonProperty("symbols")]
+        public Symbol[] Symbols { get; set; }
+
+        [JsonProperty("property", NullValueHandling = NullValueHandling.Ignore)]
+        public WordProperty Property { get; set; }
+    }
+
+    public partial class WordProperty
+    {
+        [JsonProperty("detectedLanguages")]
+        public DetectedLanguage[] DetectedLanguages { get; set; }
+    }
+
+    public partial class DetectedLanguage
+    {
+        [JsonProperty("languageCode")]
+        public string LanguageCode { get; set; }
+
+        [JsonProperty("confidence")]
+        public double Confidence { get; set; }
+    }
+
+    public partial class Symbol
+    {
+        [JsonProperty("boundingBox")]
+        public Bounding BoundingBox { get; set; }
+
+        [JsonProperty("text")]
+        public string Text { get; set; }
+
+        [JsonProperty("property", NullValueHandling = NullValueHandling.Ignore)]
+        public SymbolProperty Property { get; set; }
+    }
+
+    public partial class SymbolProperty
+    {
+        [JsonProperty("detectedBreak")]
+        public DetectedBreak DetectedBreak { get; set; }
+    }
+
+    public partial class DetectedBreak
+    {
+        [JsonProperty("type")]
+        public string Type { get; set; }
+    }
+
+    public partial class TextAnnotation
+    {
+        [JsonProperty("locale", NullValueHandling = NullValueHandling.Ignore)]
+        public string Locale { get; set; }
+
+        [JsonProperty("description")]
+        public string Description { get; set; }
+
+        [JsonProperty("boundingPoly")]
+        public Bounding BoundingPoly { get; set; }
+    }
+}

+ 59 - 0
GoogleCloud/GoogleCloud.cs

@@ -0,0 +1,59 @@
+using KdanCommon.GoogleCloud.Data.Vision;
+using KdanCommon.Helpers;
+using Newtonsoft.Json;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net.Http;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+using Windows.Globalization;
+using Windows.Storage;
+using Windows.System;
+
+namespace KdanCommon.GoogleCloud
+{
+    public class GoogleCloud
+    {
+        public static string VisionDomain = "https://cms.kdanmobile.com";
+        private Uri _visionImagesUri = null;
+
+        private HttpClient _httpClient = null;
+        private string _key = null;
+
+        public GoogleCloud(string key)
+        {
+            _key = key;
+            _httpClient = new HttpClient();
+            _visionImagesUri = new Uri($"{VisionDomain}/v1/images:annotate");
+        }
+
+        ~GoogleCloud()
+        {
+            _httpClient.Dispose();
+        }
+
+        private async Task<ImagesResponse> GetVisionImages(List<StorageFile> images)
+        {
+            ImagesResponse response = null;
+            var base64Contents = new List<string>();
+            foreach (var image in images)
+            {
+                // 最多16張圖
+                var base64Content = await Base64Helper.StorageFileToBase64(image);
+                base64Contents.Add(base64Content);
+            }
+            var imagesRequest = new ImgesRequest(base64Contents);
+            string requestJsonStr = JsonConvert.SerializeObject(imagesRequest);
+            var content = new StringContent(requestJsonStr, System.Text.Encoding.UTF8, "application/json");
+            var result = await _httpClient.PostAsync(_visionImagesUri, content);
+            if (result.StatusCode == System.Net.HttpStatusCode.OK)
+            {
+                var jsonString = await result.Content.ReadAsStringAsync();
+                response = JsonTool.DeserializeJSON<ImagesResponse>(jsonString);
+            }
+            return response;
+        }
+    }
+}

+ 33 - 0
Helpers/Base64Helper.cs

@@ -0,0 +1,33 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Windows.Storage.Streams;
+using Windows.Storage;
+
+namespace KdanCommon.Helpers
+{
+    public static class Base64Helper
+    {
+        // https://stackoverflow.com/questions/18553691/metro-getting-the-base64-string-of-a-storagefile/18555063
+        public static async Task<string> StorageFileToBase64(StorageFile file)
+        {
+            string base64String = "";
+            if (file != null)
+            {
+                using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read))
+                {
+                    using(var reader = new DataReader(fileStream.GetInputStreamAt(0)))
+                    {
+                        await reader.LoadAsync((uint)fileStream.Size);
+                        byte[] byteArray = new byte[fileStream.Size];
+                        reader.ReadBytes(byteArray);
+                        base64String = Convert.ToBase64String(byteArray);
+                    }
+                }
+            }
+            return base64String;
+        }
+    }
+}

+ 5 - 0
KdanCommon.csproj

@@ -125,6 +125,10 @@
     <Compile Include="CMSCollection\Data\JsonSetting.cs" />
     <Compile Include="CMSCollection\Data\ViewerEventBarSettingResponse.cs" />
     <Compile Include="CMSCollection\Data\WindowsCardsResponse.cs" />
+    <Compile Include="GoogleCloud\Data\Vision\ImgesRequest.cs" />
+    <Compile Include="GoogleCloud\Data\Vision\ImgesResponse.cs" />
+    <Compile Include="GoogleCloud\GoogleCloud.cs" />
+    <Compile Include="Helpers\Base64Helper.cs" />
     <Compile Include="Helpers\ColorCodeExtension.cs" />
     <Compile Include="Helpers\DynamicJsonHelper.cs" />
     <Compile Include="Helpers\HttpClientHelper.cs" />
@@ -144,6 +148,7 @@
       <Version>13.0.1</Version>
     </PackageReference>
   </ItemGroup>
+  <ItemGroup />
   <PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '14.0' ">
     <VisualStudioVersion>14.0</VisualStudioVersion>
   </PropertyGroup>