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