All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.rtyley.android.screenshot.paparazzo.processors.ImageScaler Maven / Gradle / Ivy

The newest version!
package com.github.rtyley.android.screenshot.paparazzo.processors;

import com.github.rtyley.android.screenshot.paparazzo.processors.util.Dimensions;

import java.awt.image.BufferedImage;
import java.util.Map;

import static com.github.rtyley.android.screenshot.paparazzo.processors.util.Images.imageToBufferedImage;
import static com.github.rtyley.android.screenshot.paparazzo.processors.util.Images.scaleTo;

public class ImageScaler implements ScreenshotProcessor {

    private final ScreenshotProcessor delegate;
    private final Dimensions maxBounds;

    public ImageScaler(ScreenshotProcessor delegate, Dimensions maxBounds) {
        this.delegate = delegate;
        this.maxBounds = maxBounds;
    }

    @Override
    public void process(BufferedImage image, Map request) {
        delegate.process(ensureWithinBounds(image), request);
    }

    private BufferedImage ensureWithinBounds(BufferedImage image) {
        Dimensions imageDimensions = new Dimensions(image.getWidth(), image.getHeight());

        if (maxBounds.contains(imageDimensions)) {
            return image;
        }

        return imageToBufferedImage(scaleTo(imageDimensions.scaledPreservingAspectRatioToFitWithin(maxBounds), image));
    }

    @Override
    public void finish() {
        delegate.finish();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy