Java对指定位置进行图片切割

更新时间:2023-06-30 09:43:43 阅读: 评论:0

Java对指定位置进⾏图⽚切割
protected void processRequest(HttpServletRequest request,
HttpServletRespon respon) throws ServletException, IOException {
String Pic = (String) Parameter("p");
int PointX = Integer.Parameter("x"));
int PointY = Integer.Parameter("y"));
int CutWidth = Integer.Parameter("w"));
int CutHeight = Integer.Parameter("h"));
// 图⽚宽度
int PicWidth = Integer.Parameter("pw"));
// 图⽚⾼度
int PicHeight = Integer.Parameter("ph"));
respon.tContentType("image/jpeg");
respon.tHeader("Pragma", "No-cache");
respon.tHeader("Cache-Control", "no-cache");
respon.tDateHeader("Expires", 0);
HttpSession ssion = Session();
String picPath = Pic;
String path = RealPath(picPath);
ServletOutputStream responOutputStream = OutputStream();
cut(path, responOutputStream, CutWidth, CutHeight, PointX, PointY,
PicWidth, PicHeight);
/
/ 以下关闭输⼊流!
responOutputStream.flush();
responOutputStream.clo();
}
/**
* 图⽚切割
* @param srcImageFile 图⽚地址
* @param responOutputStream rvlet输出流
* @param w 切割宽度
* @param h 切割⾼度
* @param x1 开始x结点(left)
* @param y1 开始y结点(top)
* @param sw 图⽚宽度
* @param sh 图⽚⾼度
*/
public void cut(String srcImageFile,ServletOutputStream responOutputStream, int w, int h, int x1,
int y1, int sw, int sh) {
try {
// localhost:8080/ImpCra/createServlet?p=Sunt.jpg&x=117&y=201&w=61&h=50&pw=300&ph=400 Image img;
ImageFilter cropFilter;
// 读取源图像
BufferedImage bi = ad(new File(srcImageFile));
if (sw >= w && sh >= h) {
Image image = bi.getScaledInstance(sw, sh, Image.SCALE_DEFAULT);
// 剪切起始坐标点
int x = x1;
int y = y1;
int destWidth = w; // 切⽚宽度
int destHeight = h; // 切⽚⾼度
// 图⽚⽐例
double pw = sw;
double ph = sh;
double m = (double) sw / pw;
double n = (double) sh / ph;
int wth = (int) (destWidth * m);
int hth = (int) (destHeight * n);
int xx = (int) (x * m);
int yy = (int) (y * n);
// 四个参数分别为图像起点坐标和宽⾼
// 即: CropImageFilter(int x,int y,int width,int height)
cropFilter = new CropImageFilter(xx, yy, wth, hth);
img = DefaultToolkit().createImage(
new Source(), cropFilter));
BufferedImage tag = new BufferedImage(w, h,
BufferedImage.TYPE_INT_RGB);
Graphics g = Graphics();
g.drawImage(img, 0, 0, null); // 绘制缩⼩后的图
g.dispo();
// 输出为⽂件
ImageIO.write(tag, "JPEG", responOutputStream);
}
} catch (Exception e) {
e.printStackTrace();
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="www.w3/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; chart=UTF-8" />
<title>JavaScript 图⽚切割效果(带拖放、缩放效果) </title>
</head>
<body>
<style type="text/css">
#rRightDown,#rLeftDown,#rLeftUp,#rRightUp,#rRight,#rLeft,#rUp,#rDown{position:absolute;background:#F00;width:5px; height:5px; z-index:500; font-size:0;}
#dragDiv{border:1px solid #000000;}
深圳市人才补贴
</style>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="50%">
<div id="bgDiv" >
<div id="dragDiv">
<div id="rRightDown" > </div>
<div id="rLeftDown" > </div>
<div id="rRightUp" > </div>
<div id="rLeftUp" > </div>
<div id="rRight" > </div>
<div id="rLeft" > </div>
<div id="rUp" > </div>
<div id="rDown" ></div>
</div>
</div>
</td>
黄伟文歌词<td><div id="viewDiv" > </div></td>
</tr>
</table>
<script>
var $ = function (id) {
return "string" == typeof id ? ElementById(id) : id;
};
var isIE = (document.all) ? true : fal;
function addEventHandler(oTarget, sEventType, fnHandler) {
if (oTarget.addEventListener) {
oTarget.addEventListener(sEventType, fnHandler, fal);
} el if (oTarget.attachEvent) {
oTarget.attachEvent("on" + sEventType, fnHandler);
} el {
oTarget["on" + sEventType] = fnHandler;
}
};
function removeEventHandler(oTarget, sEventType, fnHandler) {
if (veEventListener) {
} el if (oTarget.detachEvent) {
oTarget.detachEvent("on" + sEventType, fnHandler);
} el {
oTarget["on" + sEventType] = null;
}
};
var Class = {
create: function() {
return function() {
this.initialize.apply(this, arguments);
}
}
}
红掌可以晒太阳吗for (var property in source) {
destination[property] = source[property];
}
return destination;
}
//拖放程序
var Drag = ate();
Drag.prototype = {
//拖放对象,触发对象
initialize: function(obj, drag, options) {
var oThis = this;
this._obj = $(obj);//拖放对象
this.Drag = $(drag);//触发对象
this._x = this._y = 0;//记录⿏标相对拖放对象的位置
//事件对象(⽤于移除事件)
this._fM = function(e){ oThis.Move(window.event || e); }
this._fS = function(){ oThis.Stop(); }
this.SetOptions(options);
this.Limit = !!this.options.Limit;
this._obj.style.position = "absolute";
addEventHandler(this.Drag, "moudown", function(e){ oThis.Start(window.event || e); }); },
/
/设置默认属性
SetOptions: function(options) {
this.options = {//默认值
Limit: fal,//是否设置限制(为true时下⾯参数有⽤,可以是负数)
mxLeft: 0,//左边限制两相欢
mxRight: 0,//右边限制
mxTop: 0,//上边限制
mxBottom: 0,//下边限制
onMove: function(){}//移动时执⾏
};
},
//准备拖动
Start: function(oEvent) {
//记录⿏标相对拖放对象的位置
this._x = oEvent.clientX - this._obj.offtLeft;
this._y = oEvent.clientY - this._obj.offtTop;
//moumove时移动 mouup时停⽌
addEventHandler(document, "moumove", this._fM);彩云追月原唱
addEventHandler(document, "mouup", this._fS);
//使⿏标移到窗⼝外也能释放
if(isIE){
见利忘义的近义词
addEventHandler(this.Drag, "locapture", this._fS);
this.Drag.tCapture();
}el{
addEventHandler(window, "blur", this._fS);
}
},
//拖动
Move: function(oEvent) {
//清除选择(ie设置捕获后默认带这个)
//当前⿏标位置减去相对拖放对象的位置得到offt位置
var iLeft = oEvent.clientX - this._x, iTop = oEvent.clientY - this._y;
//设置范围限制
if(this.Limit){
//获取超出长度
var iRight = iLeft + this._obj.offtWidth - Right, iBottom = iTop + this._obj.offtHeight - Bottom; //这⾥是先设置右边下边再设置左边上边,可能会不准确
if(iRight > 0) iLeft -= iRight;
if(iBottom > 0) iTop -= iBottom;
赤潮是什么Left > iLeft) iLeft = Left;
Top > iTop) iTop = Top;
}
//设置位置
this._obj.style.left = iLeft + "px";
this._p = iTop + "px";
//附加程序
},
//停⽌拖动
Stop: function() {
//移除事件
removeEventHandler(document, "moumove", this._fM);
removeEventHandler(document, "mouup", this._fS);
if(isIE){
红石斑鱼的做法
removeEventHandler(this.Drag, "locapture", this._fS);
leaCapture();
}el{
removeEventHandler(window, "blur", this._fS);
}
}
};
//缩放程序
var Resize = ate();
Resize.prototype = {
//缩放对象
initialize: function(obj, options) {
var oThis = this;
this._obj = $(obj);//缩放对象

本文发布于:2023-06-30 09:43:43,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1069973.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:设置   拖放   对象
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图