package test;
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.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class AppHttp {
private static final int WIDTH = 500;//图片宽度
private static final int HEIGHT = 500;//图片高度
private static String message = "深圳前海大数金融服务有限公司";
private static String centerName = "开发二部";
private static String year = "2017年10月09日";
public static void main(String[] args) throws Exception{
BufferedImage image = startGraphics2D();
try {
//"D:/Users/ex-zhangsheng/Desktop/1.jpg"
String targetImg ="D:/Users/ex-zhangsheng/Desktop/2.jpg";
String waterImg ="D:/Users/ex-zhangsheng/Desktop/1314.png";
int x=0;
int y=0;
float alpha=0.5f;
//String filePath = "D:/Users/ex-zhangsheng/Desktop/"+new Date().getTime()+".png";
pressImage(targetImg, waterImg, x, y, alpha);
//ImageIO.write(image, "png", new File(filePath)); //将其保存在C:\\Users\\huage\\Desktop\\121231\\下
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static BufferedImage startGraphics2D(){
// 定义图像buffer
BufferedImage buffImg = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
g.setColor(Color.RED);
//设置锯齿圆滑
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//绘制圆
int radius = HEIGHT/3;//周半径
int CENTERX = WIDTH/2;//画图所出位置
int CENTERY = HEIGHT/2;//画图所处位置
Ellipse2D circle = new Ellipse2D.Double();
circle.setFrameFromCenter(CENTERX, CENTERY, CENTERX + radius, CENTERY + radius);
g.draw(circle);
//绘制中间的五角星
g.setFont(new Font("宋体", Font.BOLD, 120));
g.drawString("★", CENTERX-(120/2), CENTERY+(120/3));
//添加姓名
g.setFont(new Font("宋体", Font.LAYOUT_LEFT_TO_RIGHT, 30));// 写入签名
g.drawString(centerName, CENTERX -(40), CENTERY +(30+50));
//添加年份
g.setFont(new Font("宋体", Font.LAYOUT_LEFT_TO_RIGHT, 20));// 写入签名
g.drawString(year, CENTERX -(60), CENTERY +(30+80));
//根据输入字符串得到字符数组
String[] messages2 = message.split("",0);
String[] messages = new String[messages2.length-1];
System.arraycopy(messages2,1,messages,0,messages2.length-1);
//输入的字数
int ilength = messages.length;
//设置字体属性
int fontsize = 35;
Font f = new Font("Serif", Font.BOLD, fontsize);
FontRenderContext context = g.getFontRenderContext();
Rectangle2D bounds = f.getStringBounds(message, context);
//字符宽度=字符串长度/字符数
double char_interval = (bounds.getWidth() / ilength);
//上坡度
double ascent = -bounds.getY();
int first = 0,second = 0;
boolean odd = false;
if (ilength%2 == 1)
{
first = (ilength-1)/2;
odd = true;
}
else
{
first = (ilength)/2-1;
second = (ilength)/2;
odd = false;
}
double radius2 = radius - ascent;
double x0 = CENTERX;
double y0 = CENTERY - radius + ascent;
//旋转角度
double a = 2*Math.asin(char_interval/(2*radius2));
if (odd)
{
g.setFont(f);
g.drawString(messages[first], (float)(x0 - char_interval/2), (float)y0);
//中心点的右边
for (int i=first+1;i<ilength;i++)
{
double aa = (i - first) * a;
double ax = radius2 * Math.sin(aa);
double ay = radius2 - radius2 * Math.cos(aa);
AffineTransform transform = AffineTransform.getRotateInstance(aa);//,x0 + ax, y0 + ay);
Font f2 = f.deriveFont(transform);
g.setFont(f2);
g.drawString(messages[i], (float)(x0 + ax - char_interval/2* Math.cos(aa)), (float)(y0 + ay - char_interval/2* Math.sin(aa)));
}
//中心点的左边
for (int i=first-1;i>-1;i--)
{
double aa = (first - i) * a;
double ax = radius2 * Math.sin(aa);
double ay = radius2 - radius2 * Math.cos(aa);
AffineTransform transform = AffineTransform.getRotateInstance(-aa);//,x0 + ax, y0 + ay);
Font f2 = f.deriveFont(transform);
g.setFont(f2);
g.drawString(messages[i], (float)(x0 - ax - char_interval/2* Math.cos(aa)), (float)(y0 + ay + char_interval/2* Math.sin(aa)));
}
}
else
{
//中心点的右边
for (int i=second;i<ilength;i++)
{
double aa = (i - second + 0.5) * a;
double ax = radius2 * Math.sin(aa);
double ay = radius2 - radius2 * Math.cos(aa);
AffineTransform transform = AffineTransform.getRotateInstance(aa);//,x0 + ax, y0 + ay);
Font f2 = f.deriveFont(transform);
g.setFont(f2);
g.drawString(messages[i], (float)(x0 + ax - char_interval/2* Math.cos(aa)), (float)(y0 + ay - char_interval/2* Math.sin(aa)));
}
//中心点的左边
for (int i=first;i>-1;i--)
{
double aa = (first - i + 0.5) * a;
double ax = radius2 * Math.sin(aa);
double ay = radius2 - radius2 * Math.cos(aa);
AffineTransform transform = AffineTransform.getRotateInstance(-aa);//,x0 + ax, y0 + ay);
Font f2 = f.deriveFont(transform);
g.setFont(f2);
g.drawString(messages[i], (float)(x0 - ax - char_interval/2* Math.cos(aa)), (float)(y0 + ay + char_interval/2* Math.sin(aa)));
}
}
return buffImg;
}
/**
* 添加图片水印
*
* @param targetImg
* 目标图片路径,如:C:\\kutuku.jpg
* @param waterImg
* 水印图片路径,如:C:\\kutuku.png
* @param x
* 水印图片距离目标图片左侧的偏移量,如果x<0, 则在正中间
* @param y
* 水印图片距离目标图片上侧的偏移量,如果y<0, 则在正中间
* @param alpha
* 透明度(0.0 -- 1.0, 0.0为完全透明,1.0为完全不透明)
*/
public final static void pressImage(String targetImg, String waterImg,
int x, int y, float alpha) {
try {
// 加载目标图片
File file = new File(targetImg);
Image image = ImageIO.read(file);
int width = image.getWidth(null);
int height = image.getHeight(null);
// 将目标图片加载到内存。
BufferedImage bufferedImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufferedImage.createGraphics();
g.drawImage(image, 0, 0, width, height, null);
// 加载水印图片。
Image waterImage = ImageIO.read(new File(waterImg));
int width_1 = waterImage.getWidth(null);
int height_1 = waterImage.getHeight(null);
// 设置水印图片的透明度。
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
alpha));
// 设置水印图片的位置。
int widthDiff = width - width_1;
int heightDiff = height - height_1;
if (x < 0) {
x = widthDiff / 2;
} else if (x > widthDiff) {
x = widthDiff;
}
if (y < 0) {
y = heightDiff / 2;
} else if (y > heightDiff) {
y = heightDiff;
}
// 将水印图片“画”在原有的图片的制定位置。
g.drawImage(waterImage, x, y, width_1, height_1, null);
// 关闭画笔。
g.dispose();
// 保存目标图片。
ImageIO.write(bufferedImage, "jpg", file);
} catch (IOException e) {
e.printStackTrace();
}
}
}
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.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class AppHttp {
private static final int WIDTH = 500;//图片宽度
private static final int HEIGHT = 500;//图片高度
private static String message = "深圳前海大数金融服务有限公司";
private static String centerName = "开发二部";
private static String year = "2017年10月09日";
public static void main(String[] args) throws Exception{
BufferedImage image = startGraphics2D();
try {
//"D:/Users/ex-zhangsheng/Desktop/1.jpg"
String targetImg ="D:/Users/ex-zhangsheng/Desktop/2.jpg";
String waterImg ="D:/Users/ex-zhangsheng/Desktop/1314.png";
int x=0;
int y=0;
float alpha=0.5f;
//String filePath = "D:/Users/ex-zhangsheng/Desktop/"+new Date().getTime()+".png";
pressImage(targetImg, waterImg, x, y, alpha);
//ImageIO.write(image, "png", new File(filePath)); //将其保存在C:\\Users\\huage\\Desktop\\121231\\下
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static BufferedImage startGraphics2D(){
// 定义图像buffer
BufferedImage buffImg = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
g.setColor(Color.RED);
//设置锯齿圆滑
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//绘制圆
int radius = HEIGHT/3;//周半径
int CENTERX = WIDTH/2;//画图所出位置
int CENTERY = HEIGHT/2;//画图所处位置
Ellipse2D circle = new Ellipse2D.Double();
circle.setFrameFromCenter(CENTERX, CENTERY, CENTERX + radius, CENTERY + radius);
g.draw(circle);
//绘制中间的五角星
g.setFont(new Font("宋体", Font.BOLD, 120));
g.drawString("★", CENTERX-(120/2), CENTERY+(120/3));
//添加姓名
g.setFont(new Font("宋体", Font.LAYOUT_LEFT_TO_RIGHT, 30));// 写入签名
g.drawString(centerName, CENTERX -(40), CENTERY +(30+50));
//添加年份
g.setFont(new Font("宋体", Font.LAYOUT_LEFT_TO_RIGHT, 20));// 写入签名
g.drawString(year, CENTERX -(60), CENTERY +(30+80));
//根据输入字符串得到字符数组
String[] messages2 = message.split("",0);
String[] messages = new String[messages2.length-1];
System.arraycopy(messages2,1,messages,0,messages2.length-1);
//输入的字数
int ilength = messages.length;
//设置字体属性
int fontsize = 35;
Font f = new Font("Serif", Font.BOLD, fontsize);
FontRenderContext context = g.getFontRenderContext();
Rectangle2D bounds = f.getStringBounds(message, context);
//字符宽度=字符串长度/字符数
double char_interval = (bounds.getWidth() / ilength);
//上坡度
double ascent = -bounds.getY();
int first = 0,second = 0;
boolean odd = false;
if (ilength%2 == 1)
{
first = (ilength-1)/2;
odd = true;
}
else
{
first = (ilength)/2-1;
second = (ilength)/2;
odd = false;
}
double radius2 = radius - ascent;
double x0 = CENTERX;
double y0 = CENTERY - radius + ascent;
//旋转角度
double a = 2*Math.asin(char_interval/(2*radius2));
if (odd)
{
g.setFont(f);
g.drawString(messages[first], (float)(x0 - char_interval/2), (float)y0);
//中心点的右边
for (int i=first+1;i<ilength;i++)
{
double aa = (i - first) * a;
double ax = radius2 * Math.sin(aa);
double ay = radius2 - radius2 * Math.cos(aa);
AffineTransform transform = AffineTransform.getRotateInstance(aa);//,x0 + ax, y0 + ay);
Font f2 = f.deriveFont(transform);
g.setFont(f2);
g.drawString(messages[i], (float)(x0 + ax - char_interval/2* Math.cos(aa)), (float)(y0 + ay - char_interval/2* Math.sin(aa)));
}
//中心点的左边
for (int i=first-1;i>-1;i--)
{
double aa = (first - i) * a;
double ax = radius2 * Math.sin(aa);
double ay = radius2 - radius2 * Math.cos(aa);
AffineTransform transform = AffineTransform.getRotateInstance(-aa);//,x0 + ax, y0 + ay);
Font f2 = f.deriveFont(transform);
g.setFont(f2);
g.drawString(messages[i], (float)(x0 - ax - char_interval/2* Math.cos(aa)), (float)(y0 + ay + char_interval/2* Math.sin(aa)));
}
}
else
{
//中心点的右边
for (int i=second;i<ilength;i++)
{
double aa = (i - second + 0.5) * a;
double ax = radius2 * Math.sin(aa);
double ay = radius2 - radius2 * Math.cos(aa);
AffineTransform transform = AffineTransform.getRotateInstance(aa);//,x0 + ax, y0 + ay);
Font f2 = f.deriveFont(transform);
g.setFont(f2);
g.drawString(messages[i], (float)(x0 + ax - char_interval/2* Math.cos(aa)), (float)(y0 + ay - char_interval/2* Math.sin(aa)));
}
//中心点的左边
for (int i=first;i>-1;i--)
{
double aa = (first - i + 0.5) * a;
double ax = radius2 * Math.sin(aa);
double ay = radius2 - radius2 * Math.cos(aa);
AffineTransform transform = AffineTransform.getRotateInstance(-aa);//,x0 + ax, y0 + ay);
Font f2 = f.deriveFont(transform);
g.setFont(f2);
g.drawString(messages[i], (float)(x0 - ax - char_interval/2* Math.cos(aa)), (float)(y0 + ay + char_interval/2* Math.sin(aa)));
}
}
return buffImg;
}
/**
* 添加图片水印
*
* @param targetImg
* 目标图片路径,如:C:\\kutuku.jpg
* @param waterImg
* 水印图片路径,如:C:\\kutuku.png
* @param x
* 水印图片距离目标图片左侧的偏移量,如果x<0, 则在正中间
* @param y
* 水印图片距离目标图片上侧的偏移量,如果y<0, 则在正中间
* @param alpha
* 透明度(0.0 -- 1.0, 0.0为完全透明,1.0为完全不透明)
*/
public final static void pressImage(String targetImg, String waterImg,
int x, int y, float alpha) {
try {
// 加载目标图片
File file = new File(targetImg);
Image image = ImageIO.read(file);
int width = image.getWidth(null);
int height = image.getHeight(null);
// 将目标图片加载到内存。
BufferedImage bufferedImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufferedImage.createGraphics();
g.drawImage(image, 0, 0, width, height, null);
// 加载水印图片。
Image waterImage = ImageIO.read(new File(waterImg));
int width_1 = waterImage.getWidth(null);
int height_1 = waterImage.getHeight(null);
// 设置水印图片的透明度。
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
alpha));
// 设置水印图片的位置。
int widthDiff = width - width_1;
int heightDiff = height - height_1;
if (x < 0) {
x = widthDiff / 2;
} else if (x > widthDiff) {
x = widthDiff;
}
if (y < 0) {
y = heightDiff / 2;
} else if (y > heightDiff) {
y = heightDiff;
}
// 将水印图片“画”在原有的图片的制定位置。
g.drawImage(waterImage, x, y, width_1, height_1, null);
// 关闭画笔。
g.dispose();
// 保存目标图片。
ImageIO.write(bufferedImage, "jpg", file);
} catch (IOException e) {
e.printStackTrace();
}
}
}