java 图片缩放处理

java 图片缩放处理

最近接到一个图片处理的需求:是需要将图片进行剪裁,并存入数据库。

最后用到的是Thumbnailator这个java类库,下载地址:http://code.google.com/p/thumbnailator/ 
/**
     * 对照片进行像素调整,调整到114*114
     * @param originPicture 原始的照片输入流
     * @return 修改过后的
     */
    private InputStream fixPicture(InputStream originPicture){
        InputStream picture =null;

        BufferedImage tmpImage ;
        try {
            BufferedImage image = Thumbnails.of(originPicture)
                    .scale(1)
                    .asBufferedImage();

            int width =image.getWidth();
            int height =image.getHeight();


            if(width>height){
                tmpImage =Thumbnails.of(image)
                        .sourceRegion(Positions.CENTER, height, height)
                        .size(114, 114)
                        .keepAspectRatio(false)
                        .asBufferedImage();

            }else {
                tmpImage =Thumbnails.of(image)
                        .sourceRegion(Positions.TOP_LEFT, width, width)
                        .size(114, 114)
                        .keepAspectRatio(false)
                        .asBufferedImage();
            }


            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ImageIO.write(tmpImage, "jpg", os);
            picture= new ByteArrayInputStream(os.toByteArray());

        } catch (IOException e) {
            e.printStackTrace();
        }

        return picture;
    }
总体来说: Thumbnailator来处理图片比较方便,本例中主要是需要将图片格式存成inputstream流,用以存到数据库中,
格式转换,略微坑爹:bufferedImage->inputStream。
比较详细的使用,可以参照以下链接:
http://blog.csdn.net/zxingchao2009/article/details/7621197
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值