将Java中的图像缩放到非常小的维度
我试图在Java应用程序中将“普通”图像(如200*200px)缩小到小尺寸(10*10px)。我必须处理的图像是字符。 它实际上工作得很好,问题是它使字符与原始图像相比太厚
例如,使用此图像:Letter
如果我将缩放图像转换为数字数组并显示它,它将生成如下内容:
*
***
*****
** **
* **
******
** **
*** **
** **
**
我希望厚度为1,而不是2,因为如果图片上的字符较小,缩放后将无法使用
对于此图像:Smaller letter
结果是:
** *
***
****
****
** *
****
正如你所看到的,这个角色已经无法识别了
这是我的密码:
public BufferedImage rescaleImage(BufferedImage img, int targetWidth)
{
int type = (img.getTransparency() == Transparency.OPAQUE) ?
BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
BufferedImage ret = (BufferedImage)img;
int w = img.getWidth();
do {
if (w > targetWidth) {
w /= 2;
if (w < targetWidth) {
w = targetWidth;
}
}
BufferedImage tmp = new BufferedImage(w, w, type);
Graphics2D g2 = tmp.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2.drawImage(ret, 0, 0, w, w, null);
g2.dispose();
ret = tmp;
} while (w != targetWidth);
return ret;
}
我在setRenderingHint方法中尝试了不同的参数,但我总是得到过厚或过薄的字符(一些像素消失)
你知道我怎样才能解决这个问题吗
谢谢
勒霍克德斯基坦
共 (0) 个答案