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

你可能感兴趣的:(Java,Thumbnailator,java,图片处理)