Java_ Picture _ text _ Plus watermark

Source: Internet
Author: User

Import Java.awt.Color;

Import Java.awt.Font;

Import Java.awt.Graphics;

Import Java.awt.Graphics2D;

Import Java.awt.Image;

Import Java.awt.image.BufferedImage;

Import Java.io.File;

Import Java.io.FileOutputStream;

Import Javax.imageio.imageio;import Javax.swing.ImageIcon;

Import Com.sun.image.codec.jpeg.JPEGCodec;

Import Com.sun.image.codec.jpeg.JPEGEncodeParam;

Import Com.sun.image.codec.jpeg.JPEGImageEncoder;

public class test{

public static void Main (String arg[]) {

Pressimage ("E:/2.png", "E:/1.jpg", 0, 0); Image

Presstext ("text", "E:/1.jpg", "", 1, color.red,30, 60, 50); Text

}

/**

* Print the picture on the picture

*

* @param pressimg--

* Watermark File

* @param targetimg--

* Target file

* @param x

*--x coordinates

* @param y

*--Y coordinates

*/

Public final static void Pressimage (String pressimg, String targetimg,

int x, int y) {

try {

Target file

File _file = new file (targetimg);

Image src = imageio.read (_file);

int wideth = Src.getwidth (null);

int height = src.getheight (null);

BufferedImage image = New BufferedImage (wideth, height,

BUFFEREDIMAGE.TYPE_INT_RGB);

Graphics g = image.creategraphics ();

G.drawimage (SRC, 0, 0, wideth, height, null);

Watermark File

File _filebiao = new file (pressimg);

Image Src_biao = Imageio.read (_filebiao);

int wideth_biao = Src_biao.getwidth (null);

int height_biao = Src_biao.getheight (null);

G.drawimage (Src_biao, (Wideth-wideth_biao)/2,

(Height-height_biao)/2, Wideth_biao, Height_biao, NULL);

Watermark File End

G.dispose ();

FileOutputStream out = new FileOutputStream (targetimg);

JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out);

Encoder.encode (image);

Out.close ();

} catch (Exception e) {

E.printstacktrace ();

}

}

/**

* Print Text watermark image

*

* @param presstext

*--text

* @param targetimg--

* Target Image

* @param fontname--

* Font Name

* @param fontstyle--

* Font Style

* @param Color--

* Font Color

* @param fontSize--

* Font Size

* @param x--

* Offset Amount

* @param y

*/

public static void Presstext (String presstext, String targetimg,

String fontname, int fontstyle, color color, int fontSize, int x,int y) {

try {

File _file = new file (targetimg);

Image src = imageio.read (_file);

int wideth = Src.getwidth (null);

int height = src.getheight (null);

BufferedImage image = New BufferedImage (wideth, height,

BUFFEREDIMAGE.TYPE_INT_RGB);

Graphics g = image.creategraphics ();

G.drawimage (SRC, 0, 0, wideth, height, null);

String s= "www.qhd.com.cn";

G.setcolor (color);

G.setfont (New Font (FontName, FontStyle, fontSize));

g.DrawString (Presstext, x, y);

G.dispose ();

FileOutputStream out = new FileOutputStream (targetimg);

JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out);

Encoder.encode (image);

Out.close ();

} catch (Exception e) {

System.out.println (e);

}

}

}

JAVA SPRINGM

    • This article is from: Linux Tutorial Network

Java_ Picture _ text _ Plus watermark

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.