FocalPointModifier { public BufferedImage apply(BufferedImage img, ImageManipulationParameters parameters) { FocalPoint focalPoint = parameters.getFocalPoint(); if (focalPoint == null) { return img; } if (parameters.getFocalPointDebug()) { drawFocusPointAxis(img, focalPoint); } Point imageSize = new Point(img.getWidth(), img.getHeight()); Point absFocalPoint = parameters.getFocalPoint().convertToAbsolutePoint(imageSize); if (!absFocalPoint.isWithinBoundsOf(imageSize)) { throw error(BAD_REQUEST, "image_error_focalpoint_out_of_bounds", focalPoint.toString(), imageSize.toString()); } Point targetSize = parameters.getSize(); Float zoomFactor = parameters.getFocalPointZoom(); BufferedImage zoomedImg = applyZoom(img, zoomFactor, focalPoint, targetSize); if (zoomedImg == null) { if (targetSize == null) { throw error(BAD_REQUEST, "image_error_focalpoint_target_missing"); } Point newSize = calculateResize(imageSize, targetSize); img = applyResize(img, newSize); if (imageSize.equals(targetSize)) { return img; } boolean alignX = calculateAlignment(imageSize, targetSize); Point cropStart = calculateCropStart(alignX, targetSize, newSize, focalPoint); if (cropStart != null) { img = applyCrop(img, cropStart, targetSize); } } else { img = zoomedImg; } img.flush(); return img; } FocalPointModifier(ImageManipulatorOptions options); BufferedImage apply(BufferedImage img, ImageManipulationParameters parameters);  }
@Test public void testCropViaFocalPoint() { BufferedImage bi = new BufferedImage(100, 200, BufferedImage.TYPE_INT_ARGB); BufferedImage outputImage = new FocalPointModifier(new ImageManipulatorOptions()).apply(bi, new ImageManipulationParametersImpl().setFocalPoint(0.1f, 0.1f).setCropMode(FOCALPOINT).setSize(50, 50)); assertEquals(50, outputImage.getWidth()); assertEquals(50, outputImage.getHeight()); }