Use JAVA to implement Digital Watermarks (visible) and java digital watermarks

Source: Internet
Author: User

Use JAVA to implement Digital Watermarks (visible) and java digital watermarks

Digital Watermarks can be visible and invisible. For example, the courseware is printed with the school logo, and Weibo pictures can be printed with the information of the Uploader and Weibo logo.

You can use java to implement visible digital watermarks. Examples mainly use the AlphaComposite class in the java. awt package. Of course, before implementation, we will first introduce the AlphaComposite class:

The AlphaComposite class is a hybrid processing class with overlapping targets. The specific rule for such implementation is T. porter and T. "Compositing Digital Images" co-authored by Duff, the 12 basic rule sets described in SIGGRAPH 84,253-259. This class provides a getInstance method. The two parameters are rule and alpha. The second parameter sets an alpha value by the caller, that is, the transparency setting, the first parameter is a hybrid 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, which can be used to modify the opacity and the coverage rate of each source pixel before being used in a hybrid equation.

Porter and Duff use the following factors in the description of mixed equations:

  

Taking rule SRC_OVER as an example, using these factors, Porter and Duff define 12 methods to select the mixed factor Fs and Fd, resulting in 12 satisfactory visual effects. In the description of 12 static fields with specified visual effects, an equation with fixed Fs and Fd values is given. SRC_OVER synthesize the Source color above the target color (Porter-Duff Source Over Destination rule ). Specify Fs = 1 and Fd = (1-As), so:

Ar = As + Ad * (1-As) Cr = Cs + Cd * (1-As) the extension class has a total of 24 rules and nine methods are defined, since the getInstance () method is used in the pipeline program, Let's explain it -- • detailed definition: public static AlphaComposite getInstance (int rule, float alpha) • function: Create an AlphaComposite object, it has a specified rule and a constant alpha value used to multiply the alpha value of the source color. Before merging the source and target colors, multiply the source colors by the specified alpha value. • Parameter: rule -- Merging rule, 24 types; alpha -- constant alpha value of the Multiplication Source color. Alpha must be a floating point number in the range [0.0, 1.0] (including the boundary value. • Throw: IllegalArgumentException-If alpha is smaller 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 recommendations AlphaCompositehttp class documents: http://download.oracle.com/technetwork/java/javase/6/docs/zh/api/java/awt/AlphaComposite.html#hashCode (), Compositing Digital Images paper: Since the https://en.wikipedia.org/wiki/Alpha_compositing is image processing, first create a java2d object Graphics2D g2d = image. createGraphics (); // fill the background g2d with the source image. drawImage (image, 0, 0, image. getWidth (), image. getHeight (), null, null );View Code

After Composite is set for Graphics2D context, you can write the text or image you want to write to the source image.

  

AlphaComposite ac = AlphaComposite. getInstance (AlphaComposite. SRC_OVER, alpha); // set Composite for Graphics2D context. Composite is used in all painting 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 (the comments in the Code are enough, so there is not much to worry about ):

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 the watermark text on the source image */public void WordsToImage (String srcImagePath, float alpha, string font, int fontStyle, int fontSize, Color color, String inputWords, int x, int y, String imageFormat, String toPath) throws IOException {FileOutputStream Fos = null; try {// read the image BufferedImage image = ImageIO. read (new File (srcImagePath); // create the java2D object Graphics2D g2d = image. createGraphics (); // fill the background g2d with the source image. drawImage (image, 0, 0, image. getWidth (), image. getHeight (), null, null );//!!!! AlphaComposite ac = AlphaComposite. getInstance (AlphaComposite. SRC_OVER, alpha); // set Composite for Graphics2D context. Composite is used in all painting 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 the text font name, style, and size g2d. setFont (new Font (font, fontStyle, fontSize); g2d. setColor (color); // set the font color g2d. drawString (inputWords, x, y); // enter the watermark text and its starting x and y coordinates g2d. dispose (); // write the watermark image to the toPath fos = new FileOutputStream (toPath); ImageIO. write (image, imageFormat, fos);} // catch (Exception e) {e. printStackTrace ();} finally {if (fos! = Null) {fos. close () ;}}/ *** set the image watermark on the source image */public void ImageToImage (String srcImagePath, String appendImagePath, float alpha, int x, int y, int width, int height, String imageFormat, String toPath) throws IOException {FileOutputStream fos = null; try {// read the image BufferedImage image = ImageIO. read (new File (srcImagePath); // create the java2D object Graphics2D g2d = image. createGraphics (); // fill the background g2d with the source image. drawImage (image, 0, 0, image. getWidth (), image. getHeight (), null, null); // key point: 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 (effecti On e) {e. printStackTrace ();} finally {if (fos! = Null) {fos. close () ;}} public static void main (String [] args) throws Exception {Watermark2 imageObj = new Watermark2 (); // source image path String srcImagePath = "F: /27.jpg"; // watermark image path String appendImagePath = "F:/logo.jpg"; // ---- 77 77 77 77 77 77 77 red transparency 0.4 "float alpha = 0.4F; string font = ""; int fontStyle = Font. PLAIN; int fontSize = 77; Color color = Color. RED; String inputWords = "set watermark text on the image"; int x = 1700; int y = 77; String imageFormat = "jpg "; // storage path after the watermark text String wToPath = "F:/31.png"; // storage path after the watermark image 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, 300,200, imageFormat, IToPath );}}View Code

Implement preview:

  

For more references, Java2d document: http://www.apihome.cn/api/java/Graphics2D.html

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.