您的位置:首页 > 编程语言 > Java开发

java:SWT 缩放图像(Image)

2016-11-28 18:22 573 查看
在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好一些。

/**
* 根据指定的宽高对{@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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java swt zoom drawImage