Image watermark and thumbnail image

Source: Internet
Author: User

Simply modify the image path in the main method to debug or run the

Import Javax.imageio.ImageIO;
Import Javax.swing.Icon;
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;
Import java.text.AttributedString;
Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Graphics2D;
Import Java.awt.Image;
Import Java.awt.font.TextAttribute;
Import Java.awt.geom.AffineTransform;
Import java.awt.image.AffineTransformOp;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import Java.text.AttributedCharacterIterator;

public class Imagehandle {

Public Imagehandle () {

}

/**
* Add watermark to the image (only jpeg/jpg format is supported)
*
* @param FilePath
* The path of the picture that needs to be added watermark
* @param markcontent
* Watermark Text
* @param markcontentcolor
* Color of watermark Text
* @param qualnum
* Picture quality
* @param fonttype
* Font
* @param fontsize
* Font Size
* @return Boolean
*/
public boolean Createmark (String FilePath, String markcontent,
Color Markcontentcolor, float qualnum, String fonttype, int fontSize) {

ImageIcon Imgicon = new ImageIcon (FilePath);
Image theimg = Imgicon.getimage ();
int width = theimg.getwidth (null);
int height = theimg.getheight (null);

BufferedImage bimage = new BufferedImage (width, height,bufferedimage.type_int_rgb);
Graphics2D g = bimage.creategraphics ();
G.setcolor (Markcontentcolor);
G.setbackground (Color.White);
G.drawimage (theimg, 0, 0, NULL);

attributedstring ATS = new attributedstring (markcontent);
Font f = new Font (Fonttype, Font.Bold, fontSize);
Ats.addattribute (Textattribute.font, F, 0, markcontent.length ());
Attributedcharacteriterator iter = Ats.getiterator ();

g.DrawString (Iter,0,f.getsize ()); Add watermark text and set the location where the watermark text appears (upper left corner)
G.dispose ();

  try {
   fileoutputstream out = new FileOutputStream (filePath);
   jpegimageencoder encoder = Jpegcodec.createjpegencoder (out);
   jpegencodeparam param = Encoder.getdefaultjpegencodeparam (bimage);
   param.setquality (Qualnum, true);
   encoder.encode (bimage, param);
   out.close ();
  } catch (Exception e) {
   return false;
  }
  return true;
 }

/**
* Create thumbnails of images
*
* @param FilePath
* Path of the picture
* @param width
* The width of the thumbnail you want to generate
* @param height
* The height of the thumbnail you want to generate
* @return Icon
*/
Public Icon Createfixedboundicon (String filePath, int width, int height)
Throws Exception {

File F = new file (FilePath);
Double Ratio = 0.0; Zoom ratio

if (! F.isfile ())
throw New Exception (F
+ "is not an image file error in getfixedboundicon!");

Icon icon = new ImageIcon (FilePath);
BufferedImage Bi = Imageio.read (F);

if ((Bi.getheight () > height) | | (Bi.getwidth () > width)) {
if (Bi.getheight () > Bi.getwidth ()) {
Ratio = (new Integer (height)). Doublevalue ()/bi.getheight ();
} else {
Ratio = (new Integer (width)). Doublevalue ()/bi.getwidth ();
}
int lastlength = Filepath.lastindexof (".");
String Subfilepath = filepath.substring (0, lastlength);

String Filename=this. GetFileName (Subfilepath);//The file name of the picture
String filetypes=filepath.substring (lastlength+1); Type of picture
File Zoomfile = new File (subfilepath+ "_" +filename+ ".") +filetypes);//The full path of the picture
Image Itemp = bi.getscaledinstance (width, height, image.scale_smooth);
Affinetransformop op = new Affinetransformop (affinetransform
. Getscaleinstance (Ratio, Ratio), NULL);
Itemp = Op.filter (Bi, NULL);

try {
Imageio.write ((bufferedimage) Itemp, "JPG", zoomfile);
icon = new ImageIcon (Zoomfile.getpath ()); Setting the Output path
} catch (Exception ex) {
Ex.printstacktrace ();
}
}
return icon;
}

/**
* Get filename in file path
*
* @param FilePath
* Path of the file
* @return String
*/
public string GetFileName (string filepath) {

String returnstr = "*. *";
int length = Filepath.trim (). Length ();

filepath = Filepath.replace ('//', '/');
if (length > 0) {
int i = Filepath.lastindexof ("/");
if (I >= 0) {
filepath = filepath.substring (i + 1);
RETURNSTR = filepath;
}
}
return returnstr;
}


public static void Main (string[] args)
{
Imagehandle wm = new Imagehandle ();
try{
Wm.createmark ("G://q1.jpg", "registered trademark", color.blue,5f, "Song Body", 12);
System.out.println ("adds a watermark to the picture!");
Wm.createfixedboundicon ("G://q3.gif", 128,128);
System.out.println ("generated thumbnails for pictures!");
}catch (Exception e) {
E.printstacktrace ();
}
}

}

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.