Пример #1
0
        private async Task <Image> CannyRequest(CannyRequest request)
        {
            if (request.Threshold1.HasValue && request.Threshold1.Value <= 0 || request.Threshold2.HasValue && request.Threshold2.Value <= 0)
            {
                throw new ArgumentException("Threshold has to be greater than 0");
            }
            if (request.ApertureSize.HasValue && request.ApertureSize.Value <= 0)
            {
                throw new ArgumentException("ApertureSize has to be greater than 0");
            }
            HttpClient httpClient = HttpClientLazy.Value;

            String             json        = JsonSerializer.Serialize(request);
            StringContent      jsonContent = new StringContent(json, Encoding.UTF8, "application/json");
            HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Post, this.Endpoint + "/canny");

            httpRequest.Content = jsonContent;
            httpRequest.Headers.Add("Authorization", "ApiKey " + ApiKey.ToString());

            HttpResponseMessage response = await httpClient.SendAsync(httpRequest);

            await CheckSuccess(response);

            byte[] responseBytes = await response.Content.ReadAsByteArrayAsync();

            MemoryStream memoryStream = new MemoryStream(responseBytes);

            return(Image.FromStream(memoryStream));
        }
Пример #2
0
        private async Task <Image> CannyRequest(CannyRequest request)
        {
            if (request.Threshold1.HasValue && request.Threshold1.Value <= 0 || request.Threshold2.HasValue && request.Threshold2.Value <= 0)
            {
                throw new ArgumentException("Threshold has to be greater than 0");
            }
            if (request.ApertureSize.HasValue && request.ApertureSize.Value <= 0)
            {
                throw new ArgumentException("ApertureSize has to be greater than 0");
            }
            HttpClient httpClient = httpClientLazy.Value;

            httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("ApiKey", apiKey.ToString());

            String        json        = JsonSerializer.Serialize(request);
            StringContent jsonContent = new StringContent(json, Encoding.UTF8, "application/json");

            HttpResponseMessage response = await httpClient.PostAsync(this.Endpoint + "/canny", jsonContent);

            if (response.StatusCode == System.Net.HttpStatusCode.BadRequest)
            {
                String stringContent = await response.Content.ReadAsStringAsync();

                JsonSerializerOptions serializerOptions = new JsonSerializerOptions()
                {
                    PropertyNameCaseInsensitive = true
                };
                BadRequestResult badRequestResult = JsonSerializer.Deserialize <BadRequestResult>(stringContent, serializerOptions);
                throw new BadRequestException(badRequestResult.Code, badRequestResult.Description);
            }
            else if (!response.IsSuccessStatusCode)
            {
                throw new Exception("Could not execute request");
            }

            byte[] responseBytes = await response.Content.ReadAsByteArrayAsync();

            MemoryStream memoryStream = new MemoryStream(responseBytes);

            return(Image.FromStream(memoryStream));
        }
Пример #3
0
        private async Task <Image> CannyRequest(Uri imageUrl, double?threshold1 = null, double?threshold2 = null, int?apertureSize = null, bool?l2Gradient = null)
        {
            CannyRequest request = new CannyRequest(imageUrl, threshold1, threshold2, apertureSize, l2Gradient);

            return(await CannyRequest(request));
        }