图像上的 Java 文本 [英] Java Text on Image

查看:26
本文介绍了图像上的 Java 文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 bufferedimage 中加载图像,然后在其上写入一些文本.添加文本后,它会使图像模糊且文本失真.我打开了文本抗锯齿.可以作为附件查看.

I am loading an image in bufferedimage and then writing some text on it .After I add text it makes image blurry and text distorted. I have TEXT ANTIALIASING ON . It can be seen as attached.

推荐答案

您应该能够使用其他响应中显示的呈现提示来控制文本质量.我知道它有效,因为我做了很多,所以我认为在这种情况下,一定是其他原因导致了质量下降.

You should be able to control the text quality with the rendering hints that are shown in the other responses. I know that it works because I have done it a lot, so I think it must be something else that causes the quality degradation in this case.

  1. 您如何检查图像质量?您是将生成的 BufferedImage 直接绘制到 Java 应用程序中的屏幕图形中还是保存到磁盘,即保存为 JPEG?如果您将其保存到磁盘,请尝试将其保存为 PNG 24 而不是 JPEG.我假设您的桌面屏幕以真彩色(32 位或 24 位色深)运行,对吗?
  2. 您确定图像实际上是作为 BufferedImage.TYPE_INT_RGB 创建的吗?如果您无法控制代码中 BufferedImage 的创建,请尝试使用 TYPE_INT_RGB 创建一个新的 BufferedImage 并将源绘制到这个中,然后将文本绘制到其中.
  3. 尝试将 RenderingHints.KEY_DITHERING 设置为 RenderingHints.VALUE_DITHER_DISABLE(尽管真彩色图像不需要这样做).

如果这仍然不能帮助您找到原因,请提供更多信息(VM 版本、操作系统)和源代码.JDK 1.6 Update 10 的文本渲染已经变得非常好,但早期版本也能够在图像中生成干净的文本,只是由于不那么复杂的抗锯齿功能看起来不太好.

If this still doesn't help you to find the cause please provide some more information (VM version, operating system) and source code. The text rendering has become quite good with JDK 1.6 Update 10 but also earlier releases were able to produce clean text in images, it just didn't look as good because of less sophisticated antialiasing.

回应您的评论:

文本中的高对比度锐边是 JPEG 压缩的一般问题,因为它不是无损压缩.如果您确实需要使用 JPEG 并且无法切换到 PNG,则可以调整保存图像的压缩质量,以在图像质量和文件大小之间找到更好的折衷方案.有关如何在保存 JPEG 时设置压缩质量的信息,请参阅以下代码.

High contrast sharp edges as in text are a general problem with JPEG compression since it is not a lossless compression. If you really need to go with JPEG and can't switch to PNG, you can tune the compression quality of your saved image to find a better compromise between image quality and file size. See the following code on how to set the compression quality when you save a JPEG.

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
import javax.imageio.stream.ImageOutputStream;

//----

float quality = 0.85f;
File outfile = new File( "MyImage.jpg" );
BufferedImage image = ...;

ImageWriter imgWriter = ImageIO.getImageWritersByFormatName( "jpg" ).next();
ImageOutputStream ioStream = ImageIO.createImageOutputStream( outfile );
imgWriter.setOutput( ioStream );

JPEGImageWriteParam jpegParams = new JPEGImageWriteParam( Locale.getDefault() );
jpegParams.setCompressionMode( ImageWriteParam.MODE_EXPLICIT );
jpegParams.setCompressionQuality( quality );

imgWriter.write( null, new IIOImage( image, null, null ), jpegParams );

ioStream.flush();
ioStream.close();
imgWriter.dispose();

这篇关于图像上的 Java 文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆