public ArrayList <BufferedImage> splitImage(BufferedImage originalImage, int size) { ArrayList <BufferedImage> imgArray = new ArrayList <BufferedImage>(); for (int x = 0; x < originalImage.getWidth(); x += size) { for (int y = 0; y < originalImage.getHeight(); y += size) { imgArray.Add(originalImage.getSubimage(x, y, size, size)); } } return(imgArray); }
/// <summary> /// Crop the part of an image with a white rectangle /// </summary> /// <returns> A cropped image File </returns> public virtual File crop(BufferedImage image) { // this will be coordinates of the upper left white pixel int upperLeftCornerx = int.MaxValue; int upperLeftCornery = int.MaxValue; //this will be coordinates of the lower right white pixel int lowerRightCornerx = int.MinValue; int lowerRightCornery = int.MinValue; //find the minimum and maximum white pixel coordinates for (int i = 0; i < image.Width; i++) { for (int j = 0; j < image.Height; j++) { if (image.getRGB(i, j) == WHITE.RGB && (i < upperLeftCornerx && j < upperLeftCornery) || (i <= upperLeftCornerx && j < upperLeftCornery) || (i < upperLeftCornerx && j <= upperLeftCornery)) { upperLeftCornerx = i; upperLeftCornery = j; } if (image.getRGB(i, j) == WHITE.RGB && ((i > lowerRightCornerx && j >= lowerRightCornery) || (i >= lowerRightCornerx && j > lowerRightCornery) || (i > lowerRightCornerx && j >= lowerRightCornery))) { lowerRightCornerx = i; lowerRightCornery = j; } } } //crop the image to the white rectangle size BufferedImage croppedImage = image.getSubimage(upperLeftCornerx, upperLeftCornery, lowerRightCornerx - upperLeftCornerx, lowerRightCornery - upperLeftCornery); //make a file from that cropped image File cropFile = new File("croppedimage.png"); try { ImageIO.write(croppedImage, "png", cropFile); } catch (IOException ex) { //JAVA TO C# CONVERTER WARNING: The .NET Type.FullName property will not always yield results identical to the Java Class.getName method: Logger.getLogger(typeof(OcrDemo).FullName).log(Level.SEVERE, null, ex); } return(cropFile); }
/// <summary> /// Crops (returns subimage) of specified input image at specified points. /// </summary> /// <param name="image"> image to crop </param> /// <param name="x1"> top left x coordinate </param> /// <param name="y1"> top left y coordinate </param> /// <param name="x2"> bottom right x coordinate </param> /// <param name="y2"> bottom right y coordinate /// </param> /// <returns> image croped at specified points </returns> public static BufferedImage cropImage(BufferedImage image, int x1, int y1, int x2, int y2) { return(image.getSubimage(x1, y1, x2 - x1, y2 - y1)); }