java - 在 Graphics2D 上绘画时图像变得参差不齐
java - image is getting jagged when painting on Graphics2D
我正在尝试在 Graphics2D 上绘制一个简单的图像。但是,当我旋转图像时,它显示了一些边缘,并忽略了渲染提示。
有什么解决办法?对于 45 度等它工作正常。但对其他人来说,它搞砸了!
谢谢
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
import javax.imageio.ImageIO;
public class Canvas {
public static void main(String args[]) {
try {
BufferedImage file = ImageIO.read(new URL("https://live.staticflickr.com/8710/28233783223_2387e00f93_b.jpg"));
BufferedImage bi = new BufferedImage(1000, 500, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g2 = (Graphics2D) bi.getGraphics();
g2.addRenderingHints(new RenderingHints(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR));
g2.addRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
g2.setColor(Color.WHITE);
g2.fillRect(0, 0, 1000, 500);
AffineTransform ax = new AffineTransform();
ax.translate(200, 200);
ax.rotate(Math.PI * (-318) / 180, 500, 250);
g2.drawImage(file, ax, null);
g2.dispose();
ImageIO.write(bi, "png", new File("text.png"));
} catch (Exception ex) {
// error
}
}
}
图像绘制似乎不受抗锯齿的影响。然而,绘制矩形是。通过使用 TexturePaint
和 file
作为基础图像,我们可以简单地绘制一个与图像大小相同的矩形。生成的图像不会有任何锯齿状边缘。
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
import javax.imageio.ImageIO;
class Canvas {
public static void main(String args[]) {
try {
BufferedImage file = ImageIO.read(new URL("https://live.staticflickr.com/8710/28233783223_2387e00f93_b.jpg"));
BufferedImage bi = new BufferedImage(1000, 500, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g2 = (Graphics2D) bi.getGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2.setColor(Color.WHITE);
g2.fillRect(0, 0, 1000, 500);
AffineTransform ax = new AffineTransform();
ax.translate(200, 200);
ax.rotate(Math.PI * (-318) / 180, 500, 250);
// g2.drawImage(file, ax, null);
g2.setPaint(new TexturePaint(file, new Rectangle(0,0, file.getWidth(), file.getHeight())));
g2.transform(ax);
g2.fillRect(0,0 , 1000, 500);
g2.dispose();
ImageIO.write(bi, "png", new File("text.png"));
} catch (Exception ex) {
// error
}
}
}
我正在尝试在 Graphics2D 上绘制一个简单的图像。但是,当我旋转图像时,它显示了一些边缘,并忽略了渲染提示。
有什么解决办法?对于 45 度等它工作正常。但对其他人来说,它搞砸了!
谢谢
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
import javax.imageio.ImageIO;
public class Canvas {
public static void main(String args[]) {
try {
BufferedImage file = ImageIO.read(new URL("https://live.staticflickr.com/8710/28233783223_2387e00f93_b.jpg"));
BufferedImage bi = new BufferedImage(1000, 500, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g2 = (Graphics2D) bi.getGraphics();
g2.addRenderingHints(new RenderingHints(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR));
g2.addRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
g2.setColor(Color.WHITE);
g2.fillRect(0, 0, 1000, 500);
AffineTransform ax = new AffineTransform();
ax.translate(200, 200);
ax.rotate(Math.PI * (-318) / 180, 500, 250);
g2.drawImage(file, ax, null);
g2.dispose();
ImageIO.write(bi, "png", new File("text.png"));
} catch (Exception ex) {
// error
}
}
}
图像绘制似乎不受抗锯齿的影响。然而,绘制矩形是。通过使用 TexturePaint
和 file
作为基础图像,我们可以简单地绘制一个与图像大小相同的矩形。生成的图像不会有任何锯齿状边缘。
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
import javax.imageio.ImageIO;
class Canvas {
public static void main(String args[]) {
try {
BufferedImage file = ImageIO.read(new URL("https://live.staticflickr.com/8710/28233783223_2387e00f93_b.jpg"));
BufferedImage bi = new BufferedImage(1000, 500, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g2 = (Graphics2D) bi.getGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2.setColor(Color.WHITE);
g2.fillRect(0, 0, 1000, 500);
AffineTransform ax = new AffineTransform();
ax.translate(200, 200);
ax.rotate(Math.PI * (-318) / 180, 500, 250);
// g2.drawImage(file, ax, null);
g2.setPaint(new TexturePaint(file, new Rectangle(0,0, file.getWidth(), file.getHeight())));
g2.transform(ax);
g2.fillRect(0,0 , 1000, 500);
g2.dispose();
ImageIO.write(bi, "png", new File("text.png"));
} catch (Exception ex) {
// error
}
}
}