public static ImageDimensionResponse ExtractDimensions(FileInfo imageFileInfo) { var cvProvider = new CVProvider(); return(cvProvider.ExtractDimensions(imageFileInfo)); }
public static ProductImageDTO InsertProductImage(Guid ProductId, HttpPostedFile file, bool PrimaryImage) { string ImagePath = Properties.Settings.Default.ImagePath; var dbProductImage = new ProductImage(); var productImageDTO = new ProductImageDTO(); int categoryId = 0; ClassifierResponse classifierResponse = null; string ProductName = string.Empty; string imagePath = ""; string productProperties = "{}"; //Save image to disk if (file != null && file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); dbProductImage.Id = Guid.NewGuid(); dbProductImage.Inserted = DateTime.Now; dbProductImage.SelectedForExport = true; var path = ImagePath + dbProductImage.Id + "_" + fileName; var inp = file.InputStream; var savedProduct = GetProduct(ProductId); SaveOriginalImage(file, ImagePath, savedProduct.Id); //Save with number var cvProvider = new CVProvider(); cvProvider.SaveImageWithProductNumber(new FileInfo(path), savedProduct.ProductNumber, inp); dbProductImage.ProductId = ProductId; dbProductImage.PrimaryImage = PrimaryImage; imagePath = "Images/" + Path.GetFileName(path); dbProductImage.Path = imagePath; //Classify image if not classified var prevCategory = savedProduct.Category; if (prevCategory == null) { var classifierProvider = new ClassifierProvider(); classifierResponse = classifierProvider.ClassifyImage(path); dbProductImage.Category = classifierResponse.Category; dbProductImage.ClassifierPropability = classifierResponse.CategoryPorpability; //ToDo: Check i low propability } else { dbProductImage.Category = prevCategory; dbProductImage.ClassifierPropability = 0; } using (var db = new DitatEntities()) { db.ProductImages.Add(dbProductImage); db.SaveChanges(); productImageDTO.Category = dbProductImage.Category; switch (dbProductImage.Category) { case "DVD": categoryId = 2; var eanAttributeProvider = new EANAttributeProvider(); EANInfo eanInfo = eanAttributeProvider.GetEANInfo(path); productProperties = "[]"; //Only name is provided from ean-search.org ProductName = eanInfo.name; productImageDTO.ProductProperties = productProperties; productImageDTO.Name = ProductName; productImageDTO.ErrorMessage = eanInfo.ErrorMessage; /* * productProperties = @"[{""Property"":""Artist"",""Value"":""" + "Test" + @"""},{""Property"":""År"",""Value"":""" + "1234" + @"""}]"; * ProductName = dbProductImage.Category + " - " + (dbProductImage.ClassifierPropability * 100).ToString() + "%, ej klart"; * productImageDTO.ProductProperties = productProperties; * productImageDTO.Name = ProductName; */ break; case "CD": string format = string.Empty; string code = string.Empty; categoryId = 3; var cdAttributeProvider = new CDAttributeProvider(); CDInfo cdInfo = cdAttributeProvider.GetCDInfo(path); productProperties = cdInfo.ProductProperties; ProductName = cdInfo.Title; productImageDTO.ProductProperties = productProperties; productImageDTO.Name = ProductName; productImageDTO.ErrorMessage = cdInfo.ErrorMessage; break; default: if (savedProduct.Name == "") { ProductName = dbProductImage.Category + " - " + (dbProductImage.ClassifierPropability * 100).ToString() + "%, ej klart"; } break; } if (dbProductImage.Category == "Bok" && (savedProduct.Name == "Bok" || savedProduct.Name == "")) { categoryId = 1; //Bok var bookAttributeProvider = new BookAttributeProvider(); var bookInfo = bookAttributeProvider.GetBookInfo(path); productProperties = bookInfo.ProductProperties; ProductName = bookInfo.Title; productImageDTO.ProductProperties = productProperties; productImageDTO.Name = bookInfo.Title; productImageDTO.ErrorMessage = bookInfo.ErrorMessage; } if (dbProductImage.Category == "Kruka") { categoryId = 7; ProductName = "Kruka"; productProperties = @"[{ ""Key"":""Width"",""Value"":"""",""Type"":""number"",""Icon"":""panorama_horizontal""},{ ""Key"":""Height"",""Value"":"""",""Type"":""number"",""Icon"":""panorama_vertical""}]"; var imageDimensionResponse = ExtractDimensions(new FileInfo(path)); //Save processed images if they exists if (imageDimensionResponse.ImageWithDimensions != null) { var dbProductImageWithDimensions = new ProductImage(); dbProductImageWithDimensions.Id = Guid.NewGuid(); path = ImagePath + dbProductImageWithDimensions.Id + "_" + fileName; imageDimensionResponse.ImageWithDimensions.Save(path); dbProductImageWithDimensions.ProductId = ProductId; dbProductImageWithDimensions.PrimaryImage = false; dbProductImageWithDimensions.Path = "Images/" + Path.GetFileName(path); dbProductImageWithDimensions.ProductId = ProductId; dbProductImageWithDimensions.PrimaryImage = false; dbProductImageWithDimensions.SelectedForExport = true; db.ProductImages.Add(dbProductImageWithDimensions); db.SaveChanges(); var dbProductImageCropped = new ProductImage(); dbProductImageCropped.Id = Guid.NewGuid(); path = ImagePath + dbProductImageCropped.Id + "_crop" + fileName; //imageDimensionResponse.CroppedImage.Save(path); //Save with number cvProvider.SaveImageWithProductNumber(new FileInfo(path), savedProduct.ProductNumber, imageDimensionResponse.CroppedImage); dbProductImageCropped.ProductId = ProductId; dbProductImageCropped.PrimaryImage = true; dbProductImageCropped.Path = "Images/" + Path.GetFileName(path); dbProductImageCropped.ProductId = ProductId; dbProductImageCropped.PrimaryImage = false; dbProductImageCropped.SelectedForExport = true; db.ProductImages.Add(dbProductImageCropped); db.SaveChanges(); //Todo: Set Primary image SetPrimaryImage(dbProductImageCropped.Id); //Return cropped image imagePath = dbProductImageCropped.Path; Decimal width = Math.Round((Decimal)imageDimensionResponse.Width); Decimal height = Math.Round((Decimal)imageDimensionResponse.Height); //productProperties = @"[{""Property"":""Höjd"",""Value"":""" + height.ToString() + @" cm""},{""Property"":""Bredd"",""Value"":""" + width.ToString() + @" cm""}]"; //Höjd: "panorama_vertical" Bredd: "panorama_horizontal" productProperties = @"[{ ""Key"":""Height"",""Value"":""" + height.ToString() + @""",""Type"":""number"",""Icon"":""panorama_vertical""},{ ""Key"":""Width"",""Value"":""" + width.ToString() + @""",""Type"":""number"",""Icon"":""panorama_horizontal""}]"; } else { productImageDTO.ErrorMessage = imageDimensionResponse.ErrorMessage; } } } } //Update Product information using (var db = new DitatEntities()) { Product dbProduct = db.Products.Single(p => p.Id == ProductId); productImageDTO.Id = dbProductImage.Id; productImageDTO.Path = imagePath; //productImageDTO.PrimaryImage = dbProductImage.PrimaryImage; productImageDTO.ProductProperties = productProperties; productImageDTO.Name = ProductName; productImageDTO.Category = dbProduct.Category; if (ProductName != null || ProductName != "" || ProductName != String.Empty) { if (ProductName == null) { ProductName = ""; } if (ProductName.Length > 1) { dbProduct.Name = ProductName; if (!HasProperties(dbProduct.Properties)) { dbProduct.Properties = productProperties; } } } if (dbProductImage.Category != null) { dbProduct.Category = dbProductImage.Category;//classifierResponse.Category; } if (categoryId > 0) { dbProduct.CategoryId = categoryId; } //Fel: Skriver över properties om man lägger till en odefinierad bil till en befintlig bok: if (productImageDTO.ErrorMessage == null || dbProduct.Properties == null) { if (HasProperties(dbProduct.Properties) == false) { dbProduct.Properties = productProperties; } } db.SaveChanges(); } return(productImageDTO); }