Focus Points

A focus point is an image’s ideal vertical and horizontal center after cropping. You can specify focus points using the following techniques:

If you know that an image’s subject is always centered in the middle (as in time-lapse photography), you can return that coordinate in a Focus object. This technique ensures Brightspot generates crops around the image’s native center.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package content.images;

import com.psddev.cms.image.Focus;
import com.psddev.cms.image.FocusCalculator;
import com.psddev.dari.util.ObjectUtils;
import com.psddev.dari.util.StorageItem;

import java.util.Map;

public class CenterFocusCalculator implements FocusCalculator {

    @Override
    public Focus getFocus(StorageItem storageItem) {
        Map<String, Object> metaData = storageItem.getMetadata();
        double imageWidth = ObjectUtils.to(double.class, metaData.get("width"));
        double imageHeight = ObjectUtils.to(double.class, metaData.get("height"));
        Focus centerFocus = new Focus(imageWidth / 2, imageHeight / 2);

        System.out.println("Focus, x-coordinate " + centerFocus.getX());
        System.out.println("Focus, y-coordinate " + centerFocus.getY());
        return centerFocus;
    }

}

In the previous snippet—

  • Line 10 declares this ViewModel implements the FocusCalculator interface.
  • Lines 15–17 retrieve the image’s width and height from the metadata, divide each in half, and return both values in a Focus object.
  • Lines 19–20 print the focus point to the console.
Focus, x-coordinate 200.0
Focus, y-coordinate 131.0

See also: