/**
* 上传图片
* @return path 图片物理地址
* */
public String uploadImage(HttpServletRequest request){
String returnPath="/images/upload/temp/";
String path = config.getString(Constants.UPLOAD_PHOTO_ROOT_PATH)+returnPath;
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
if(multipartResolver.isMultipart(request))
{
//判断 request 是否有文件上传,即多部分请求...
MultipartHttpServletRequest multiRequest = multipartResolver.resolveMultipart(request);
Iterator<String> iter = multiRequest.getFileNames();
while(iter.hasNext()){
MultipartFile file = multiRequest.getFile(iter.next());
String fileName = System.currentTimeMillis() + "_lover_upload.jpg";
path =path+fileName;
returnPath = returnPath+fileName;
File localFile = new File(path);
logger.info("upload image path="+path);
try {
file.transferTo(localFile);
}catch (Exception e) {
e.printStackTrace();
}
}
}
return returnPath;
}
/**
* 合成图片
* @param pathOne 合成基础图片的工程相对路径
* @param pathTwo
* @param releticePath 生成新图片的相对位置
* @param x 相对主图片x轴偏移量
* @param y 相对于主图片y轴偏移量
* @return String 合成新图片的相对地址
* */
public String combinImage(String pathOne,String pathTwo,String reletivePath,int x,int y){
String rootPath = config.getString(Constants.UPLOAD_PHOTO_ROOT_PATH);
String returnPath = "";//返回的地址
try
{
//读取第一张图片
File fileOne = new File(rootPath+pathOne);
BufferedImage imageOne = ImageIO.read(fileOne);
int imageOneWidth = imageOne.getWidth();//图片宽度
int imageOneHeight = imageOne.getHeight();//图片高度
//从图片中读取RGB
int[] imageArrayOne = new int[imageOneWidth*imageOneHeight];
imageArrayOne = imageOne.getRGB(0,0,imageOneWidth,imageOneHeight,imageArrayOne,0,imageOneWidth);
//对第二张图片做相同的处理
File fileTwo = new File(rootPath+pathTwo);
BufferedImage imageTwo = ImageIO.read(fileTwo);
int width = imageTwo.getWidth();
int height = imageTwo.getHeight();
int[] imageArrayTwo = new int[width*height];
imageArrayTwo = imageTwo.getRGB(0,0,width,height,imageArrayTwo,0,width);
//生成新图片
BufferedImage imageNew = new BufferedImage(imageOneWidth,imageOneHeight,BufferedImage.TYPE_INT_RGB);
imageNew.setRGB(0,0,imageOneWidth,imageOneHeight,imageArrayOne,0,imageOneWidth);
imageNew.setRGB(x,y,width,height,imageArrayTwo,0,width);//第二张图片合成到第一张图片响应位置
String fileName = new Date().getTime() + ".jpg";
File outFile = new File(rootPath+reletivePath+File.separator+fileName);
ImageIO.write(imageNew, "jpg", outFile);//写图片
logger.info("create new image path = "+reletivePath+File.separator+fileName);
returnPath = reletivePath+fileName;
}catch(Exception e){
e.printStackTrace();
}
return returnPath;
}
/**
* 截图
* @param path 图片相对路径
* @param reletivePath 截图后保存的相对路径
* @param x 截取时相对于原图片的x轴便宜量
* @param y 相对于原图片的y轴偏移量
* @param width 截图宽度
* @param height 截图高度
* @param scrrenWidth 原图在页面显示时的宽度(带px)
* @param scrrenHeight 原图在页面显示时的高度
* @return String 截图后新图片的相对地址
* **/
public String[] screenImage(String path,String reletivePath,int x,int y,int width,int height,String screenWidth,String screenHeight){
String rootPath = config.getString(Constants.UPLOAD_PHOTO_ROOT_PATH);
String returnPath = "";
try {
File file = new File(rootPath+path);
BufferedImage image = ImageIO.read(file);
int image_width = image.getWidth();//图片宽度
int image_height = image.getHeight();//图片高度
//从图片中读取RGB
int[] imageArrayOne = new int[image_width*image_height];
//换算截图的位置
x = (x*100000/Integer.parseInt(screenWidth.replace("px", "")))*image_width/100000;
y = (y*100000/Integer.parseInt(screenHeight.replace("px", "")))*image_height/100000;
width = (width*100000/Integer.parseInt(screenWidth.replace("px", "")))*image_width/100000;
height = (height*100000/Integer.parseInt(screenHeight.replace("px", "")))*image_height/100000;
imageArrayOne = image.getRGB(x,y,width,height,imageArrayOne,0,width);
//生成新图片
BufferedImage ImageNew = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
ImageNew.setRGB(0,0,width,height,imageArrayOne,0,width);
Random r = new Random();
int random = r.nextInt(100000);
String fileName = System.currentTimeMillis()+"_"+random+".jpg";
File outFile = new File(rootPath+reletivePath+File.separator+fileName);
returnPath = reletivePath+"/"+fileName;
ImageIO.write(ImageNew, "jpg", outFile);
Runtime run = Runtime.getRuntime();//同步到静态 资源服务器
Process proccess = run.exec("/home/script/rsync_file.sh "+fileName);
proccess.waitFor();
if(outFile.exists()){
return new String[]{returnPath,"1"};
}
file.delete();
logger.info("screenImage success path = "+rootPath+reletivePath+File.separator+fileName);
} catch (Exception e) {
e.printStackTrace();
}
return new String[]{returnPath,"0"};
}
Spring MVC 上传图片、图片合成、截图service
最新推荐文章于 2023-01-13 11:45:53 发布