Пример #1
0
        public void Generate_Image_Hash()
        {
            var imageFilename             = @"C:\temp\image_tests\1.jpg";
            var secondImagFilename        = @"C:\temp\image_tests\2.jpg";
            var resizedFirstImageFilename = @"C:\temp\image_tests\1-resized.jpg";
            var thirdImageFilename        = @"C:\temp\image_tests\3.jpg";

            var hash = ImageHasher.AverageHash(imageFilename);

            Assert.True(hash > 0);

            var secondHash = ImageHasher.AverageHash(imageFilename);

            Assert.True(secondHash > 0);
            Assert.Equal(hash, secondHash);

            secondHash = ImageHasher.AverageHash(secondImagFilename);
            Assert.True(secondHash > 0);
            Assert.Equal(hash, secondHash);

            var similiar = ImageHasher.Similarity(imageFilename, secondImagFilename);

            Assert.Equal(100d, similiar);

            Assert.True(ImageHasher.ImagesAreSame(imageFilename, secondImagFilename));


            secondHash = ImageHasher.AverageHash(resizedFirstImageFilename);
            Assert.True(secondHash > 0);
            Assert.Equal(hash, secondHash);

            Assert.False(ImageHasher.ImagesAreSame(imageFilename, thirdImageFilename));
        }
Пример #2
0
 public static async Task <Image> GetImageFromUrlAsync(string url)
 {
     byte[] imageBytes = null;
     try
     {
         using (var webClient = new WebClient())
         {
             webClient.Headers.Add("user-agent", WebHelper.UserAgent);
             imageBytes = await webClient.DownloadDataTaskAsync(new Uri(url));
         }
     }
     catch
     {
     }
     if (imageBytes != null)
     {
         var signature = ImageHasher.AverageHash(imageBytes).ToString();
         var ib        = ImageHelper.ConvertToJpegFormat(imageBytes);
         return(new Image
         {
             Url = url,
             Status = Statuses.New,
             Signature = signature,
             Bytes = ib
         });
     }
     return(null);
 }
Пример #3
0
 public string GenerateSignature()
 {
     if (this.Bytes == null || !this.Bytes.Any())
     {
         return(null);
     }
     return(ImageHasher.AverageHash(this.Bytes).ToString());
 }
Пример #4
0
        public static async Task <IImage> GetImageFromUrlAsync(string url)
        {
            byte[] imageBytes = null;
            try
            {
                using (var webClient = new WebClient())
                {
                    webClient.Headers.Add("user-agent", UserAgent);
                    imageBytes = await webClient.DownloadDataTaskAsync(new Uri(url));
                }
            }
            catch
            {
            }

            try
            {
                if (imageBytes != null)
                {
                    var signature = ImageHasher.AverageHash(imageBytes).ToString();
                    var ib        = ImageHelper.ConvertToJpegFormat(imageBytes);
                    return(new Image(Guid.NewGuid())
                    {
                        Url = url,
                        Status = Statuses.New,
                        Signature = signature,
                        Bytes = ib
                    });
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine($"GetImageFromUrlAsync Url [{ url }], Exception [{ ex.ToString() }", "Warning");
            }
            return(null);
        }