Implementing a digital watermark in Java (visible)

Source: Internet
Author: User

Digital watermark has visible invisible points, such as the courseware printed on the school emblem, micro-bo hair pictures feasted printed on the uploader information and micro-blog logo.

Using Java to achieve the visible digital watermark, the grass is mainly used in the java.awt package of the Alphacomposite class, of course, before implementing the Alphacomposite class:

The Alphacomposite class is a mixed-processing class about two target overlaps, and the specific rules for implementing this class are T. Porter and T. Duff co-authored "Compositing Digital Images", SIGGRAPH 84, 253-259 described in 12 basic rule sets. The class provides a getinstance method in which two parameters are rule and alpha, the second parameter sets an alpha value by the caller, which is the setting for transparency, and the first argument is a blending method. This class extends the equations defined by Porter and Duff, and includes an additional factor. An instance of the Alphacomposite class can contain an alpha value that can be used to modify the opacity and the coverage of each source pixel before using the value for the mixed equation.

Porter and Duff's thesis uses 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 methods for selecting mixed-factor Fs and Fd, resulting in 12 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) After the class extension there are 24 rules, defined 9 methods, because the straw man program used in the Method getinstance () to explain the--detailed definition: public static alphacomposite getinstance (int rule, float Alpha) • Function: Creates a Alphacomposite object that has the specified rule and a constant alpha value that is used to multiply the alpha value of the source color.  Multiplies the source color by the specified alpha value before combining the source color with the target color. • Parameters: rule--synthesis rules, 24 kinds; alpha--The constant alpha value that will multiply 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 rules: CLEAR, SRC, DST, Src_over, Dst_over, src_in, dst_in,  Src_out, Dst_out, Src_atop, Dst_atop, or XOR. More detailed recommendation Alphacompositehttp class document: http://download.oracle.com/technetwork/java/javase/6/docs/zh/api/java/awt/ Alphacomposite.html#hashcode (), compositing Digital Images paper:https://en.wikipedia.org/wiki/Alpha_compositingsince it is image processing, create a Java2d object first 
Graphics2D g2d=image.creategraphics (); // fill the background with the source image NULL null);
View Code

Then, after setting composite for the graphics2d context, you can write the text or picture you want to write to the source image.

  

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,//            g2d.setcomposite (AC);

Complete code (comments are sufficient in the code, not much verbose):

 PackageCumt.zry.two;Importjava.awt.*;ImportJava.awt.image.BufferedImage;ImportJava.io.*;ImportJavax.imageio.*; Public classwatermark2{ PublicWatermark2 () {Super();};/*** Set watermark text on the source image*/     Public voidWordstoimage (String Srcimagepath,floatAlpha, String font,intFontStyle,intfontsize,color Color, String inputwords,intXintY,string imageformat,string Topath)throwsioexception{FileOutputStream Fos=NULL; Try {            //Reading PicturesBufferedImage image = Imageio.read (NewFile (Srcimagepath)); //Create a Java2d objectGraphics2D g2d=Image.creategraphics (); //fill the background with the source imageG2d.drawimage (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 the new pixel is combined with the existing pixels on the graphics device during rendering. G2d.setcomposite (AC); //Set text font name, style, sizeG2d.setfont (Newfont (font, FontStyle, fontSize)); G2d.setcolor (color);//Set Font ColorG2d.drawstring (inputwords, x, y);//Enter the watermark text and its starting x, y coordinatesG2d.dispose (); //writes the image after the watermark to the Topath pathfos=NewFileOutputStream (Topath);        Imageio.write (image, ImageFormat, FOS); }         //file operation error thrown        Catch(Exception e) {e.printstacktrace (); }finally{            if(fos!=NULL) {fos.close (); }        }    }         /*** Set picture watermark on the source image*/     Public voidimagetoimage (String srcimagepath,string Appendimagepath,floatAlphaintXintYintWidthintheight, String imageformat,string topath)throwsioexception{FileOutputStream Fos=NULL; Try {            //Read GraphBufferedImage image = Imageio.read (NewFile (Srcimagepath)); //Create a Java2d objectGraphics2D g2d=Image.creategraphics (); //fill the background with the source imageG2d.drawimage (image, 0, 0, image.getwidth (), Image.getheight (),NULL,NULL); //Key PlacesAlphacomposite AC =alphacomposite.getinstance (Alphacomposite.src_over, Alpha);                        G2d.setcomposite (AC); BufferedImage Appendimage= Imageio.read (NewFile (Appendimagepath)); G2d.drawimage (Appendimage, x, y, width, height,NULL,NULL);            G2d.dispose (); FOS=NewFileOutputStream (Topath);        Imageio.write (image, ImageFormat, FOS); } Catch(Exception e) {e.printstacktrace (); }finally{            if(fos!=NULL) {fos.close (); }        }    }         Public Static voidMain (string[] args)throwsException {Watermark2 imageobj=NewWatermark2 (); //Source Picture PathString Srcimagepath = "F:/27.jpg"; //Watermark Picture PathString Appendimagepath = "F:/logo.jpg"; //----Arial Plain font 77th-word red transparency 0.4 "        floatAlpha = 0.4F; String Font= "Song Body"; intFontStyle =Font.plain; intFontSize = 77; Color Color=color.red; String Inputwords= "Set watermark text on picture"; intx = 1700; inty = 77; String ImageFormat= "JPG"; //the storage path after the watermark textString Wtopath = "F:/31.png"; //the storage path after the watermark pictureString Itopath = "F:/7.png" ;  Imageobj.wordstoimage (Srcimagepath, alpha, Font, FontStyle, fontSize, Color, inputwords, x, Y, ImageFormat,         Wtopath); Imageobj.imagetoimage (Srcimagepath, Appendimagepath, alpha, X, Y,300, 200, ImageFormat, Itopath); }}
View Code

To implement a preview:

  

For more reference documentation, JAVA2D documentation: http://www.apihome.cn/api/java/Graphics2D.html

Implementing a digital watermark in Java (visible)

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.