Digital watermark is visible, visible, such as the school emblem printed on the courseware, micro-bo hair pictures feasted printed on the information and Weibo logo.
Using Java to achieve a visible digital watermark, grass is mainly used in the java.awt package Alphacomposite class, of course, before the implementation of the Alphacomposite class:
The Alphacomposite class is a mixed-processing class about two targets overlapping, and the specific rules for implementation of this class are the "compositing Digital Images" of Porter and T. Duff, described in SIGGRAPH 84, 253-259. 12 basic rule sets. The class provides a getinstance method in which two parameters are rule and alpha, and the second parameter sets an alpha value for the caller, which is the setting for transparency, and the first parameter is a blending method. This class extends the equations defined by Porter and Duff, and contains an additional factor. An instance of the Alphacomposite class can contain an alpha value that can be used to modify the opacity and coverage of each source pixel before using the value in a mixed equation.
The porter and Duff papers use the following factors in the description of the mixed equation:
Taking rule src_over as an example, using these factors, Porter and Duff define 12 kinds of methods to select the mixed factor Fs and Fd, thus producing 12 kinds of satisfying visual effects. In the description of 12 static fields that specify visual effects, an equation with the value of Fs and Fd is given. Src_over the source color (porter-duff source over destination rule) above the target color. Specify FS = 1 and Fd = (1-as), so:
Ar = as + ad* (1-as)
Cr = Cs + cd* (1-as)
This class expands after a total of 24 rules, defines 9 methods, because the straw man program uses the method getinstance () to explain
• Detailed definition: public static alphacomposite getinstance (int rule, float alpha)
• Function: Creates a Alphacomposite object that has the specified rule and constant alpha value used to multiply the source color alpha value. The source color is multiplied by the specified alpha value before the source color is synthesized with the target color.
• Parameters: rule--synthesis rules, 24 kinds; alpha--will multiply the constant alpha value of the alpha value of the source color. Alpha must be a floating-point number within the range [0.0, 1.0] (containing the boundary value).
• Thrown: illegalargumentexception-if alpha is less than 0.0 or greater than 1.0, or rule is one of the following: Clear, SRC, DST, Src_over, Dst_over, src_in, dst_in, Src_out, Dst_out, Src_atop, Dst_atop, or XOR.
Since it is image processing, create a Java2d object first
Copy Code code as follows:
Graphics2D G2d=image.creategraphics ();
Populating backgrounds with source images
G2d.drawimage (image, 0, 0, image.getwidth (), image.getheight (), NULL, NULL);
Then you can write the text or picture you want to write to the source picture after you set the composite for the graphics2d context
Alphacomposite ac = alphacomposite.getinstance (alphacomposite.src_over, Alpha);
Sets the composite for the graphics2d context. Composite is used in all drawing methods, such as DrawImage,
DrawString, draw and fill. It specifies how new pixels are combined with existing pixels on the graphics device during rendering.
G2d.setcomposite (AC);
Complete code (comment in code is enough, not verbose)
Package cumt.zry.two;
Import java.awt.*;
Import Java.awt.image.BufferedImage;
Import java.io.*;
Import javax.imageio.*;
public class watermark2{Public Watermark2 () {super ();}; /** * Set watermark text on the source picture/public void Wordstoimage (string srcimagepath,float Alpha, string Font,int fontstyle,int fon Tsize,color Color, String inputwords,int x,int y,string imageformat,string topath) throws ioexception{fileoutputst
Ream Fos=null;
try {//Read the picture bufferedimage image = Imageio.read (new File (Srcimagepath));
Create java2d Object graphics2d g2d=image.creategraphics ();
Fills the background g2d.drawimage with the source image (image, 0, 0, image.getwidth (), image.getheight (), NULL, NULL);
//!!!!
Alphacomposite ac = alphacomposite.getinstance (alphacomposite.src_over, Alpha); Sets the composite for the graphics2d context. Composite is used in all drawing methods, such as DrawImage,//drawstring, Draw, and fill.
It specifies how new pixels are combined with existing pixels on the graphics device during rendering.
G2d.setcomposite (AC); Set text font name, style, size g2d.setfont (new font,FontStyle, FontSize)); G2d.setcolor (color);//Set Font color g2d.drawstring (inputwords, x, y);
Input watermark text and its starting x, Y coordinate g2d.dispose ();
Writes the image after the watermark to the Topath path fos=new fileoutputstream (Topath);
Imageio.write (image, ImageFormat, FOS);
//File operation error throws catch (Exception e) {e.printstacktrace ();
}finally{if (fos!=null) {fos.close (); }}/** * Set a picture watermark on the source image/public void Imagetoimage (String srcimagepath,string Appendimagepath, Floa T alpha,int x,int y,int width,int height, String imageformat,string topath) throws ioexception{FileOutputStream F
OS = null;
try {//read graph BufferedImage image = Imageio.read (new File (Srcimagepath));
Create java2d Object graphics2d g2d=image.creategraphics ();
Fills the background g2d.drawimage with the source image (image, 0, 0, image.getwidth (), image.getheight (), NULL, NULL);
Key place Alphacomposite AC = alphacomposite.getinstance (alphacomposite.src_over, Alpha);
G2d.setcomposite (AC); BufferedimaGE appendimage = imageio.read (new File (Appendimagepath));
G2d.drawimage (Appendimage, x, y, width, height, null, NULL);
G2d.dispose ();
Fos=new FileOutputStream (Topath);
Imageio.write (image, ImageFormat, FOS);
catch (Exception e) {e.printstacktrace ();
}finally{if (fos!=null) {fos.close ();
}} public static void Main (string[] args) throws Exception {Watermark2 imageobj = new Watermark2 ();
Source picture path String Srcimagepath = "F:/27.jpg";
Watermark Picture path String Appendimagepath = "F:/logo.jpg";
----XXFarEastFont-Arial Common font 77th word red transparency 0.4 "float alpha = 0.4F;
String font = "Song Body";
int fontstyle = Font.plain;
int fontsize = 77;
Color color = color.red;
String inputwords = "Set watermark text on picture";
int x = 1700;
int y = 77;
String imageformat = "jpg";
Storage path after watermark text String wtopath = "F:/31.png";
Watermark image after the storage path String Itopath = "F:/7.png"; Imageobj.wordstoimage (Srcimagepath, alpha, Font, FontStyle, fontsize, color, Inputwords, x, Y, ImageFormat, Wtopath);
Imageobj.imagetoimage (Srcimagepath, Appendimagepath, alpha, x, y, MB, imageformat, Itopath);
}
}
To implement a preview:
The above is the entire content of the article, I hope to help you learn.