Java Image Processing-merge Images

Source: Internet
Author: User

How to display the compositing effect of two images, such as stamping a file, requires that the image be transparent.

Assume that the file image is P1 and the seal image is P2.

Bufferedimage bi1 = NULL;
Int x = 10;
Int y = 10;
Bufferedimage bi2 = NULL;
Try {
Bi1 = javax. ImageIO. ImageIO. Read (new file ("p1.jpg "));
Bi2 = javax. ImageIO. ImageIO. Read (new file ("p2.png "));
} Catch (ioexception e ){
E. printstacktrace ();
}

Graphics G = bi1.getgraphics ();
G. drawimage (bi2, X, Y, null );

Fileoutputstream out = NULL; // output to the file stream

Try {
Out = new fileoutputstream ("p.jpg ");
Required imageencoder encoder = required codec. createjpegencoder (out );
Encoder. encode (bi1 );
Out. Close ();
} Catch (ioexception e ){
E. printstacktrace (); merged country
}-

P1 background image, P2 seal, and P are output images. P2 supports transparent images such as GIF and PNG.

X and Y are the coordinates of the seals placed in the background image (starting from the upper left corner ).

Set the drawing transparency in java2d:

Float alpha = 0.5f; // transparency
Graphics2d g2d = bufferedimage. creategraphics ();

// set transparency
g2d. setcomposite (alphacomposite. getinstance (
alphacomposite. src_atop, alpha); // start
// g2d. drawimage (); // drawing
g2d. setcomposite (alphacomposite. getinstance (
alphacomposite. src_over); // end

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.