简体   繁体   中英

Combining two images in java?

In my project users uploads picture. Some times it is smaller than expected.

I'd like to create a default size white(empty) image and place users image(small) at 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 it with java?

I am not good in java image processing. But i use BuferedImage to expand uploaded image.

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

The above code, fills black color if the source image width and height is less than 50. The source image is rendered from left top and remaining area is filled by black.

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

Any suggestions will be appreciative.

Thanks

Did you try newImage.getGraphics().translate((25-srcImage.getWidth()/2), (25-srcImage.getHeight()/2)); ?

Have you tried increasing the size of the image, try in this link , they show you how.

Returning to your question, if you want to put the image in the center, well you should change the drawImage(), the third and fourth params, set the location of the image in wich case you must calculate the center of the blank image minus the size of the original image and put it there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM