java - Resize Image to defined Dimension and paint unused area red? -


the following happens server side. want scale image following.

enter image description here

the image should scaled fit in predefined dimension. image should scale fit in bounding rectangle. know how scale image java libs imagescalr. after scaling image should painted in target dimensions rect , places image not fill target rect should painted red shown in following image:

enter image description here

how can paint image target rectangle , fill areas no image red?

create bufferedimage bounding of box of desired area

// 100x100 desired bounding box of scaled area // change actual area want use bufferedimage scaledarea = new bufferedimage(100, 100, bufferedimage.type_int_rgb); 

using bufferedimage's graphics context, fill image desired color

graphics2d g2d = scaledarea.creategraphics(); g2d.setcolor(color.red); g2d.fillrect(0, 0, 100, 100); 

draw scaled image image...

// 100x100 desired bounding box of scaled area // change actual area want use int x = (100 - scaled.getwidth()) / 2; int y = (100 - scaled.getheight()) / 2; g2d.drawimage(scaled, x, y, null); g2d.dispose(); 

then use imageio.write save result

enter image description here

have @ 2d graphics , writing/saving image more details


Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -