Exemplo n.º 1
0
        public async Task <ActionResult> CalculateHashes()
        {
            // Получим информацию о файле
            var mime = Request.ContentType;

            if (mime == null)
            {
                return(StatusCode(415));                // Unsupported Media Type
            }
            var filePath = Path.GetTempFileName();

            using (var fs = new FileStream(filePath, FileMode.Create))
            {
                await Request.Body.CopyToAsync(fs);
            }

            var result = new HashResult();

            using (var img = new ImgProcessing(filePath))
            {
                result.AHash = img.Processing(Mode.CropAHash);
                result.PHash = img.Processing(Mode.CropPHash);
                result.DHash = img.Processing(Mode.CropDHash);
            }

            var file = new FileInfo(filePath);

            file.Delete();

            return(Ok(result));
        }
Exemplo n.º 2
0
        private static void Main()
        {
            const Mode mode = Mode.CropPHash;

            using (var proc1 = new ImgProcessing(@"test1.jpg"))
                using (var proc2 = new ImgProcessing(@"test2.jpg"))
                {
                    var hash1 = proc1.Processing(mode);
                    var hash2 = proc2.Processing(mode);

                    // Вывод результатов
                    ImgProcessing.Print(hash1);
                    Console.WriteLine(Convert.ToString(hash1, 2).PadLeft(64, '0'));
                    Console.WriteLine(Convert.ToString(hash1, 16));

                    // Вывод результатов
                    ImgProcessing.Print(hash2);
                    Console.WriteLine(Convert.ToString(hash2, 2).PadLeft(64, '0'));
                    Console.WriteLine(Convert.ToString(hash2, 16));

                    Console.WriteLine("\nHamDistanse: " + ImgProcessing.GetHamDistanse(hash1, hash2));
                }
        }
Exemplo n.º 3
0
        public ActionResult <ResponseWrapper> Post(RequestWrapper model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var file1 = Path.GetTempFileName();
            var file2 = Path.GetTempFileName();

            using (WebClient webClient = new WebClient())
            {
                webClient.DownloadFile(model.RequestMergingImages.Request.UrlImage1, file1);
                webClient.DownloadFile(model.RequestMergingImages.Request.UrlImage2, file2);
            }

            var result = new ResponseWrapper();

            result.ResponseMergingImages.Response.RequestId = model.RequestMergingImages.Request.RequestId;

            long aHash1;
            long pHash1;
            long dHash1;
            long aHash2;
            long pHash2;
            long dHash2;

            var sw = Stopwatch.StartNew();

            sw.Start();

            using (var img1 = new ImgProcessing(file1))
            {
                aHash1 = img1.Processing(Mode.CropAHash);
                pHash1 = img1.Processing(Mode.CropPHash);
                dHash1 = img1.Processing(Mode.CropDHash);
            }

            using (var img2 = new ImgProcessing(file2))
            {
                aHash2 = img2.Processing(Mode.CropAHash);
                pHash2 = img2.Processing(Mode.CropPHash);
                dHash2 = img2.Processing(Mode.CropDHash);
            }

            var DiffrenseHashA = Convert.ToString(aHash1 ^ aHash2, 2).PadLeft(64, '0');
            var DiffrenseHashP = Convert.ToString(pHash1 ^ pHash2, 2).PadLeft(64, '0');
            var DiffrenseHashD = Convert.ToString(dHash1 ^ dHash2, 2).PadLeft(64, '0');

            var HammingDistanseHashA = DiffrenseHashA.ToCharArray().Count(x => x == '1');
            var HammingDistanseHashP = DiffrenseHashP.ToCharArray().Count(x => x == '1');
            var HammingDistanseHashD = DiffrenseHashD.ToCharArray().Count(x => x == '1');

            result.ResponseMergingImages.Response.ResultA = 100 - (HammingDistanseHashA / 64.0) * 100;
            result.ResponseMergingImages.Response.ResultP = 100 - (HammingDistanseHashP / 64.0) * 100;
            result.ResponseMergingImages.Response.ResultD = 100 - (HammingDistanseHashD / 64.0) * 100;

            sw.Stop();
            result.ResponseMergingImages.Response.ExecutionTimeMsec = sw.ElapsedMilliseconds;

            var file = new FileInfo(file1);

            if (file.Exists)
            {
                file.Delete();
            }

            file = new FileInfo(file2);
            if (file.Exists)
            {
                file.Delete();
            }

            return(result);
        }
Exemplo n.º 4
0
        public async Task <ViewResult> Upload(List <IFormFile> files)
        {
            var sw        = Stopwatch.StartNew();
            var result    = new ResultModel();
            var firstFile = true;

            foreach (var formFile in files)
            {
                if (formFile.Length <= 0)
                {
                    continue;
                }

                var filePath = Path.GetTempFileName();

                using (var stream = new FileStream(filePath, FileMode.Create))
                {
                    await formFile.CopyToAsync(stream);
                }

                using (var img = new ImgProcessing(filePath))
                {
                    if (firstFile)
                    {
                        sw.Start();
                        result.Img1Hash = img.Processing(Mode.CropAHash);
                        sw.Stop();
                        result.TimeHash += sw.ElapsedMilliseconds;

                        sw.Start();
                        result.Img1PHash = img.Processing(Mode.CropPHash);
                        sw.Stop();
                        result.TimePHash += sw.ElapsedMilliseconds;

                        sw.Start();
                        result.Img1Magic = img.Processing(Mode.CropDHash);
                        sw.Stop();
                        result.TimeMagic += sw.ElapsedMilliseconds;

                        firstFile = false;
                    }
                    else
                    {
                        sw.Start();
                        result.Img2Hash = img.Processing(Mode.CropAHash);
                        sw.Stop();
                        result.TimeHash += sw.ElapsedMilliseconds;

                        sw.Start();
                        result.Img2PHash = img.Processing(Mode.CropPHash);
                        sw.Stop();
                        result.TimePHash += sw.ElapsedMilliseconds;

                        sw.Start();
                        result.Img2Magic = img.Processing(Mode.CropDHash);
                        sw.Stop();
                        result.TimeMagic += sw.ElapsedMilliseconds;
                    }
                }

                var file = new FileInfo(filePath);
                file.Delete();
            }

            return(View("Results", result));
        }