java - Resize Image to defined Dimension and paint unused area red? -
the following happens server side. want scale image following.
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:
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
have @ 2d graphics , writing/saving image more details
Comments
Post a Comment