Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Code or idea of adding watermark to video with java
Code or idea of adding watermark to video with java
Package com.jc.ts.services;

Import java.awt.color;

Import java.awt.graphics;

Import java.awt.image;

Import java.awt.image.buffered image;

Import java.io.fileinputstream;

import Java . io . filenotfoundexception;

Import java.io.ioexception;

Import javax.imageio.imageio;

Import javax.servlet ... sun.image.codec.jpeg.imageformatexception;

Import com.sun.image.codec.jpeg.jpegcodec;

Import com.sun.image.codec.jpegimageencoder;

/**

* Add image watermarking service category

* */

Public class watermark {

/**

* @param sizeContext adds watermark text.

* @param request request stream object

* @param request response stream object

* */

@SuppressWarnings("deprecation ")

Public static void createmarksize (String Size Context, HttpServletRequest, HttpServletResponse) (

Try {

string path = request . getrealpath(request . getservletpath());

file inputstream in = new file inputstream(path);

image src = imageio . read(in);

int w = src . getwidth(null);

int h = src . getheight(null);

BufferedImage img = new BufferedImage(w, h, BufferedImage. TYPE _ INT _ RGB); //Build the Sketchpad

graphics g = img . get graphics(); //Take a brush

g.drawImage(src,0,0,w,h,null); //Write the source picture to the sketchpad.

g . set color(color . red);

g.drawString(sizeContext, 10,5); //Add text

g . dispose(); //Generate pictures

JPEGImageEncoder e = JPEG codec . createjpegencoder(response . get output stream());

e . encode(img);

response.getOutputStream()。 close();

} catch(file not found exception e){

// TODO automatically generated catch block

e . printstacktrace();

} catch (ImageFormatException e) {

// TODO automatically generated catch block

e . printstacktrace();

} catch (IOException e) {

// TODO automatically generated catch block

e . printstacktrace();

}

}

/**

* @param localPath Add watermark LOGO path.

* @param request request stream object

* @param request response stream object

**/

@SuppressWarnings("deprecation ")

Public static void createMarkLogo (string localPath, HttpServletRequest, HttpServletResponse) {

Try {

file inputstream file = new file inputstream(local path);

image fimg = imageio . read(file);

int fw = fimg . getwidth(null);

int FH = fimg . get height(null);

string path = request . getrealpath(request . getservletpath());

file inputstream in = new file inputstream(path);

image src = imageio . read(in);

int w = src . getwidth(null);

int h = src . getheight(null);

BufferedImage img = new BufferedImage(w, h, BufferedImage. TYPE _ INT _ RGB); //Build the Sketchpad

graphics g = img . get graphics(); //Take a brush

g.drawImage(src,0,0,w,h,null); //Write the original image to the drawing board.

g.drawImage(fimg,w-20,h- 15,fw,fh,null); //Write the watermark image into the drawing board.

g . dispose(); //Generate pictures

JPEGImageEncoder e = JPEG codec . createjpegencoder(response . get output stream());

e . encode(img);

response.getOutputStream()。 close();

} catch(file not found exception e){

// TODO automatically generated catch block

e . printstacktrace();

} catch (ImageFormatException e) {

// TODO automatically generated catch block

e . printstacktrace();

} catch (IOException e) {

// TODO automatically generated catch block

e . printstacktrace();

}

}

}

/**

* @param localPath Add watermark image path

* @param request request stream object

* @param request response stream object

* @param width The width of the watermark image

* @param height The length of the watermark picture.

**/

@SuppressWarnings("deprecation ")

public static void createMarkLogo(String local path,HttpServletRequest request,HttpServletResponse response,int width,int height) {

Try {

file inputstream file = new file inputstream(local path);

image fimg = imageio . read(file);

int fw = fimg . getwidth(null);

int FH = fimg . get height(null);

string path = request . getrealpath(request . getservletpath());

file inputstream in = new file inputstream(path);

image src = imageio . read(in);

int w = src . getwidth(null); //w filters the screen width for you.

int h = src . getheight(null); //h Filter the length of the picture for you.

BufferedImage img = new BufferedImage(w+ width, h+ height, buffered image. TYPE _ INT _ RGB); //Build the artboard (the width of the artboard is the sum of two pictures)

graphics g = img . get graphics(); //Take a brush

g.drawImage(src,0,0,w,h,null); //Write the original image to the drawing board.

g.drawImage(fimg,width,height,fw,fh,null); //Write the watermark image into the drawing board.

g . dispose(); //Generate pictures

JPEGImageEncoder e = JPEG codec . createjpegencoder(response . get output stream());

e . encode(img);

response.getOutputStream()。 close();

} catch(file not found exception e){

// TODO automatically generated catch block

e . printstacktrace();

} catch (ImageFormatException e) {

// TODO automatically generated catch block

e . printstacktrace();

} catch (IOException e) {

// TODO automatically generated catch block

e . printstacktrace();

}

}

}

Pay attention to the annotation of the third method, g.drawimage (fimg, width, height, fw, FH, null); According to the parameters, you are adjusting the hue (below the original).

Filter call

Package com.jc.ts.filter;

Import java.io.ioexception;

Import javax.servlet.filter;

Import javax.servlet.filterchain;

Import javax.servlet.filterconfig;

Import javax.servlet.servletexception;

Import javax.servlet.servletrequest;

Import javax.servlet.servletresponse;

Import javax.servlet ... jc.ts.services.watermark;

The public class WaterFilter implements the filter {

public void destroy() {

// TODO automatically generated method stub

}

public void do filter(servlet request arg 0,ServletResponse arg 1,

FilterChain arg2) raises IOException, ServletException {

http servlet request request =(http servlet request)arg 0;

HttpServletResponse = (httpservlet response) arg1;

//WaterMark.createMarkSize ("Nanjing ts", request, response);

//watermark.createmarklogo ("d: \ \ workspace \ \ mybook \ \ webroot \ \ images \ \ logo \ \ book.jpg", request, response);

watermark . create mark logo(" D:\ \ workspace \ \ mybook \ \ WebRoot \ \ images \ \ logo \ \ book . jpg ",request,response,20,30);

//Note that the path is absolute, and the three effects cannot be executed at the same time.

}

Public void init(FilterConfig arg0) throws ServletException {

// TODO automatically generated method stub

}

}

Web.xml configuration (written on servlet)

& lt filter & gt

& lt/description & gt; this is the description of my J2EE component & lt/description >

& lt/display-name & gt; this is the display name of my J2EE component & lt/display-name >

& lt/filter-name & gt; water filter & lt/filter-name >

& ltfilter-class & gt; com . JC . ts . filter . water filter & lt; /filter-class & gt;

& lt/filter & gt;