对图片进行缩放剪切
- public String cut()
- {
- String fileName = getRequest().getParameter("fileName" ); //图片路径+图片名称
- String txt_width = getRequest().getParameter("txt_width" ); //缩放后宽
- String txt_height = getRequest().getParameter("txt_height" ); //缩放后高
- String txt_top = getRequest().getParameter("txt_top" ); //x坐标
- String txt_left = getRequest().getParameter("txt_left" ); //y坐标
- String txt_DropWidth = getRequest().getParameter("txt_DropWidth" ); //剪切宽
- String txt_DropHeight = getRequest().getParameter("txt_DropHeight" ); //剪切高
- String filePath = getRequest().getSession().getServletContext().getRealPath("/" )+ "attachFiles\\" +fileName;
- String cutFilePath = getRequest().getSession().getServletContext().getRealPath("/" )+ "attachFiles\\cut\\" +fileName;
- File fileList = new File(getRequest().getSession().getServletContext().getRealPath( "/" )+ "attachFiles\\cut\\" );
- if (!fileList.exists())
- {
- fileList.mkdirs();
- }
- try {
- System.setProperty("jmagick.systemclassloader" , "no" );
- ImageInfo info = new ImageInfo(filePath);
- MagickImage image = new MagickImage(info);
- MagickImage cropped = null ;
- MagickImage scaleImg = image.scaleImage(Integer.valueOf(txt_width), Integer.valueOf(txt_height));//缩放图片
- Rectangle rect = new Rectangle (Integer.valueOf(txt_left),Integer.valueOf(txt_top),Integer.valueOf(txt_DropWidth),Integer.valueOf(txt_DropHeight));
- cropped = scaleImg.cropImage(rect);//剪切图片
- cropped.setFileName(cutFilePath);
- cropped .writeImage(info);
- getRequest().setAttribute("cutImagePath" , getRequest().getContextPath()+ "/attachFiles/cut/" +fileName);
- } catch (MagickException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return "cut" ;
- }
public String cut()
{
String fileName = getRequest().getParameter("fileName");//图片路径+图片名称
String txt_width = getRequest().getParameter("txt_width");//缩放后宽
String txt_height = getRequest().getParameter("txt_height");//缩放后高
String txt_top = getRequest().getParameter("txt_top");//x坐标
String txt_left = getRequest().getParameter("txt_left");//y坐标
String txt_DropWidth = getRequest().getParameter("txt_DropWidth");//剪切宽
String txt_DropHeight = getRequest().getParameter("txt_DropHeight");//剪切高
String filePath = getRequest().getSession().getServletContext().getRealPath("/")+"attachFiles\\"+fileName;
String cutFilePath = getRequest().getSession().getServletContext().getRealPath("/")+"attachFiles\\cut\\"+fileName;
File fileList = new File(getRequest().getSession().getServletContext().getRealPath("/")+"attachFiles\\cut\\");
if(!fileList.exists())
{
fileList.mkdirs();
}
try {
System.setProperty("jmagick.systemclassloader","no");
ImageInfo info = new ImageInfo(filePath);
MagickImage image = new MagickImage(info);
MagickImage cropped = null;
MagickImage scaleImg = image.scaleImage(Integer.valueOf(txt_width), Integer.valueOf(txt_height));//缩放图片
Rectangle rect = new Rectangle (Integer.valueOf(txt_left),Integer.valueOf(txt_top),Integer.valueOf(txt_DropWidth),Integer.valueOf(txt_DropHeight));
cropped = scaleImg.cropImage(rect);//剪切图片
cropped.setFileName(cutFilePath);
cropped .writeImage(info);
getRequest().setAttribute("cutImagePath", getRequest().getContextPath()+"/attachFiles/cut/"+fileName);
} catch (MagickException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "cut";
}
对图片进行缩放剪切并添加水印
- public String watermark()
- {
- String fileName = getRequest().getParameter("fileName" ); //图片路径+名字
- String logoFileName = getRequest().getParameter("logoFileName" ); //水印图片历经+名字
- Integer txt_width = Integer.valueOf(getRequest().getParameter("txt_width" )); //缩放后宽
- Integer txt_height = Integer.valueOf(getRequest().getParameter("txt_height" )); //缩放后高
- Integer txt_top = Integer.valueOf(getRequest().getParameter("txt_top" )); //x坐标
- Integer txt_left = Integer.valueOf(getRequest().getParameter("txt_left" )); //y坐标
- Integer txt_DropWidth = Integer.valueOf(getRequest().getParameter("txt_DropWidth" )); //剪切宽度
- Integer txt_DropHeight = Integer.valueOf(getRequest().getParameter("txt_DropHeight" )); //剪切高度
- String filePath = getRequest().getSession().getServletContext().getRealPath("/" )+ "attachFiles\\" +fileName;
- String logoImagPath = getRequest().getSession().getServletContext().getRealPath("/" )+ "attachFiles\\logo\\" +logoFileName;
- String flag = getRequest().getParameter("flag" );
- String cutFilePath = getRequest().getSession().getServletContext().getRealPath("/" )+ "attachFiles\\cut\\" +fileName;
- File fileList = new File(getRequest().getSession().getServletContext().getRealPath( "/" )+ "attachFiles\\cut\\" );
- if (!fileList.exists())
- {
- fileList.mkdirs();
- }
- try {
- System.setProperty("jmagick.systemclassloader" , "no" );
- ImageInfo info = new ImageInfo(filePath);
- MagickImage image = new MagickImage(info);
- MagickImage cropped = null ;
- MagickImage fLogo = null ;
- MagickImage sLogo = null ;
- Dimension logoDim = null ;
- MagickImage scaleImg = image.scaleImage(txt_width, txt_height);
- Rectangle rect = new Rectangle (txt_left,txt_top,txt_DropWidth,txt_DropHeight);
- cropped = scaleImg.cropImage(rect);
- fLogo = new MagickImage( new ImageInfo(logoImagPath));
- logoDim = fLogo.getDimension();
- int lw = txt_DropWidth / 4 ;
- int lh = logoDim.height * lw / logoDim.width;
- sLogo = fLogo.scaleImage(lw, lh);
- //水印出现在左上方
- if (flag.equals( "leftTop" ))
- {
- cropped.compositeImage(CompositeOperator.AtopCompositeOp, sLogo,
- lh / 10 , lh / 10 );
- }
- else if (flag.equals( "rightTop" ))
- {
- cropped.compositeImage(CompositeOperator.AtopCompositeOp, sLogo,
- txt_DropWidth - (lw + lh / 10 ), lh / 10 );
- }
- else if (flag.equals( "middle" ))
- {
- cropped.compositeImage(CompositeOperator.AtopCompositeOp, sLogo,
- (txt_DropWidth - lw)/2 , (txt_DropHeight-lh)/ 2 );
- }
- else if (flag.equals( "leftBottom" ))
- {
- cropped.compositeImage(CompositeOperator.AtopCompositeOp, sLogo,
- lh / 10 , txt_DropHeight - (lh + lh / 10 ));
- }
- else
- {
- cropped.compositeImage(CompositeOperator.AtopCompositeOp, sLogo,
- txt_DropWidth - (lw + lh / 10 ), txt_DropHeight - (lh + lh / 10 ));
- }
- cropped.setFileName(cutFilePath);
- cropped.writeImage(info);
- getRequest().setAttribute("cutImagePath" , getRequest().getContextPath()+ "/attachFiles/cut/" +fileName);
- } catch (MagickException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return "cut" ;
- }
public String watermark()
{
String fileName = getRequest().getParameter("fileName");//图片路径+名字
String logoFileName = getRequest().getParameter("logoFileName");//水印图片历经+名字
Integer txt_width = Integer.valueOf(getRequest().getParameter("txt_width"));//缩放后宽
Integer txt_height = Integer.valueOf(getRequest().getParameter("txt_height"));//缩放后高
Integer txt_top = Integer.valueOf(getRequest().getParameter("txt_top"));//x坐标
Integer txt_left = Integer.valueOf(getRequest().getParameter("txt_left"));//y坐标
Integer txt_DropWidth = Integer.valueOf(getRequest().getParameter("txt_DropWidth"));//剪切宽度
Integer txt_DropHeight = Integer.valueOf(getRequest().getParameter("txt_DropHeight"));//剪切高度
String filePath = getRequest().getSession().getServletContext().getRealPath("/")+"attachFiles\\"+fileName;
String logoImagPath = getRequest().getSession().getServletContext().getRealPath("/")+"attachFiles\\logo\\"+logoFileName;
String flag = getRequest().getParameter("flag");
String cutFilePath = getRequest().getSession().getServletContext().getRealPath("/")+"attachFiles\\cut\\"+fileName;
File fileList = new File(getRequest().getSession().getServletContext().getRealPath("/")+"attachFiles\\cut\\");
if(!fileList.exists())
{
fileList.mkdirs();
}
try {
System.setProperty("jmagick.systemclassloader","no");
ImageInfo info = new ImageInfo(filePath);
MagickImage image = new MagickImage(info);
MagickImage cropped = null;
MagickImage fLogo = null;
MagickImage sLogo = null;
Dimension logoDim = null;
MagickImage scaleImg = image.scaleImage(txt_width, txt_height);
Rectangle rect = new Rectangle (txt_left,txt_top,txt_DropWidth,txt_DropHeight);
cropped = scaleImg.cropImage(rect);
fLogo = new MagickImage(new ImageInfo(logoImagPath));
logoDim = fLogo.getDimension();
int lw = txt_DropWidth / 4;
int lh = logoDim.height * lw / logoDim.width;
sLogo = fLogo.scaleImage(lw, lh);
//水印出现在左上方
if(flag.equals("leftTop"))
{
cropped.compositeImage(CompositeOperator.AtopCompositeOp, sLogo,
lh / 10, lh / 10);
}
else if(flag.equals("rightTop"))
{
cropped.compositeImage(CompositeOperator.AtopCompositeOp, sLogo,
txt_DropWidth - (lw + lh / 10), lh / 10);
}
else if(flag.equals("middle"))
{
cropped.compositeImage(CompositeOperator.AtopCompositeOp, sLogo,
(txt_DropWidth - lw)/2, (txt_DropHeight-lh)/2);
}
else if(flag.equals("leftBottom"))
{
cropped.compositeImage(CompositeOperator.AtopCompositeOp, sLogo,
lh / 10, txt_DropHeight - (lh + lh / 10));
}
else
{
cropped.compositeImage(CompositeOperator.AtopCompositeOp, sLogo,
txt_DropWidth - (lw + lh / 10), txt_DropHeight - (lh + lh / 10));
}
cropped.setFileName(cutFilePath);
cropped.writeImage(info);
getRequest().setAttribute("cutImagePath", getRequest().getContextPath()+"/attachFiles/cut/"+fileName);
} catch (MagickException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "cut";
}
锐化图片
- //锐化
- public void sharpen(String filePath,String savePath)
- {
- ImageInfo info = null ;
- MagickImage image = null ;
- MagickImage sharpened = null ;
- try {
- info = new ImageInfo(filePath);
- image = new MagickImage(info);
- sharpened = image.sharpenImage(1.0 , 5.0 );
- sharpened.setFileName(savePath);
- sharpened.writeImage(info);
- } catch (MagickException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- finally {
- if (image != null ) {
- image.destroyImages();
- }
- }
- }
//锐化
public void sharpen(String filePath,String savePath)
{
ImageInfo info = null;
MagickImage image = null;
MagickImage sharpened = null;
try {
info = new ImageInfo(filePath);
image = new MagickImage(info);
sharpened = image.sharpenImage(1.0, 5.0);
sharpened.setFileName(savePath);
sharpened.writeImage(info);
} catch (MagickException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
if (image != null) {
image.destroyImages();
}
}
}
最后:不要忘了这句话
- System.setProperty( "jmagick.systemclassloader" , "no" );