servletTool

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)));

}

}

}


}

//-----------------------------------------

package com.luojin.tools;
import java.text.SimpleDateFormat;
import java.util.Date;
public class bussIorder {
public static String creatAccontNum(Integer userid){
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
String accountid=sdf.format(new Date()).toString();
if(userid<10){
 accountid=accountid+"00"+userid.toString();
}else if(userid<100){
 accountid=accountid+"0"+userid.toString();
}else{
 accountid=accountid+userid.toString();
}

return accountid;

}
public static String getOrderNo() {
 long No = 0;
 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
 String nowdate = sdf.format(new Date());
 No = Long.parseLong(nowdate) * 1000;// 这里如果一天订单多的话可以用一万或更大 
 No+=getNo();
 
 return getuserId()+No;
 
}
public static int getNo(){
 return 001;
 
}

public static String getuserId(){
 return "012";
 
}
}
//---------------------------------------------------
package com.luojin.tools;

import java.awt.Image;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageInputStream;

import com.sun.xml.internal.ws.util.ByteArrayBuffer;

public class SqlServerDBOperator {
//数据库服务类
final static String  SQLSERVER_DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String username="sa";
String password="123456";
String dbName="AdministarLSA";
String db_url="jdbc:sqlserver://127.0.0.1:1433; DatabaseName=" + dbName;
 /* final static String  SQLSERVER_DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";*/
 //public  static final String SQLSERVER_DRIVER="oracle.jdbc.driver.OracleDriver";
 /*String username="adminsa as Normal";
 String password="sa123";
 String username="system";
 String password="orcl123#";
 //String db_url="jdbc:sqlserver://127.0.0.1:1433; DatabaseName=" + dbName;
 String db_url="jdbc:oracle:thin://127.0.0.1:1521:ORCL";*/
 public Connection getConnection()//要返回一个连接对象
 {
Connection conn = null;
try {
   // 注册数据库驱动程序
   Class.forName(SQLSERVER_DRIVER);
   // 通过DriverManager(JDBC管理器的)
   // getConnection方法获取连接对象
   // 三个参数分别是 url访问路径,用户名和密码
   // DriverManager检查当前驱动程序是否合法,然后才通过指定参数为客户返回连接对象
   conn = DriverManager.getConnection(db_url, username, password);
} catch (ClassNotFoundException e) {
   e.printStackTrace();
} catch (SQLException e) {
   e.printStackTrace();
}
return conn;
   }
 public void closeConn(Connection conn) {
// 关闭前先检查连接对象是否为空,再检查连接对象是否已经关闭,如果已经关闭,就不用做释放操作了
try {
   if (conn != null && !conn.isClosed()) {
conn.close();
   }
} catch (SQLException e) {
   e.printStackTrace();
}
   }

 /*  public static void main(String[] args) throws Exception {
SqlServerDBOperator oper = new SqlServerDBOperator();
System.out.println(oper.getConnection().toString());
ImageIO.read(new FileImageInputStream(new File("D:/2.gif")));
Connection conn = oper.getConnection();
PreparedStatement ps = conn.prepareStatement("update sysUser set ");
   }*/
 public static void main(String[] args) throws Exception {
SqlServerDBOperator oper = new SqlServerDBOperator();
System.out.println(oper.getConnection().toString());
File file = new File("D://ss2.jpg");
Connection conn = oper.getConnection();
PreparedStatement ps = conn.prepareStatement("select statePersionImg from LandEstateImg where id='d2430e84-cd9d-481d-a30e-82e1238b345a'");
ResultSet rs = ps.executeQuery();
if(rs.next()){
InputStream is = rs.getBinaryStream("statePersionImg");
FileOutputStream fos = new FileOutputStream(file);
byte[] bytes = new byte[1024];
int len = 0;
ByteArrayBuffer buffer = new ByteArrayBuffer();
while ( (len = is.read(bytes)) > 0) {
buffer.write(bytes, 0, len);
}
fos.write(buffer.getRawData());
fos.close();
}
     }
}


你可能感兴趣的:(servletTool)