一般上传图片分三步:
第一步:将原始图片上传到服务器端保存,不妨命名为src.jpg
第二步:在浏览器端将src.jpg显示出来,然后使用jQuery,对其进行“客户端剪切”。
所谓客户端剪切就是根据用户在界面中对原始图片放大,移动,剪切时,获得一些参数。
具体需要六个参数(srcWidth, srcHeight, rect.x, rect.y , rect.width, rect.height)参数。
其中,srcWidth 和srcHeight表明原始图片在客户端放大后的宽和高
rect.x和rect.y表明剪切部分相对(srcWidth, srcHeight)的起始坐标
rect.width和rect.height表示需要剪切的图片的大小。
第三步:获取第二步得到的参数,对内存中原始图片先进行预处理(按照srcWidth和srcHeight进行缩放)、剪切。
然后对预处理后的内存图像剪切,打印出来。
第一步实现比较简单,第二步需要学习jQuery,网上例子很多。我们重点讨论第三步, java切图。
1.main函数
- packagesyj.main;
- importjava.awt.Rectangle;
- importjava.io.File;
- importjava.io.IOException;
- importsyj.util.ImageHepler;
- publicclassTest{
- publicstaticvoidmain(String[]args)throwsIOException{
- StringpicPath="img/src.jpg";
- StringdestPath="img/result.jpg";
- Rectanglerect=newRectangle(0,0,2000,1200);
- ImageHepler.cut(picPath,destPath,1440,900,rect);
- }
- }
2.关键图像操作函数
- packagesyj.util;
- importjava.awt.Color;
- importjava.awt.Graphics;
- importjava.awt.Image;
- importjava.awt.Rectangle;
- importjava.awt.image.BufferedImage;
- importjava.io.File;
- importjava.io.IOException;
- importjava.io.PrintStream;
- importjavax.imageio.ImageIO;
- publicclassImageHepler{
- /**
- *@Description:将srcImageFile裁剪后生成destImageFile
- *@paramsrcImageFile原始图
- *@paramdestImageFile目标图
- *@paramwidth原始图预处理后width
- *@paramheight原始图预处理后height
- *@paramrect目标图输出的格式(rect.x,rect.y,rect.width,rect.height)
- *@throwsIOException
- *@authorSunYanjun
- */
- publicstaticvoidcut(StringsrcImageFile,StringdestImageFile,intwidth,intheight,Rectanglerect)throwsIOException{
- Imageimage=ImageIO.read(newFile(srcImageFile));
- BufferedImagebImage=makeThumbnail(image,width,height);
- //把原始图片输出
- ImageIO.write(bImage,"jpg",newFile("img/src2.jpg"));
- saveSubImage(bImage,rect,newFile(destImageFile));
- }
- /**
- *@Description:将srcImageFile裁剪后生成destImageFile
- *@paramsrcImageFile原始图
- *@paramdestImageFile目标图
- *@paramwidth原始图预处理后width
- *@paramheight原始图预处理后height
- *@paramrect目标图输出的格式(rect.x,rect.y,rect.width,rect.height)
- *@throwsIOException
- *@authorSunYanjun
- */
- publicstaticvoidcut(FilesrcImageFile,FiledestImageFile,intwidth,intheight,Rectanglerect)throwsIOException{
- Imageimage=ImageIO.read(srcImageFile);
- BufferedImagebImage=makeThumbnail(image,width,height);
- saveSubImage(bImage,rect,destImageFile);
- }
- /**
- *@Description:对原始图片根据(x,y,width,height)=(0,0,width,height)进行缩放,生成BufferImage
- *@paramimg
- *@paramwidth预处理后图片的宽度
- *@paramheight预处理后图片高度
- *@return
- *@authorSunYanjun
- *@throwsIOException
- */
- privatestaticBufferedImagemakeThumbnail(Imageimg,intwidth,intheight)throwsIOException{
- BufferedImagetag=newBufferedImage(width,height,1);
- Graphicsg=tag.getGraphics();
- g.drawImage(img.getScaledInstance(width,height,4),0,0,null);
- g.dispose();
- returntag;
- }
- /**
- *@Description:对BufferImage按照(x,y,width,height)=(subImageBounds.x,subImageBounds.y,subImageBounds.width,subImageBounds.height)进行裁剪
- *如果subImageBounds范围过大,则用空白填充周围的区域。
- *
- *@paramimage
- *@paramsubImageBounds
- *@paramdestImageFile
- *@throwsIOException
- *@authorSunYanjun
- */
- privatestaticvoidsaveSubImage(BufferedImageimage,RectanglesubImageBounds,FiledestImageFile)throwsIOException{
- StringfileName=destImageFile.getName();
- StringformatName=fileName.substring(fileName.lastIndexOf('.')+1);
- BufferedImagesubImage=newBufferedImage(subImageBounds.width,subImageBounds.height,1);
- Graphicsg=subImage.getGraphics();
- if((subImageBounds.width>image.getWidth())
- ||(subImageBounds.height>image.getHeight())){
- intleft=subImageBounds.x;
- inttop=subImageBounds.y;
- if(image.getWidth()<subImageBounds.width)
- left=(subImageBounds.width-image.getWidth())/2;
- if(image.getHeight()<subImageBounds.height)
- top=(subImageBounds.height-image.getHeight())/2;
- g.setColor(Color.white);
- g.fillRect(0,0,subImageBounds.width,subImageBounds.height);
- g.drawImage(image,left,top,null);
- ImageIO.write(image,formatName,destImageFile);
- }else{
- g.drawImage(image.getSubimage(subImageBounds.x,subImageBounds.y,
- subImageBounds.width,subImageBounds.height),0,0,null);
- }
- g.dispose();
- ImageIO.write(subImage,formatName,destImageFile);
- }
- }
BufferedImage subImage = new BufferedImage(subImageBounds.width, subImageBounds.height, BufferedImage.TYPE_INT_RGB);Graphics2D g = subImage.createGraphics();
// 解决图片背景透明问题--且只针对png
subImage = g.getDeviceConfiguration().createCompatibleImage(subImageBounds.width, subImageBounds.height, Transparency.TRANSLUCENT);
g.dispose();
g = subImage.createGraphics();