[英]Cannot rotate transparent image using swt Transform
我正在嘗試在 canvas 上旋轉 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();
先感謝您。
您可以水平和垂直翻轉它(無需任何像素變換),而不是將圖像旋轉 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 特定的圖像旋轉/翻轉示例,也沒有變換。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.