Combining two images in java?

In my project, users upload a picture. Sometimes it is less than expected.

I would like to create a default white (blank) image and place the user's image (small) in the center of the white image. In other words, I want to cover all sides of a small image with white pixels.

How can I do this using java?

I am not good at java image processing. But I use BuferedImageto expand the downloaded image.

BufferedImage newImage = new BufferedImage(50, 50,  BufferedImage.TYPE_INT_RGB);
newImage.getGraphics().drawImage(srcImage, 0, 0, srcImage.getWidth(), srcImage.getHeight(), null);

The code above fills in black if the width and height of the original image is less than 50. The original image is displayed on the left, and the remaining area is filled in black.

Is it possible to move the original image to the center and fill the remaining pixels with white.

Any suggestions would be greatly appreciated.

thank

+3
source share
2 answers

You tried newImage.getGraphics (). translate ((25-srcImage.getWidth () / 2), (25-srcImage.getHeight () / 2));

+1
source

You tried to increase the size of the image, try the link , they show you how.

Returning to your question, if you want to put the image in the center, well, you have to change drawImage (), the third and fourth parameters, set the location of the image if you have to calculate the center of the empty image minus the size of the original image and put it there .

0
source

Source: https://habr.com/ru/post/1791054/


All Articles