java 图片怎么设置抗锯齿_Java2D / Swing:将具有文本抗锯齿功能的组件渲染到BufferedImage...

我想渲染一个Java Swing组件,例如一个JButton,我也把它放在一个JFrame上,一个BufferedImage.这通常有效,但有一个主要缺点:文本抗锯齿,尤其是“LCD”抗锯齿模式,在渲染到BufferedImage时不起作用.

我已经将一些示例代码放在一起来演示问题,但首先是我的系统信息:

>操作系统:Windows 7 64位

> JVM:1.6.0_26-b03(32位)

下面的示例代码将创建一个简单的JFrame,在其上放置一个JButton,然后将JButton呈现为文件“test.png”:

public class TextAntiAliasingTest

{

public TextAntiAliasingTest() throws IOException

{

// Create Test-Button which will be rendered to an image

JButton button = new JButton( "The Test-Button" );

button.setSize( 200, 70 );

button.setLocation( 200, 150 );

// Create JFrame

final JFrame frame = new JFrame();

frame.setSize( 800, 600 );

frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

frame.setLayout( null );

frame.setLocationRelativeTo( null );

frame.add( button );

// Show JFrame

SwingUtilities.invokeLater( new Runnable() {

@Override public void run() {

frame.setVisible( true );

}

});

// Render JButton to an BufferedImage

BufferedImage image = new BufferedImage( 800, 600, BufferedImage.TYPE_INT_ARGB );

Graphics2D g2d = (Graphics2D)image.getGraphics();

button.paint( g2d );

// Write BufferedImage to a PNG file

ImageIO.write( image, "PNG", new File( "test.png" ) );

}

public static void main( String[] args ) throws Exception

{

UIManager.setLookAndFeel( "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" );

System.setProperty( "awt.useSystemAAFontSettings", "lcd" );

new TextAntiAliasingTest();

}

}

下图显示了屏幕上JFrame中的JButton与图像文件中相同的呈现JButton之间的区别:

实际上在图像中有一些文本抗锯齿,但不是在JFrame屏幕上显示的LCD优化抗锯齿(此问题也出现在默认的LookAndFeel中,而不仅仅出现在“WindowsLookAndFeel”中).

我已经尝试在“g2d”,BufferedImage的Graphics2D上下文中明确设置RenderingHint for text anti aliasing,如下所示:

g2d.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING,

RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB );

但它根本没有效果.

我迫切需要将JButton呈现为一个图像文件,就像它在屏幕上呈现一样(这只是一个例子,实际上我需要渲染一些更复杂的组件,这些组件都遭受了这种抗锯齿问题)我希望有一个真实的没有任何令人讨厌的解决方法的解决方案,如截取屏幕截图等.

我非常感谢任何帮助 – 非常感谢!

解决方法:

这是一个JVM错误

解决方法

现在我绘制一个不透明的位图并将其blit到我的目标位图中.如果文本背后的背景颜色是明白的,这应该有效:

private void drawString(String text, int x, int y, Graphics2D g, Color bg){

// Prepare an off-screen image to draw the string to

Rectangle2D bounds = g.getFontMetrics().getStringBounds(text, g);

BufferedImage image = configuration.createCompatibleImage(

(int)(bounds.getWidth() + 1.0f),

(int)(bounds.getHeight() + 1.0f),

Transparency.OPAQUE);

Graphics2D ig = image.createGraphics();

// Fill the background color

ig.setColor(bg);

ig.fillRect(0, 0, image.getWidth(), image.getHeight());

// Draw the string

int x0 = 0;

int y0 = ig.getFontMetrics().getAscent();

ig.setColor(g.getColor());

ig.setRenderingHints(g.getRenderingHints());

ig.setFont(g.getFont());

ig.drawString(text, x0, y0);

ig.dispose();

// Blit the image to the destination

g.drawImage(image, x-x0, y-y0, null);

}

如果你的背景不是普通的颜色,你必须将文本渲染为黑色的白色到位图,然后用文字颜色C填充另一个位图.然后将其blit到你的目标图像,D为:D = A * C或D = D *(1-A)A * C或一些其他合适的混合函数.

标签:java,antialiasing,bufferedimage,java-2d,graphics2d

来源: https://codeday.me/bug/20190521/1147278.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值