您的位置:首页 > 其它

按照给定尺寸进行图片的缩放

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 ;

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: