Generate thumbnail and fill empty space with color

1.3k Views Asked by At

Is it possible to implement the first example with Scalr?

My code is the following:

BufferedImage thumbnail = Scalr.resize(ImageIO.read(sourceFile), Scalr.Method.ULTRA_QUALITY, Scalr.Mode.FIT_TO_WIDTH,
                width, height, Scalr.OP_ANTIALIAS);
ImageIO.write(thumbnail, destinationfile.getExtension(), destinationfile);

What I want is to receive the image like this: enter image description here where the blue bars are the space I want to fill with the color.

Thank you

Update: maybe it is possible to implement with Thumbnailator?

2

There are 2 best solutions below

0
On

Just done! Perhaps it can help you!

public static BufferedImage resizeAndCrop(BufferedImage bufferedImage) throws IOException {

        int himg = bufferedImage.getHeight();
        int wimg = bufferedImage.getWidth();

        double rateh = himg/dim;
        double ratew = wimg/dim;
        double rate = ratew;
        if(rateh>ratew)
        rate = rateh;
        
        int dimhimg = (int) (himg/rate);
        int dimwimg = (int) (wimg/rate);
        
        double startw = dim/2 - dimwimg/2;
        double starth = dim/2 - dimhimg/2;
        
        BufferedImage tThumbImage = new BufferedImage( dim, dim, BufferedImage.TYPE_INT_RGB );
        Graphics2D tGraphics2D = tThumbImage.createGraphics(); //create a graphics object to paint to
        tGraphics2D.setBackground( Color.WHITE );
        tGraphics2D.setPaint( Color.WHITE );
        tGraphics2D.fillRect( 0, 0, dim, dim );
        tGraphics2D.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        tGraphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,  RenderingHints.VALUE_ANTIALIAS_ON);
        tGraphics2D.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING,  RenderingHints.VALUE_COLOR_RENDER_QUALITY); 
        tGraphics2D.drawImage( bufferedImage, (int)startw, (int)starth, dimwimg, dimhimg, null ); //draw the image scaled

        File ff = new File(path + "jdata/tmp/prova.jpg");
        ImageIO.write( tThumbImage, "JPG", ff); //write the image to a file
        BufferedImage croppedContainMethod = ImageIO.read(ff);
        return croppedContainMethod; 
}
0
On

Nobody has idea so I will publish my solution... I decided to continue to use Scalr (I didn't checked the Thumbnailator's last version but the previous ones failed on big pictures). So first of all I call resize method, and then, if sizes of the new thumbnail are bigger then given ones I call crop method that crops a thumbnail by the center.. The code is the following:

BufferedImage thumbnail = Scalr.resize(sourceFile, Scalr.Method.ULTRA_QUALITY, Scalr.Mode.AUTOMATIC, destinationSize.width, destinationSize.height);
if (thumbnail.getWidth() > destinationSize.width)
    thumbnail = Scalr.crop(thumbnail, (thumbnail.getWidth() - destinationSize.width) / 2, 0, destinationSize.width, destinationSize.height);
else if (thumbnail.getHeight() > destinationSize.height) 
    thumbnail = Scalr.crop(thumbnail, 0, (thumbnail.getHeight() - destinationSize.height) / 2, destinationSize.width, destinationSize.height);

It is not ideal, but at least it handles 'wide' images after generation of thumbnails