Skip to content

sigpwned/smartcrop4j

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

smartcrop4j tests Maven Central Version

Content-aware image cropping for Java 11+ based on the excellent @jwagner/smartcrop.js.

Functionality

Example Crop Image: https://www.flickr.com/photos/endogamia/5682480447/ by N. Feans, via @jwagner/smartcrop.js.

Given a BufferedImage, smartcrop4j considers the following heuristics to recommend an "interesting" crop with a given aspect ratio from the original BufferedImage:

  • Edge detection
  • Regions of high saturation
  • Regions rich in skin-like colors
  • Composition best practices, e.g., Rule of thirds

Quickstart

To produce a crop of an image of a given width and height with the default configuration, simply use:

BufferedImage croppedImage=Smartcrop.crop(originalImage, cropWidth, cropHeight);

This will cause the system to choose a crop of the appropriate aspect ratio and then copy the result to a new image, possibly scaling the cropped area up or down depending on the image and crop sizes.

To load an image from a file, crop it, and store the result in a new file with the same image type, all using the default configuration, use:

File croppedFile=Smartcrop.crop(imageFile, cropWidth, cropHeight);
try {
    // Do some work...
}
finally {
    croppedFile.delete();
}

To boost the importance of an area of the image for the purposes of crop selection, for example to incorporate information from face detection or object labeling, use:

BufferedImage croppedImage=Smartcrop.crop(originalImage, cropWidth, cropHeight, List.of(
    new CropBoost(firstBoostX, firstBoostY, firstBoostWidth, firstBoostHeight, 1.0f),
    new CropBoost(secondBoostX, secondBoostY, secondBoostWidth, secondBoostHeight, 0.5f)));