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

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

问题描述

我在bufferedimage中加载图像,然后在其上写入一些文本。添加文本后,图像模糊,文本变形。我有TEXT ANTIALIASING ON。它可以被视为附加。

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.


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

如果仍然没有, t帮助你找到原因,请提供一些更多信息(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天全站免登陆