public CropConfiguration( AspectRatio aspectRatio, CropStrategy cropStrategy, Option <Color> backgroundColor) { AspectRatio = aspectRatio; CropStrategy = cropStrategy; BackgroundColor = backgroundColor; }
public void Initialize() { switch (function) { case "Resize": //this.strategy = new ResizedStrategy(this.x, this.y, this.width, this.height); ResizeKARStrategy rs = new ResizeKARStrategy(this.width, this.height); rs.Edit(this.sourcePath, this.destinationPath); Console.WriteLine("Done!"); Console.WriteLine(); break; case "PNG": ConvertPNGStrategy cs1 = new ConvertPNGStrategy(); cs1.Edit(this.sourcePath, this.destinationPath); Console.WriteLine("Done!"); Console.WriteLine(); break; case "JPEG": ConvertJPGStrategy cs2 = new ConvertJPGStrategy(); cs2.Edit(this.sourcePath, this.destinationPath); Console.WriteLine("Done!"); Console.WriteLine(); break; case "GIF": ConvertGIFStrategy cs3 = new ConvertGIFStrategy(); cs3.Edit(this.sourcePath, this.destinationPath); Console.WriteLine("Done!"); Console.WriteLine(); break; case "Skew": SkewStrategy ss = new SkewStrategy(this.x1, this.x2, this.x3, this.x4, this.y1, this.y2, this.y3, this.y4); ss.Edit(this.sourcePath, this.destinationPath); Console.WriteLine("Done!"); Console.WriteLine(); break; case "Crop": CropStrategy crs = new CropStrategy(this.width, this.height); crs.Edit(this.sourcePath, this.destinationPath); Console.WriteLine("Done!"); Console.WriteLine(); break; } }
public Rectangle FindCropRectangle(Bitmap bmp, CropStrategy CropStrategy, string txt = "") { var imageRawData = new Dictionary <int, List <double> >(); int width = bmp.Width; int height = bmp.Height; //read in bitmpa data row by row into imageRawData var leftMostIndex = 0; var rightMostIndex = 0; for (int i = 0; i < height; i++) { var rowRawData = new List <double>(); for (int j = 0; j < width; j++) { var pixel = bmp.GetPixel(j, i); var tmp = (pixel.R == 255) ? 0 : 1; if (tmp == 1) { if (leftMostIndex > j || leftMostIndex == 0) { leftMostIndex = j; } if (rightMostIndex < j || rightMostIndex == 0) { rightMostIndex = j; } } rowRawData.Add(tmp); } imageRawData.Add(i, rowRawData); } //find the rectangle var darkRows = imageRawData.Where(x => x.Value.Contains(1)); var firstDarkRowIndex = darkRows.First().Key; var lastDarkRowIndex = darkRows.Last().Key; //rightMostIndex - leftMostIndex var letterWidth = rightMostIndex - leftMostIndex; var letterHeight = lastDarkRowIndex - firstDarkRowIndex; //the right index and left index is same, set the rectangle width to 1 letterWidth = letterWidth == 0 ? 1 : letterWidth + 1; letterHeight = letterHeight == 0 ? 1 : letterHeight + 1; //modify the leftMostIndex and firstDarkRowIndex leftMostIndex = leftMostIndex > 1 ? leftMostIndex : leftMostIndex - 1; firstDarkRowIndex = firstDarkRowIndex > 1 ? firstDarkRowIndex : firstDarkRowIndex - 1; Rectangle edgeRetangle = new Rectangle(); if (CropStrategy == CropStrategy.CropToEdge) { edgeRetangle = new Rectangle(leftMostIndex, firstDarkRowIndex, letterWidth, letterHeight); } else if (CropStrategy == CropStrategy.CropHeightOnly) { edgeRetangle = new Rectangle(0, firstDarkRowIndex, width, letterHeight); } else if (CropStrategy == CropStrategy.CropWidthOnly) { edgeRetangle = new Rectangle(leftMostIndex, 0, letterWidth, height); } return(edgeRetangle); }
public NormalizeDecorator(CropStrategy cropStrategy) { CropStrategy = cropStrategy; ImageProcessor = new ImageProcessor(); }