前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java:SWT 缩放图像(Image)

java:SWT 缩放图像(Image)

作者头像
10km
发布2019-05-25 22:29:22
1.1K0
发布2019-05-25 22:29:22
举报
文章被收录于专栏:10km的专栏10km的专栏

版权声明:本文为博主原创文章,转载请注明源地址。 https://cloud.tencent.com/developer/article/1433704

在SWT中下面两个方法都可以实现Image缩放,

GC.drawImage(Image image, int srcX, int srcY, int srcWidth, int srcHeight, int dstX, int dstY, int dstWidth, int dstHeight); ImageData.scaledTo(int width, int height)

但是为了保证缩放图像质量,还是用GC.drawImage好一些。

代码语言:javascript
复制
    /**
     * 根据指定的宽高对{@link Image}图像进行绽放
     * @param src 原图对象
     * @param width 目标图像宽度
     * @param height 目标图像高度
     * @return 返回缩放后的{@link Image}对象
     */
    private Image resize(Image src, int width, int height) {
        Image scaled = new Image(Display.getDefault(), width, height);
        GC gc = new GC(scaled);
        try{
            gc.setAdvanced(true); 、// 打开高级绘图模式
            gc.setAntialias(SWT.ON);// 设置消除锯齿
            gc.setInterpolation(SWT.HIGH); // 设置插值
            gc.drawImage(src, 0, 0, src.getBounds().width, src.getBounds().height,0, 0, width, height);
        }finally{
            gc.dispose();
        }
        return scaled;
    }
    /**
     * 根据缩放比例对{@link Image}对象进行缩放
     * @param src 原图对象
     * @param zoom 缩放比例
     * @return 返回缩放后的{@link Image}对象
     */
    private Image resize(Image src, float zoom) {
        Rectangle bounds = src.getBounds();
        bounds.width*=zoom;
        bounds.height*=zoom;
        return resize(src,bounds.width,bounds.height);
    }

参考资料:

http://www.ibm.com/developerworks/cn/opensource/os-cn-swtimage2/

http://blog.csdn.net/zhangzh332/article/details/6687812

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016年11月28日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档