Java padding image

2020-06-12 02:26发布

I am working on creating an online image editing tool.Looking for some refernce how can I add an image with white space on right side.For example see this image enter image description here

3条回答
劳资没心,怎么记你
2楼-- · 2020-06-12 03:16

Presumably, you want to create a new image from an existing image, where the new image has white space on the left and right?

Suppose the unpadded image was a BufferedImage and is called 'image'. Suppose the amount of whitespace you want on each side is 'w'. What you want to do is create a new BufferedImage wider than the original, then paint the entire thing white, and finally draw the smaller image on top of it:

BufferedImage newImage = new BufferedImage(image.getWidth() + 2 * w, image.getHeight(), image.getType());

Graphics g = newImage.getGraphics();

g.setColor(Color.white);
g.fillRect(0, 0, image.getWidth() + 2 * w, image.getHeight());
g.drawImage(image, w, 0, null);
g.dispose();
查看更多
做个烂人
3楼-- · 2020-06-12 03:16

Create a new BufferedImage object of the right size; use Graphics.fillRect() to paint it white; draw the image into the top-left corner with drawImage(); then save your new image.

查看更多
Anthone
4楼-- · 2020-06-12 03:17

If anyone comes upon a similar problem, I would definitively recommend imgScalr. You can add padding with literally one line imageSource= Scalr.pad(imageSource,pad,Color.White);.

查看更多
登录 后发表回答