无法使用 swt Transform 旋转透明图像

发布于 2024-11-13 19:34:40 字数 571 浏览 1 评论 0原文

我试图在画布上旋转 PNG 图像,旋转后图像的质量变得非常差。最初,PNG 是透明背景上的箭头。旋转后无法辨别它是箭头。

我使用以下代码:

Transform oldTransform = new Transform(
Display.getCurrent());
gc.getTransform(oldTransform);

Transform transform = new Transform(Display.getCurrent());
transform.translate(xm + imageBounds.width / 2, ym + imageBounds.height / 2);
transform.rotate(179);
transform.translate(-xm - imageBounds.width / 2, -ym - imageBounds.height / 2);

gc.setTransform(transform);
gc.drawImage(image, xm, ym);
gc.setTransform(oldTransform);

transform.dispose();

提前谢谢您。

I'm trying to rotate a PNG image on a canvas and the quality of the image becomes very bad after rotation. Initially the PNG is an arrow on a transparent background. After rotation it is impossible to tell that it is an arrow.

I use following code:

Transform oldTransform = new Transform(
Display.getCurrent());
gc.getTransform(oldTransform);

Transform transform = new Transform(Display.getCurrent());
transform.translate(xm + imageBounds.width / 2, ym + imageBounds.height / 2);
transform.rotate(179);
transform.translate(-xm - imageBounds.width / 2, -ym - imageBounds.height / 2);

gc.setTransform(transform);
gc.drawImage(image, xm, ym);
gc.setTransform(oldTransform);

transform.dispose();

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧话新听 2024-11-20 19:34:40

您可以水平和垂直翻转图像(无需任何像素转换),而不是将图像旋转 180 度:

private BufferedImage flipH(BufferedImage src) {
        int w = src.getWidth();  
        int h = src.getHeight();  
        BufferedImage dst = new BufferedImage(w, h, src.getType());
        Graphics2D g = dst.createGraphics();  
        g.drawImage(src,
                    0, // x of first corner  (destination)
                    0, // y of first corner  (destination)
                    w, // x of second corner (destination)
                    h, // y of second corner (destination)
                    w, // x of first corner  (source)
                    0, // y of first corner  (source)
                    0, // x of second corner (source)
                    h, // y of second corner (source)
                    null);  
        g.dispose();
        return dst;
}

private BufferedImage flipV(BufferedImage src) {
        int w = src.getWidth();  
        int h = src.getHeight();  
        BufferedImage dst = new BufferedImage(w, h, src.getType());
        Graphics2D g = dst.createGraphics();  
        g.drawImage(src, 0, 0, w, h, 0, h, w, 0, null);  
        g.dispose();
        return dst;
}

...
BufferedImage flipped = flipH(flipV(ImageIO.read(new File("test.png"))));
ImageIcon icon = new ImageIcon(flipped);
...

编辑:或者更好的是,在单个操作中水平和垂直翻转(与旋转 180 度相同) :

g.drawImage(src, 0, 0, w, h, w, h, 0, 0, null);  

Edit2: 还有一个特定于 SWT 的图像旋转/翻转示例,无需 Transform 也是

Instead of rotating the image 180 degrees, you could flip it horizontally and vertically (without any pixel transformation):

private BufferedImage flipH(BufferedImage src) {
        int w = src.getWidth();  
        int h = src.getHeight();  
        BufferedImage dst = new BufferedImage(w, h, src.getType());
        Graphics2D g = dst.createGraphics();  
        g.drawImage(src,
                    0, // x of first corner  (destination)
                    0, // y of first corner  (destination)
                    w, // x of second corner (destination)
                    h, // y of second corner (destination)
                    w, // x of first corner  (source)
                    0, // y of first corner  (source)
                    0, // x of second corner (source)
                    h, // y of second corner (source)
                    null);  
        g.dispose();
        return dst;
}

private BufferedImage flipV(BufferedImage src) {
        int w = src.getWidth();  
        int h = src.getHeight();  
        BufferedImage dst = new BufferedImage(w, h, src.getType());
        Graphics2D g = dst.createGraphics();  
        g.drawImage(src, 0, 0, w, h, 0, h, w, 0, null);  
        g.dispose();
        return dst;
}

...
BufferedImage flipped = flipH(flipV(ImageIO.read(new File("test.png"))));
ImageIcon icon = new ImageIcon(flipped);
...

Edit: or even better, flip both horizontally and vertically in single op (same as rotating 180 degrees):

g.drawImage(src, 0, 0, w, h, w, h, 0, 0, null);  

Edit2: There is also SWT-specific example of image rotation/flipping without Transform too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文