45fan.com - 路饭网

搜索: 您的位置主页 > 电脑频道 > 电脑教程 > 阅读资讯:java操作图片的方法

java操作图片的方法

2016-09-05 06:19:46 来源:www.45fan.com 【

java操作图片的方法

package com.hhwy.mis.equipment.trigraph.img;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hhwy.mis.database.DBOperator;
import com.hhwy.mis.equipment.trigraph.dao.ImgDao;
import com.hhwy.mis.equipment.trigraph.img.ganta.DrawImg;
import com.hhwy.mis.equipment.trigraph.img.ganta.ImgData;
import com.hhwy.mis.equipment.trigraph.img.ganta.RoundData;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class CreateImageServlet extends HttpServlet {

private static final String CONTENT_TYPE = "image/jpeg;charset=GB2312";

/**
* Constructor of the object.
*/
public CreateImageServlet() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);

String imageFile="/mis/style/temp1/images/aa.jpg"; //被嵌的图片的虚拟路径
// int x = 0; //坐标
// int y = 0;


ServletOutputStream output=response.getOutputStream();

if(imageFile.toLowerCase().endsWith(".jpeg")||imageFile.toLowerCase().endsWith(".jpg")) {
imageFile = getServletContext().getRealPath(imageFile);
// System.out.println("绝对路径===="+imageFile);

long gtID=(long) 3.77176468794521E16;// //77176468794524E16
if(request.getParameter("gtID")!=null)
gtID=Long.parseLong(request.getParameter("gtID"));
// System.out.println("gtID===="+request.getParameter("gtID"));

// if(request.getParameter("gtID").equals("1"))
// gtID=(long) 3.77176468794527E16;//3.77176468794479E16
// if(request.getParameter("gtID").equals("2"))
// gtID=(long) 3.77176468794522E16;//
// if(request.getParameter("gtID").equals("3"))
// gtID=(long) 3.77176468794527E16;//

//
DBOperator dbo = new DBOperator();
ImgDao imgDao=new ImgDao();
ImgData imgData=imgDao.getImgData(dbo,gtID);


InputStream imageIn =imgData.getInputStream(); //new FileInputStream(new File(imageFile));

if (imageIn != null) {
try {
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(imageIn);
BufferedImage image = decoder.decodeAsBufferedImage();
Graphics g = image.getGraphics();
initFont(g); // 初始化字体
System.out.println("font====" + g.getFont());

// List list=testInitData();
List list = imgDao.getRoundList(imgData);
new DrawImg().drawRounds(g, list);

// //////////////////////////
// 输出数据流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
encoder.encode(image);
imageIn.close();
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
dbo.freeCon();

}
output.close();
}


private void initFont(Graphics g) {
String fontColor = ""; // 字体颜色
int fontSize = 14; // 字体大小
String fontStyle = "bold"; // 字体风格(斜体,粗体等)
String fontName = "宋体"; // 字体名称
g.setColor(Color.BLUE);
Font mFont = new Font(fontName, Font.PLAIN, fontSize);// 默认字体
if (fontStyle.equalsIgnoreCase("italic"))
mFont = new Font(fontName, Font.ITALIC, fontSize);
if (fontStyle.equalsIgnoreCase("bold"))
mFont = new Font(fontName, Font.BOLD, fontSize);
if (fontStyle.equalsIgnoreCase("plain"))
mFont = new Font(fontName, Font.PLAIN, fontSize);
// System.out.println("字体大小:=" + mFont.getSize());
g.setFont(mFont);
}


private List testInitData() {
List list=new ArrayList();
list.add(new RoundData(20,40,8));
list.add(new RoundData(50,80,8));
list.add(new RoundData(45,30,8));
list.add(new RoundData(50,60,8));
//list.add(new RoundData(20,15,8));
return list;
}


/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doGet(request,response);
}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}

}
/**
* Copyright(c)2006 Beijing ieforever Co. Ltd.
* All right reserved.
* 项目名称:北京电力配网MIS系统
* 创建日期:2006-5-16
*/

package com.hhwy.mis.equipment.trigraph.img.ganta;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.util.*;
/**
* 创建日期:2006-5-16
* Title:文件所属模块(台帐、查询统计、权限、运行管理、报表)
* Description:对本文件的详细描述,原则上不能少于50字
* @author shaokun305
* @mender:(文件的修改者,文件创建者之外的人)
* @version 1.0
* Remark:认为有必要的其他信息
*/

public class DrawImg {

/**
* 字母标注集合。
*/
public static final String[] LETTER= {"A","B","C","D","E","F","G","H","I","J","K","L"};

/**
*
* 功能:根据索引获得随机规定的颜色Color。
* 作者: shaokun305
* 创建日期:2006-5-16
* @param index
* @return
*/
public static Color getColor(int index) {
switch (index) {
case 0:
return Color.YELLOW;
case 1:
return Color.GREEN;
case 2:
return Color.RED;
case 3:
return Color.BLACK;
default:
return Color.BLACK;

}
}
 

/**
* 功能: 作者: shaokun305 创建日期:2006-5-16
*
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根

}



/**
* 画一组图形元素和字母标注
* 功能:
* 作者: shaokun305
* 创建日期:2006-5-16
* @param g
* @param rooundlist
*/
public void drawRounds(Graphics g,List rooundlist) {
Color color=g.getColor();
Font font=g.getFont();

if(rooundlist!=null) {
for (int i = 0; i < rooundlist.size(); i++) {
RoundData roundData = (RoundData) rooundlist.get(i);
drawRound(g,roundData,i);
}
}
//还原初始化的数据。
g.setColor(color);
g.setFont(font);
}

/**
*
* 功能: 画一个图形和对应的字母标注
* 作者: shaokun305
* 创建日期:2006-5-16
* @param g
* @param roundData
* @param color
*/
private void drawRound(Graphics g,RoundData rd,int index) {
g.setColor(getColor(index));
g.drawArc(rd.getPointX(),rd.getPointY(),rd.getRadii(),rd.getRadii(),0,360);
g.fillArc(rd.getPointX(),rd.getPointY(),rd.getRadii(),rd.getRadii(),0,360);

g.setColor(Color.BLACK);
g.drawString(rd.getLetter(),rd.getPointX()-10,rd.getPointY()+10);

}

}

 

本文地址:http://www.45fan.com/dnjc/72509.html
Tags: 图片 操作 Java
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部