首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >生成缩略图并用颜色填充空白区域

生成缩略图并用颜色填充空白区域
EN

Stack Overflow用户
提问于 2013-07-21 21:55:50
回答 2查看 1.2K关注 0票数 1

有没有可能用Scalr实现first example

我的代码如下:

代码语言:javascript
复制
BufferedImage thumbnail = Scalr.resize(ImageIO.read(sourceFile), Scalr.Method.ULTRA_QUALITY, Scalr.Mode.FIT_TO_WIDTH,
                width, height, Scalr.OP_ANTIALIAS);
ImageIO.write(thumbnail, destinationfile.getExtension(), destinationfile);

我想要的是像这样接收图像:

其中蓝色条是我想用颜色填充的空间。

谢谢

更新:也许可以用Thumbnailator实现

EN

回答 2

Stack Overflow用户

发布于 2013-07-24 19:09:28

没有人知道,所以我将发布我的解决方案……我决定继续使用Scalr (我没有检查Thumbnailator的最新版本,但之前的版本在大图片上失败了)。所以首先我调用resize方法,然后,如果新缩略图的尺寸比给定的要大,我调用crop方法,它从中心裁剪一个缩略图。代码如下:

代码语言:javascript
复制
BufferedImage thumbnail = Scalr.resize(sourceFile, Scalr.Method.ULTRA_QUALITY, Scalr.Mode.AUTOMATIC, destinationSize.width, destinationSize.height);
if (thumbnail.getWidth() > destinationSize.width)
    thumbnail = Scalr.crop(thumbnail, (thumbnail.getWidth() - destinationSize.width) / 2, 0, destinationSize.width, destinationSize.height);
else if (thumbnail.getHeight() > destinationSize.height) 
    thumbnail = Scalr.crop(thumbnail, 0, (thumbnail.getHeight() - destinationSize.height) / 2, destinationSize.width, destinationSize.height);

它并不理想,但至少它能处理生成缩略图后的“宽”图像

票数 1
EN

Stack Overflow用户

发布于 2018-05-14 21:07:05

刚做完!也许它可以帮助你!

代码语言:javascript
复制
public static BufferedImage resizeAndCrop(BufferedImage bufferedImage) throws IOException {

        int himg = bufferedImage.getHeight();
        int wimg = bufferedImage.getWidth();

        double rateh = himg/dim;
        double ratew = wimg/dim;
        double rate = ratew;
        if(rateh>ratew)
        rate = rateh;

        int dimhimg = (int) (himg/rate);
        int dimwimg = (int) (wimg/rate);

        double startw = dim/2 - dimwimg/2;
        double starth = dim/2 - dimhimg/2;

        BufferedImage tThumbImage = new BufferedImage( dim, dim, BufferedImage.TYPE_INT_RGB );
        Graphics2D tGraphics2D = tThumbImage.createGraphics(); //create a graphics object to paint to
        tGraphics2D.setBackground( Color.WHITE );
        tGraphics2D.setPaint( Color.WHITE );
        tGraphics2D.fillRect( 0, 0, dim, dim );
        tGraphics2D.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        tGraphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,  RenderingHints.VALUE_ANTIALIAS_ON);
        tGraphics2D.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING,  RenderingHints.VALUE_COLOR_RENDER_QUALITY); 
        tGraphics2D.drawImage( bufferedImage, (int)startw, (int)starth, dimwimg, dimhimg, null ); //draw the image scaled

        File ff = new File(path + "jdata/tmp/prova.jpg");
        ImageIO.write( tThumbImage, "JPG", ff); //write the image to a file
        BufferedImage croppedContainMethod = ImageIO.read(ff);
        return croppedContainMethod; 

}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17773072

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档