Image watermarking information processing

First, the preparatory step

 1.1 Original: D: \\ \\ 9252150_092049419327_2.jpg File Table

 

1.2 logo picture: D: \\ \\ img \\ File Table biomass burning .png

 

 

 

Second, the source

   Appropriate adjustments according to their needs

  1  / * *
   2       * 
   3       * @param srcImgPath path of the original image
   . 4       * @param tarImgPath new image path    
   5       * @param Content1 watermarked content 1
   . 6       * @param logoImg logo image path
   . 7       * @param Content2 watermarked content 2
   . 8       * / 
  . 9      public   void Mark (String srcImgPath, tarImgPath String, String Content1, logoImg String, String Content2) {
 10          the try {
 . 11  
12 is              / * // open path image server
 13 is              the URL = new new URL the URL (srcImgPath);
 14             Connection = url.openConnection the URLConnection ();
 15              // get image data input stream
 16              the InputStream connection.getInputStream IS = ();
 . 17              the BufferedImage srcImg = ImageIO.read (IS); * / 
18 is  
. 19              Image srcImg ImageIO.read = ( new new File (srcImgPath)); // source is a local read path
 20              // Get the width and height of the original image 
21 is              int srcImgwidth = srcImg.getWidth ( null );
 22 is              int srcImgheight = srcImg.getHeight ( null );
 23 is  
24              // draw a watermark need to create a drawing board drawing board 
25             = BuffImg the BufferedImage new new the BufferedImage (srcImgwidth, srcImgheight, BufferedImage.TYPE_INT_RGB);
 26 is              // create a 2D image of 
27              the Graphics2D G = buffImg.createGraphics ();
 28              // drawn 
29              G.drawImage (srcImg, 0 , 0 , srcImgwidth , srcImgheight, null );
 30  
31 is              // black 
32              g.setColor (as Color.BLACK.);
 33 is              // 1.0f as transparency value from 0 to 1.0, sequentially became opaque 
34 is              g.setComposite (AlphaComposite.getInstance (the AlphaComposite. SRC_ATOP, 0.3f));
 35              // Videos layer 
36              g.fillRect (srcImgwidth- 230 , srcImgheight- 410 , 200 is , 310 );
 37 [  
38 is              // white 
39              g.setColor (Color.WHITE);
 40              // set the width of the pen. large, the thick frame 
41 is              g.setStroke ( new new BasicStroke with (. 2F));
 42 is              // set the line transparency value from 0 to 1.0, sequentially became opaque 
43 is              g.setComposite (AlphaComposite.getInstance (AlphaComposite.SRC_OVER));
 44 is              // draw a rectangle 
45              g.drawRect (srcImgwidth-215 , srcImgheight- 395 , 170. , 280 );
 46 is  
47              // set the font style of the watermark 
48              the Font font = new new the Font ( " Microsoft elegant black " , Font.PLAIN, 20 is );
 49              g.setFont (font);
 50              / / the acquired coordinates shown in the corresponding position of the watermark 
51 is              g.drawString ( " ringelman blackness: " , srcImgwidth- 200 is , srcImgheight- 360 );
 52 is  
53 is              // set the font style of the watermark 
54 is              the Font fonts =new new the Font ( " Microsoft elegant black " , Font.PLAIN, 30 );
 55              g.setFont (Fonts);
 56 is              // draw the watermark in the corresponding position of the coordinate acquisition 
57 is              g.drawString ( " level. 3 " , srcImgwidth- 100 , srcImgheight- 355 );
 58  
59  
60              // set the font style of the watermark 
61 is              the Font font1 = new new the Font ( " Microsoft elegant black " , Font.BOLD, 34 is );
 62 is              g.setFont (font1);
 63 is              // the Get watermark coordinates shown in the corresponding position
64-              g.drawString (Content1, srcImgwidth- 205 , srcImgheight- 310 );
 65  
66  
67              // set the font style for the watermark 
68              the Font font2 = new new the Font ( " Microsoft elegant black " , Font.PLAIN, 20 );
 69              g.setFont (font2);
 70              // drawn based on the coordinates acquired at a position corresponding to the watermark 
71 is              g.drawString ( " regional real AQI " , srcImgwidth- 190 , srcImgheight- 190 );
 72  
73 is              // set the watermark font style 
74             = Font3 font new new font ( " Microsoft elegant black " , Font.PLAIN, 60 );
 75              g.setFont (font3);
 76              // draw the watermark based on the coordinate position corresponding to the acquired 
77              g.drawString (Content2, srcImgwidth- 180 [ , srcImgheight- 130. );
 78  
79              // path watermark logo watermark image generally png or gif, and transparency can be provided so that    
80              the ImageIcon imgIcon = new new the ImageIcon (logoImg);   
 81              // get Image object.   
82              Image IMG = imgIcon.getImage ();   
 83              //Transparency    
84              g.setComposite (AlphaComposite.getInstance (AlphaComposite.SRC_ATOP));  
 85              // represents the position of the watermark image    
86              G.drawImage (IMG, srcImgwidth- 160. , srcImgheight- 290 is , imgIcon.getIconWidth () / 2 , imgIcon.getIconHeight () / 2 , null );
 87              // G.drawImage (img.getScaledInstance (450,300, Image.SCALE_SMOOTH), 40, 450, the this); 
 88              // release resources drawing board 
89              g.dispose ();
 90  
91 is              / / output new image 
92              a FileOutputStream the outputStream = new newFileOutputStream (tarImgPath);
 93  
94              // create a new picture 
95              ImageIO.write (buffImg, " PNG " , outputStream);
 96              System. OUT .println ( " watermark addition is complete! " );
 97  
98              // refresh stream 
99              outputStream .flush ();
 100              // Close stream 
101              outputStream.close ();
 102  
103          } the catch (IOException E) {
 104              e.printStackTrace ();
 105          }
 106     }
. 1  public  static  void main (String [] args) throws a FileNotFoundException, a URISyntaxException to {
 2          Long the startTime = System.currentTimeMillis (); 
 . 3          // log image location 
. 4          String logo = " D: \\ documents \\ img \\ Table biomass combustion .png " ;  
 . 5  
. 6          // picture 1, picture servers
 . 7          // String srcImgPath =" http://xxx.xxx.xxxx.xxx : 8080 / 20,191,015 / D54088974 / 111.53.146.19_01_20191015102934821_FIRE_SOURCE_DETECTION.jpg ";   
 . 8          / / original 2, the local picture 
9          String = srcImgPath "D: \\ documents 9252150_092049419327_2.jpg Table \\ " ;   
 10          // rear position watermarked image 
. 11          String tarImgPath = " D: \\ documents \\ Table biomass combustion .jpg " ;
 12 is          String Content1 = " Industrial plume " ;
 13 is          String Content2 = " 122 " ;
 14          // original position, the position of the output image, the watermark text color, text along 
15          new new WaterMarkUtils () myuanark (srcImgPath, tarImgPath, Content1, logo, Content2);.
 16          Long endTime = the System .currentTimeMillis ();
 . 17          . the System OUT .println (" Program Run Time: " + (endTime - the startTime) + " MS " ); // output program run time 
18      }

Third, the results:

 

Local open position: D: \\ documents \\ Table biomass combustion .jpg

 

 

 

Guess you like

Origin www.cnblogs.com/KdeS/p/11805914.html