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

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 ;
    }




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值