按照给定尺寸进行图片的缩放
2015-07-28 11:27
274 查看
1.PhotoController中的 /getPhoto 接口。
@RequestMapping("/getPhoto")
@ResponseBody
public void getPhoto(HttpServletResponse response,
@RequestParam(value = "photoId") String photoId,
@RequestParam(value = "width",required = false)Integer width,
@RequestParam(value = "height",required = false)Integer height) throws Exception{
if(logger.isDebugEnabled()){
logger.debug("--------------------getPhoto----------------------");
logger.debug("photoId = "+photoId);
logger.debug("width =" +width );
logger.debug("height = " +height);
}
byte[] bytes;
try{
if(null!= width && 0!=width && null! = height && 0!=height){
bytes = photoManager.getPhotoThumbnailBytes(Long.parseLong(photoId),width,height);
}else{
bytes = photoManager.getPhotoBytes(Long.parseLong(photoId));
}
}catch(IOException e){
e.printStackTrace();
logger.debug("获取图片异常");
throw new JsonObjectException("获取图片异常");
}catch (PhotoNotFoundException e){
e.printStackTrace();
logger.debug("图片不存在");
throw new JsonObjectException("图片不存在");
}catch (NumberFormatException e){
e.printStackTrace();
logger.debug("获取图片参数异常");
throw new JsonObjectException("获取图片参数异常");
}
String extensionName = "png";
response.setContentType("image/" + extensionName);
IOUtils.copy(new ByteArrayInputStream(bytes),response.getOutputStream());
}
2.PhotoManagerImpl 里的getPhotoThumbnailBytes和getPhotoBytes函数
public byte[] getPhotoBytes(Long photoId) throws IOException {
Photo photo=photoDao.get(photoId);
String photoDirFilename = getPathPhotoBaseDir(photo.getPath());
File photoFile = new File(photoDirFilename + File.separator + photoId);
return FileUtils.readFileToByteArray(photoFile);
}
public byte[] getPhotoThumbnailBytes(Long photoId, int width, int height) throws IOException, PhotoNotFoundException {
Photo photo=photoDao.get(photoId);
String photoDirFilename = getPathPhotoBaseDir(photo.getPath()) + File.separator + (width + "_" + height);
File photoFile = new File(photoDirFilename + File.separator + photoId);
if (photoFile.exists()) {
return FileUtils.readFileToByteArray(photoFile);
} else {
File originalPhotoFile = new File(getPathPhotoBaseDir(photo.getPath()) + File.separator + photoId);
if (originalPhotoFile.exists()) {
BufferedImage imgSrc = ImageIO.read(originalPhotoFile);
BufferedImage newImage = new BufferedImage(imgSrc.getWidth(), imgSrc.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
// for (int x = 0; x < imgSrc.getWidth(); x++) {
// for (int y = 0; y < imgSrc.getHeight(); y++) {
// newImage.setRGB(x, y, imgSrc.getRGB(x, y));
// }
// }
BufferedImage targetImg;
// while(width>imgSrc.getWidth()||height>imgSrc.getHeight()){
// width/=2;
// height/=2;
// }
// imgSrc=newImage;
// targetImg=newImage;
try {
if (imgSrc.getWidth()*height<width*imgSrc.getHeight()) {
targetImg=imgSrc;
// targetImg = Scalr.resize(imgSrc, Scalr.Method.ULTRA_QUALITY, Scalr.Mode.FIT_TO_WIDTH, width, height,
// Scalr.OP_ANTIALIAS);
height=height*imgSrc.getWidth()/width;
width=imgSrc.getWidth();
System.out.println("height="+height);
System.out.println("width="+width);
targetImg = targetImg.getSubimage(0, Math.abs(targetImg.getHeight()-height)/2, width, height);
} else {
targetImg=imgSrc;
// targetImg = Scalr.resize(imgSrc, Scalr.Method.ULTRA_QUALITY, Scalr.Mode.FIT_TO_HEIGHT, width, height,
// Scalr.OP_ANTIALIAS);
width=width*imgSrc.getHeight()/height;
height=imgSrc.getHeight();
System.out.println(width);
System.out.println(height);
targetImg = targetImg.getSubimage(Math.abs(targetImg.getWidth()-width)/2,0, width, height);
}
} catch (Exception e) {
targetImg=imgSrc;
e.printStackTrace();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// if (null!=photo.getMimeType()) {
// ImageIO.write(targetImg, photo.getMimeType(), baos);
// }else {
// ImageIO.write(targetImg, "png", baos);
// }
ImageIO.write(targetImg, "png", baos);
baos.flush();
byte[] imageInByte = baos.toByteArray();
baos.close();
FileUtils.writeByteArrayToFile(photoFile, imageInByte);
return imageInByte;
} else {
log.error("photo not found with id:" + photoId);
throw new PhotoNotFoundException("photo not found with id:" + photoId);
}
}
}
private String getPathPhotoBaseDir(String path) {
return photoBaseDir + File.separator + path ;
}
@RequestMapping("/getPhoto")
@ResponseBody
public void getPhoto(HttpServletResponse response,
@RequestParam(value = "photoId") String photoId,
@RequestParam(value = "width",required = false)Integer width,
@RequestParam(value = "height",required = false)Integer height) throws Exception{
if(logger.isDebugEnabled()){
logger.debug("--------------------getPhoto----------------------");
logger.debug("photoId = "+photoId);
logger.debug("width =" +width );
logger.debug("height = " +height);
}
byte[] bytes;
try{
if(null!= width && 0!=width && null! = height && 0!=height){
bytes = photoManager.getPhotoThumbnailBytes(Long.parseLong(photoId),width,height);
}else{
bytes = photoManager.getPhotoBytes(Long.parseLong(photoId));
}
}catch(IOException e){
e.printStackTrace();
logger.debug("获取图片异常");
throw new JsonObjectException("获取图片异常");
}catch (PhotoNotFoundException e){
e.printStackTrace();
logger.debug("图片不存在");
throw new JsonObjectException("图片不存在");
}catch (NumberFormatException e){
e.printStackTrace();
logger.debug("获取图片参数异常");
throw new JsonObjectException("获取图片参数异常");
}
String extensionName = "png";
response.setContentType("image/" + extensionName);
IOUtils.copy(new ByteArrayInputStream(bytes),response.getOutputStream());
}
2.PhotoManagerImpl 里的getPhotoThumbnailBytes和getPhotoBytes函数
public byte[] getPhotoBytes(Long photoId) throws IOException {
Photo photo=photoDao.get(photoId);
String photoDirFilename = getPathPhotoBaseDir(photo.getPath());
File photoFile = new File(photoDirFilename + File.separator + photoId);
return FileUtils.readFileToByteArray(photoFile);
}
public byte[] getPhotoThumbnailBytes(Long photoId, int width, int height) throws IOException, PhotoNotFoundException {
Photo photo=photoDao.get(photoId);
String photoDirFilename = getPathPhotoBaseDir(photo.getPath()) + File.separator + (width + "_" + height);
File photoFile = new File(photoDirFilename + File.separator + photoId);
if (photoFile.exists()) {
return FileUtils.readFileToByteArray(photoFile);
} else {
File originalPhotoFile = new File(getPathPhotoBaseDir(photo.getPath()) + File.separator + photoId);
if (originalPhotoFile.exists()) {
BufferedImage imgSrc = ImageIO.read(originalPhotoFile);
BufferedImage newImage = new BufferedImage(imgSrc.getWidth(), imgSrc.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
// for (int x = 0; x < imgSrc.getWidth(); x++) {
// for (int y = 0; y < imgSrc.getHeight(); y++) {
// newImage.setRGB(x, y, imgSrc.getRGB(x, y));
// }
// }
BufferedImage targetImg;
// while(width>imgSrc.getWidth()||height>imgSrc.getHeight()){
// width/=2;
// height/=2;
// }
// imgSrc=newImage;
// targetImg=newImage;
try {
if (imgSrc.getWidth()*height<width*imgSrc.getHeight()) {
targetImg=imgSrc;
// targetImg = Scalr.resize(imgSrc, Scalr.Method.ULTRA_QUALITY, Scalr.Mode.FIT_TO_WIDTH, width, height,
// Scalr.OP_ANTIALIAS);
height=height*imgSrc.getWidth()/width;
width=imgSrc.getWidth();
System.out.println("height="+height);
System.out.println("width="+width);
targetImg = targetImg.getSubimage(0, Math.abs(targetImg.getHeight()-height)/2, width, height);
} else {
targetImg=imgSrc;
// targetImg = Scalr.resize(imgSrc, Scalr.Method.ULTRA_QUALITY, Scalr.Mode.FIT_TO_HEIGHT, width, height,
// Scalr.OP_ANTIALIAS);
width=width*imgSrc.getHeight()/height;
height=imgSrc.getHeight();
System.out.println(width);
System.out.println(height);
targetImg = targetImg.getSubimage(Math.abs(targetImg.getWidth()-width)/2,0, width, height);
}
} catch (Exception e) {
targetImg=imgSrc;
e.printStackTrace();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// if (null!=photo.getMimeType()) {
// ImageIO.write(targetImg, photo.getMimeType(), baos);
// }else {
// ImageIO.write(targetImg, "png", baos);
// }
ImageIO.write(targetImg, "png", baos);
baos.flush();
byte[] imageInByte = baos.toByteArray();
baos.close();
FileUtils.writeByteArrayToFile(photoFile, imageInByte);
return imageInByte;
} else {
log.error("photo not found with id:" + photoId);
throw new PhotoNotFoundException("photo not found with id:" + photoId);
}
}
}
private String getPathPhotoBaseDir(String path) {
return photoBaseDir + File.separator + path ;
}
相关文章推荐
- Spring DI的3种方式
- 我的Android进阶之旅------>MIME类型大全
- 将一个字符串转换成数字
- MVC中Model的设计(刚刚)
- 使用agsXMPP SDK向Openfire发送文件的注意事项
- Maya Calendar(Poj1008
- 再看看maven--手动创建项目与自动创建项目
- jquery的each遍历
- poj2387
- Java IO学习【14】读取键盘录入
- C#设计模式(6)——原型模式(Prototype Pattern)
- iOS多线程的初步研究(四)-- NSTimer
- Js 冒泡事件阻止
- 键盘遮挡
- linux下搭建生成HLS所需的.ts和.m3u8文件
- progressbar 使用 Excel取数据
- hibernate如何连接数据库
- ubuntu14.04开机自动调节亮度
- ios 判断app是否为第一次启动
- JavaWeb学习——文件上传和下载