package com.luojin.tools;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import com.luojin.model.sysUser;
import com.sun.image.codec.jpeg.*;//sun公司仅提供了jpg图片文件的编码api
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
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.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import javax.imageio.ImageIO;
public class AddsignName {
public static final int CENTERX = 470;
public static final int CENTERY = 360;
public AddsignName(sysUser userinfo,String copayName,String backImag,String QRImage,String headImag)
{
try {
String filejob="D:\\员工工作证\\"+userinfo.getUserAccount()+"工作证.jpg";
File filepath=new File(filejob);
if(!filepath.exists()){
filepath.mkdirs();
}
//读取文件
String imagePath=backImag;
//要盖章的图片文件
String toimagepth =QRImage;
//要加盖的头像文件
String headimagepth=headImag;
//转为IO流
InputStream imageIn;
imageIn= new FileInputStream(new File(imagePath));
System.out.println("---------");
//将IO流转换为jpeg格式编码
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(imageIn);
//得到编码后的图片对象
BufferedImage image = decoder.decodeAsBufferedImage();
Graphics g = image.getGraphics();
/*InputStream imageIn2 = null;
imageIn2 = new FileInputStream(new File(toimagepth));
imageIn3 = new FileInputStream(new File(headimagepth));
JPEGImageDecoder decoder2 = JPEGCodec
.createJPEGDecoder(imageIn2);
//得到编码后的图片对象
BufferedImage image2 = decoder2.decodeAsBufferedImage();*/
BufferedImage image2 = getBuffImage(toimagepth);
BufferedImage image3 = getBuffImage(headimagepth);
//加盖图片章
ImageObserver obser = null;
int x = image.getWidth() - image2.getWidth();
int y = image.getHeight() - image2.getHeight();
g.drawImage(image2, x, y, obser);
int x1 = image.getWidth() - image3.getWidth();
int y1 = image.getHeight() - image3.getHeight();
g.drawImage(image3, x1-220, y1-520, obser);
g.setFont(new Font("宋体", Font.BOLD, 35));
g.drawString(userinfo.getUserAccount(), image.getWidth() - 320,
image.getHeight() - 280);
g.drawString(userinfo.getUserIdentfier(), image.getWidth() - 350,
image.getHeight() - 220);
g.drawString(userinfo.getJobNumber(), image.getWidth()-370, image.getHeight()-160);
String message=copayName;
System.out.println("=========");
paintComponent(g, message);
g.dispose();
ImageIO.write(image, "jpeg", filepath);
System.out.println("ok");
} catch (FileNotFoundException e) {
// 自动生成 catch 块
e.printStackTrace();
} catch (ImageFormatException e) {
// 自动生成 catch 块
e.printStackTrace();
} catch (IOException e) {
// 自动生成 catch 块
e.printStackTrace();
}
}
//
public static BufferedImage getBuffImage(String path){
BufferedImage image=null;
try {
InputStream imageIn=new FileInputStream(path);
JPEGImageDecoder decoder2 = JPEGCodec.createJPEGDecoder(imageIn);
try {
image = decoder2.decodeAsBufferedImage();
} catch (ImageFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return image;
}
public static void saveFixedBoundIcon(File imageFile, int height, int width)throws Exception {
double Ratio = 0.0;
if (imageFile == null || !imageFile.isFile())
throw new Exception(imageFile + "找不到指定的文件!");
String filePath = imageFile.getPath();
BufferedImage Bi = ImageIO.read(imageFile);
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();
}
File savefile = new File(filePath + "_" + height + "_" + width
+ ".jpg");
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, "jpeg", savefile);
} catch (Exception ex) {}}}
public void paintComponent(Graphics g, String message )
{
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.RED);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//绘制圆
int radius = 120;
Ellipse2D circle = new Ellipse2D.Double();
circle.setFrameFromCenter(CENTERX, CENTERY, CENTERX + radius, CENTERY + radius);
g2.draw(circle);
//绘制中间的五角星
Font starFont = new Font("宋体", Font.BOLD, 80);
g2.setFont(starFont);
g2.drawString("★", CENTERX - 40, CENTERY + 30);
//根据输入字符串得到字符数组
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 = 40;
Font f = new Font("Serif", Font.BOLD, fontsize);
FontRenderContext context = g2.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)
{
g2.setFont(f);
g2.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);
g2.setFont(f2);
g2.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);
g2.setFont(f2);
g2.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);
g2.setFont(f2);
g2.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);
g2.setFont(f2);
g2.drawString(messages[i], (float)(x0 - ax - char_interval/2* Math.cos(aa)), (float)(y0 + ay + char_interval/2* Math.sin(aa)));
}
}
}
}