简体   繁体   English

图像旋转时字符串抗锯齿不起作用

[英]String antialiasing don't work when image rotate

I have some problem with string antialiasing in Java. 我在Java中使用字符串抗锯齿有一些问题。 If I use this code 如果我使用此代码

g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

on normal (not rotated string), all is fine. 在正常情况下(非旋转字符串),一切都很好。 But, when I try use antialiasing on rotated image, where I draw string - this string rendering without antialiasing feature. 但是,当我尝试在旋转的图像上使用抗锯齿功能时,我会在该图像上绘制字符串-此字符串呈现没有抗锯齿功能。

This is example of my code, where antialiasing doesn't work: 这是我的代码示例,其中抗锯齿不起作用:

Graphics2D g = originalImage.createGraphics();

AffineTransform affineTransform2 = new AffineTransform();
affineTransform2.setToIdentity();
affineTransform2.translate(125, 325);
affineTransform2.rotate(Math.toRadians(345));

BufferedImage positionImage = new BufferedImage(100, 40, BufferedImage.TYPE_INT_ARGB);
Graphics2D positionImageG2D = positionImage.createGraphics();
positionImageG2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
positionImageG2D.setFont(getFont(30f));
positionImageG2D.drawString("Some Text",100, 40);
positionImageG2D.dispose();

g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.drawImage(positionImage, affineTransform2, null); 

You don't draw text to g (last line), so whatever value you pass to KEY_TEXT_ANTIALIASING won't have any effect. 您不会在g (最后一行)上绘制文本,因此,无论您将什么值传递给KEY_TEXT_ANTIALIASING都不会起作用。

You are drawing a rotated image with already antialiased text. 您正在绘制带有已经消除锯齿的文本的旋转图像。 Try setting KEY_INTERPOLATION to VALUE_INTERPOLATION_BICUBIC or VALUE_INTERPOLATION_BILINEAR , which should effect scaling and rotation of images. 尝试将KEY_INTERPOLATION设置为VALUE_INTERPOLATION_BICUBICVALUE_INTERPOLATION_BILINEAR ,这将影响图像的缩放和旋转。

Another thing you could try, is to draw the text (rotated) directly onto g , without the temporary image. 您可以尝试的另一件事是将文本(旋转的)直接绘制到g ,而不使用临时图像。 Should also work, but you might have to adjust positioning relative to the rotation. 应该也可以,但是您可能必须相对于旋转调整位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM