package com.walkerjava.utils
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.font.TextAttribute;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;
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;
/**
* Construct image output
*/
public class Test {
/**
*
* @param soundFilePath
* : source image path
* @param targetFilePath
* : The generated target image path
* @param markContent
* : the text to be added
* @param markContentColor
* : text color
* @param qualNum
* : Quality number
* @param fontType
* : font type
* @param fontSize
* :font size
* @return
*/
public static void createMark(String souchFilePath, String targetFilePath,
String markContent, Color markContentColor, float qualNum,
String fontType, int fontSize, int w, int h, Color color) {
markContentColor = color;
/* Build the source image to process */
ImageIcon imageIcon = new ImageIcon(souchFilePath);
/* Get the image to process */
Image image = imageIcon.getImage();
// Image can get the attribute information of the image
int width = image.getWidth(null);
int height = image.getHeight(null);
// To draw a picture of the same size as the source picture (you can define it yourself)
BufferedImage bImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// Build the 2D brush
Graphics2D g = bImage.createGraphics();
/* Set the color of the text drawn by the 2D brush */
g.setColor(markContentColor);
/* Set the background color of the text drawn by the 2D brush*/
g.setBackground(Color.white);
/* draw the picture */
g.drawImage(image, 0, 0, null);
/* --------Process the text to be displayed -------------- */
AttributedString ats = new AttributedString(markContent);
Font font = new Font(fontType, Font.BOLD, fontSize);
g.setFont(font);
/* Eliminate the aliasing of java.awt.Font font */
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
/* Eliminate the aliasing of java.awt.Font font */
For more examples, please visit http://www.walkerjava.com/forum.php?mod=viewthread&tid=135
// font = g.getFont().deriveFont(30.0f);
ats.addAttribute (TextAttribute.FONT, font, 0, markContent.length ());
AttributedCharacterIterator path = ats.getIterator();
/* Add the text of the watermark and set the content of the watermark text ---- position */
g.drawString(iter, width - w, height - h);
/* --------Process the text to be displayed -------------- www.it165.net */
g.dispose();// The end of the brush
try {
// output file to the specified path
FileOutputStream out = new FileOutputStream(targetFilePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bImage);
param.setQuality(qualNum, true);
encoder.encode(bImage, param);
out.close();
} catch (Exception e) {
e.printStackTrace ();
}
}
public static void main(String[] args) {
Test.createMark("d:\\image\\daili.jpg", "d:\\image\\601.jpg",
"This is a text watermark added to the picture by a java program", null, 1, "Simplified square block script", 30, 700, 300,
Color.GRAY);
}
}