Copy Code code as follows:
Package com.blogs.image;
Import Java.awt.AlphaComposite;
Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Graphics2D;
Import Java.awt.Image;
Import java.awt.RenderingHints;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Javax.imageio.ImageIO;
Import Javax.swing.ImageIcon;
/**
* Image Watermark
*/
public class Imageutil {
/**
* @param args
*/
public static void Main (string[] args) {
String Srcimgpath = "E:/2.png";
String IconPath = "E:\\logo.jpg";
String Targerpath = "E:/3.jpg";
Add a watermark to a picture
Imageutil.watermarkimagebyicon (IconPath, Srcimgpath, Targerpath, 0, 0,
, 0.1f);
Add a watermark to the picture, watermark rotation-45
Imagemarklogobyicon.markimagebyicon (IconPath, Srcimgpath,
TargerPath2,-45);
}
/**
* To add a watermark to the picture, you can set the watermark picture rotation angle
*
* @param iconpath
* Watermark Picture Path
* @param Srcimgpath
* Source Picture Path
* @param Targerpath
* Target Picture path
* @param degree
* Watermark Picture Rotation angle
* @param width
* Width (compared with left)
* @param height
* Height (compared with top)
* @param clarity
* Transparency (number less than 1) closer to 0 more transparent
*/
public static void Watermarkimagebyicon (String iconpath, String Srcimgpath,
String Targerpath, Integer degree, integer width, integer height,
Float clarity) {
OutputStream OS = null;
try {
Image srcimg = Imageio.read (new File (Srcimgpath));
System.out.println ("width:" + srcimg.getwidth (null));
System.out.println ("Height:" + srcimg.getheight (null));
BufferedImage buffimg = new BufferedImage (Srcimg.getwidth (NULL),
Srcimg.getheight (NULL), BUFFEREDIMAGE.TYPE_INT_RGB);
Get the Brush object
Graphics g= buffimg.getgraphics ();
Graphics2D g = buffimg.creategraphics ();
Set Jagged Edge processing for line segments
G.setrenderinghint (Renderinghints.key_interpolation,
Renderinghints.value_interpolation_bilinear);
G.drawimage (
Srcimg.getscaledinstance (Srcimg.getwidth (NULL),
Srcimg.getheight (NULL), Image.scale_smooth), 0, 0,
NULL);
if (null!= degree) {
Set Watermark rotation
G.rotate (Math.toradians (degree),
(double) Buffimg.getwidth ()/2,
(double) Buffimg.getheight ()/2);
}
Watermark Image path watermark is generally GIF or PNG, so you can set the transparency
ImageIcon Imgicon = new ImageIcon (IconPath);
Gets the image object.
Image img = imgicon.getimage ();
float alpha = clarity; Transparency
G.setcomposite (Alphacomposite.getinstance (Alphacomposite.src_atop,
Alpha));
Represents the location of a watermark picture
G.drawimage (img, width, height, null);
G.setcomposite (Alphacomposite.getinstance (alphacomposite.src_over));
G.dispose ();
OS = new FileOutputStream (Targerpath);
Generate pictures
Imageio.write (buffimg, "JPG", OS);
System.out.println ("Add watermark Picture Complete!");
catch (Exception e) {
E.printstacktrace ();
finally {
try {
if (null!= OS)
Os.close ();
catch (Exception e) {
E.printstacktrace ();
}
}
}
/**
* To add a watermark to the picture, you can set the watermark picture rotation angle
*
* @param logotext
* Watermark Text
* @param Srcimgpath
* Source Picture Path
* @param Targerpath
* Target Picture path
* @param degree
* Watermark Picture Rotation angle
* @param width
* Width (compared with left)
* @param height
* Height (compared with top)
* @param clarity
* Transparency (number less than 1) closer to 0 more transparent
*/
public static void Watermarkbytext (String logotext, String Srcimgpath,
String Targerpath, Integer degree, integer width, integer height,
Float clarity) {
The path to the main picture
InputStream is = null;
OutputStream OS = null;
try {
Image srcimg = Imageio.read (new File (Srcimgpath));
BufferedImage buffimg = new BufferedImage (Srcimg.getwidth (NULL),
Srcimg.getheight (NULL), BUFFEREDIMAGE.TYPE_INT_RGB);
Get the Brush object
Graphics g= buffimg.getgraphics ();
Graphics2D g = buffimg.creategraphics ();
Set Jagged Edge processing for line segments
G.setrenderinghint (Renderinghints.key_interpolation,
Renderinghints.value_interpolation_bilinear);
G.drawimage (
Srcimg.getscaledinstance (Srcimg.getwidth (NULL),
Srcimg.getheight (NULL), Image.scale_smooth), 0, 0,
NULL);
if (null!= degree) {
Set Watermark rotation
G.rotate (Math.toradians (degree),
(double) Buffimg.getwidth ()/2,
(double) Buffimg.getheight ()/2);
}
Set color
G.setcolor (color.red);
Set Font
G.setfont (New Font ("Song Body", Font.Bold, 30));
float alpha = clarity;
G.setcomposite (Alphacomposite.getinstance (Alphacomposite.src_atop,
Alpha));
The first parameter-> the contents of the set, followed by two parameters-> the coordinates of the text on the picture (X,y).
g.DrawString (logotext, width, height);
G.dispose ();
OS = new FileOutputStream (Targerpath);
Generate pictures
Imageio.write (buffimg, "JPG", OS);
System.out.println ("Add watermark Text complete!");
catch (Exception e) {
E.printstacktrace ();
finally {
try {
if (null!= is)
Is.close ();
catch (Exception e) {
E.printstacktrace ();
}
try {
if (null!= OS)
Os.close ();
catch (Exception e) {
E.printstacktrace ();
}
}
}
}
There is also a picture scaling code:
Copy Code code as follows:
/**
* Picture Scaling (picture zoom to a specified size, white fill in blank)
*
* @param Srcpath
* Source Picture Path
* @param destpath
* Image path after zooming
*/
public static void Zoomimage (String srcpath, string destpath, int destheight, int destwidth) {
try {
BufferedImage srcbufferedimage = Imageio.read (new File (Srcpath));
int imgwidth = Destwidth;
int imgheight = Destheight;
int srcwidth = Srcbufferedimage.getwidth ();
int srcheight = Srcbufferedimage.getheight ();
if (srcheight >= srcwidth) {
ImgWidth = (int) math.round ((Destheight * 1.0/srcheight) * srcwidth));
} else {
ImgHeight = (int) math.round ((Destwidth * 1.0/srcwidth) * srcheight));
}
BufferedImage destbufferedimage = new BufferedImage (Destwidth, Destheight, Bufferedimage.type_int_rgb);
Graphics2D graphics2d = Destbufferedimage.creategraphics ();
Graphics2d.setbackground (Color.White);
Graphics2d.clearrect (0, 0, destwidth, destheight);
Graphics2d.drawimage (Srcbufferedimage.getscaledinstance (ImgWidth, ImgHeight, Image.scale_smooth), (DESTWIDTH/2)-( IMGWIDTH/2), (DESTHEIGHT/2)-(IMGHEIGHT/2), null);
Graphics2d.dispose ();
Imageio.write (Destbufferedimage, "JPEG", New File (DestPath));
catch (IOException e) {
E.printstacktrace ();
}
}