List of utility methods to do BufferedImage Crop
BufferedImage | crop(BufferedImage src, Rectangle rect) Crops an image to a given region. BufferedImage dest = new BufferedImage((int) rect.getWidth(), (int) rect.getHeight(), src.getType()); Graphics g = dest.getGraphics(); g.drawImage(src, 0, 0, (int) rect.getWidth(), (int) rect.getHeight(), (int) rect.getX(), (int) rect.getY(), (int) (rect.getX() + rect.getWidth()), (int) (rect.getY() + rect.getHeight()), null); g.dispose(); return dest; |
Raster | crop(final Raster src, final long tx, final long ty, final long minTx, final long maxTy, final int tilesize) crop final int dtx = (int) (tx - minTx); final int dty = (int) (maxTy - ty); final int x = dtx * tilesize; final int y = dty * tilesize; final WritableRaster cropped = src.createCompatibleWritableRaster(tilesize, tilesize); cropped.setDataElements(0, 0, tilesize, tilesize, src.getDataElements(x, y, tilesize, tilesize, null)); return cropped; |
void | crop4Square(BufferedImage source, File to, int size) crop Square try { String mimeType = "image/jpeg"; if (to.getName().endsWith(".png")) { mimeType = "image/png"; if (to.getName().endsWith(".gif")) { mimeType = "image/gif"; int width = source.getWidth(); int height = source.getHeight(); Image croppedImage; if (width == height) { croppedImage = source.getScaledInstance(size, size, Image.SCALE_SMOOTH); } else { int left, top; if (width > height) { left = (width - height) / 2; top = 0; croppedImage = source.getSubimage(left, top, height, height); } else { left = 0; top = (height - width) / 2; croppedImage = source.getSubimage(left, top, width, width); croppedImage = croppedImage.getScaledInstance(size, size, Image.SCALE_SMOOTH); BufferedImage dest = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB); Graphics graphics = dest.getGraphics(); graphics.setColor(Color.WHITE); graphics.fillRect(0, 0, size, size); graphics.drawImage(croppedImage, 0, 0, null); ImageWriter writer = ImageIO.getImageWritersByMIMEType(mimeType).next(); writer.setOutput(new FileImageOutputStream(to)); IIOImage image = new IIOImage(dest, null, null); writer.write(null, image, createImageWriteParam(writer)); } catch (Exception e) { throw new RuntimeException(e); |
BufferedImage | cropAndScaleImage(BufferedImage image, int cropX, int cropY, int cropW, int cropH, int scaleW, int scaleH) crop And Scale Image BufferedImage bimage = new BufferedImage(scaleW, scaleH, image.getType()); Graphics2D bGr = bimage.createGraphics(); bGr.drawImage(image, 0, 0, scaleW - 1, scaleH - 1, cropX, cropY, cropX + cropW - 1, cropY + cropH - 1, null); bGr.dispose(); return bimage; |
BufferedImage | cropBottomTransparent(BufferedImage img) crop Bottom Transparent int maxY = img.getHeight(); boolean isTransparent = true; while (isTransparent) { maxY--; if (maxY == 0) break; for (int x = 0; x < img.getWidth() && isTransparent; x++) { if (img.getRGB(x, maxY) != 0) ... |
Rectangle | cropBoundingBox(Rectangle r, int width, int height) crop Bounding Box if (r.x < 0) r.x = 0; if (r.y < 0) r.y = 0; if ((r.x + r.width) > width) r.width = width - r.x; if ((r.y + r.height) > height) r.height = height - r.y; ... |
BufferedImage | cropImage(BufferedImage image, int cropWidth, int cropHeight) Method crop an image to the given dimensions. BufferedImage retImg = null; int width = 0; int height = 0; width = image.getWidth(); height = image.getHeight(); retImg = new BufferedImage(cropWidth, cropHeight, BufferedImage.TYPE_INT_RGB); for (int i = 0; i < cropWidth; i++) { for (int j = 0; j < cropHeight; j++) { ... |
BufferedImage | cropImage(BufferedImage image, int fromX, int fromY, int width, int height) crop Image assert (width > 0 && height > 0); BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = img.getGraphics(); g.drawImage(image, fromX, fromY, img.getWidth(), img.getHeight(), null); return img; |
BufferedImage | cropImage(BufferedImage image, int lc, int rc, int tc, int bc) crop Image BufferedImage dest = new BufferedImage(image.getWidth() - (lc + rc), image.getHeight() - (tc + bc), BufferedImage.TYPE_BYTE_GRAY); Graphics g = dest.getGraphics(); g.drawImage(image, 0, 0, image.getWidth() - (lc + rc), image.getHeight() - (tc + bc), lc, tc, image.getWidth() - rc, image.getHeight() - bc, null); g.dispose(); return dest; |
BufferedImage | cropImage(BufferedImage image, int width, int height) Crops the image to the given size starting at (0,0) if (image.getWidth() == width && image.getHeight() == height) { return image; return image.getSubimage(0, 0, width, height); |