Beispiel #1
0
        private static ImgHash GetImgHash(string f)
        {
            var hash = new ImgHash();

            hash.GenerateFromPath(f);
            return(hash);
        }
        private async Task<Boyan> ProcessImageBoyan(Message message)
        {
            var biggestPhoto = message.Photo.OrderByDescending(x => x.FileSize).FirstOrDefault();

            var tempFolderPath = Path.Combine(AppContext.BaseDirectory, "Tmp");
            if(Directory.Exists(tempFolderPath) == false)
                Directory.CreateDirectory(tempFolderPath);

            var filePath = Path.Combine(tempFolderPath, $"{Guid.NewGuid()}.jpg");
            using (var stream = File.Create(filePath))
            {
                var file = await Bot.GetInfoAndDownloadFileAsync(biggestPhoto.FileId, stream);
            }

            var hash = new ImgHash();
            hash.GenerateFromPath(filePath);

            Boyan boyan = null;
            foreach (var imageBoyan in Boyans.Items.Where(x => x.ImageHash != null))
            {
                var similarity = hash.CompareWith(imageBoyan.ImageHash);
                if (similarity >= 99)
                {
                    boyan = imageBoyan;
                    break;
                }
            }

            if(boyan == null)
            {
                var newBoyan = new Boyan { ImageHash = hash.HashData, MessageId = message.MessageId.ToString(), ChatId = message.Chat.Id.ToString(), DateCreated = DateTimeOffset.UtcNow };
                Boyans.Items.Add(newBoyan);
            }

            return boyan;
        }