栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Java中2d图形中调整缓冲图像的大小?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Java中2d图形中调整缓冲图像的大小?

public BufferedImage scaleImage(BufferedImage img, int width, int height,        Color background) {    int imgWidth = img.getWidth();    int imgHeight = img.getHeight();    if (imgWidth*height < imgHeight*width) {        width = imgWidth*height/imgHeight;    } else {        height = imgHeight*width/imgWidth;    }    BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);    Graphics2D g = newImage.createGraphics();    try {        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,     RenderingHints.VALUE_INTERPOLATION_BICUBIC);        g.setBackground(background);        g.clearRect(0, 0, width, height);        g.drawImage(img, 0, 0, width, height, null);    } finally {        g.dispose();    }    return newImage;}

更新:不同的算法

import java.awt.Graphics2D;import java.awt.Image;import java.awt.RenderingHints;import java.awt.image.BufferedImage;public enum Resizer {    NEAREST_NEIGHBOR {        @Override        public BufferedImage resize(BufferedImage source,     int width, int height) { return commonResize(source, width, height,         RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);        }    },    BILINEAR {        @Override        public BufferedImage resize(BufferedImage source,     int width, int height) { return commonResize(source, width, height,         RenderingHints.VALUE_INTERPOLATION_BILINEAR);        }    },    BICUBIC {        @Override        public BufferedImage resize(BufferedImage source,     int width, int height) { return commonResize(source, width, height,         RenderingHints.VALUE_INTERPOLATION_BICUBIC);        }    },    PROGRESSIVE_BILINEAR {        @Override        public BufferedImage resize(BufferedImage source,     int width, int height) { return progressiveResize(source, width, height,         RenderingHints.VALUE_INTERPOLATION_BILINEAR);        }    },    PROGRESSIVE_BICUBIC {        @Override        public BufferedImage resize(BufferedImage source,     int width, int height) { return progressiveResize(source, width, height,         RenderingHints.VALUE_INTERPOLATION_BICUBIC);        }    },    AVERAGE {        @Override        public BufferedImage resize(BufferedImage source,     int width, int height) { Image img2 = source.getScaledInstance(width, height,         Image.SCALE_AREA_AVERAGING); BufferedImage img = new BufferedImage(width, height,         source.getType()); Graphics2D g = img.createGraphics(); try {     g.drawImage(img2, 0, 0, width, height, null); } finally {     g.dispose(); } return img;        }    };    public abstract BufferedImage resize(BufferedImage source, int width, int height);    private static BufferedImage progressiveResize(BufferedImage source, int width, int height, Object hint) {        int w = Math.max(source.getWidth()/2, width);        int h = Math.max(source.getHeight()/2, height);        BufferedImage img = commonResize(source, w, h, hint);        while (w != width || h != height) { BufferedImage prev = img; w = Math.max(w/2, width); h = Math.max(h/2, height); img = commonResize(prev, w, h, hint); prev.flush();        }        return img;    }    private static BufferedImage commonResize(BufferedImage source, int width, int height, Object hint) {        BufferedImage img = new BufferedImage(width, height,     source.getType());        Graphics2D g = img.createGraphics();        try { g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint); g.drawImage(source, 0, 0, width, height, null);        } finally { g.dispose();        }        return img;    }};


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/573779.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号