写入16位BufferedImage TYPE_USHORT_GRAY [英] Write to 16 bit BufferedImage TYPE_USHORT_GRAY

查看:600
本文介绍了写入16位BufferedImage TYPE_USHORT_GRAY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用BufferedImage.TYPE_USHORT_GRAY将16位灰度图像数据写入png。通常我写这样的图像:

I'm trying to write 16 bit grayscale imagedata to a png using BufferedImage.TYPE_USHORT_GRAY. Normally I write to an image like so:

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

然后:

image.setRGB(x,y,Color.getRGB);

设置像素,最后:

ImageIO.write(image, "png", new File(path + ".png"));

写入png图像。

但现在我把它作为图像:

But now I have this as image:

BufferedImage imageGray = new BufferedImage(width, height, BufferedImage.TYPE_USHORT_GRAY);

如何将像素保存为该格式?使用带有16位整数的setRGB()似乎不起作用,当我打开保存的png文件时,我发现很多条带正在发生。

How do I go about saving pixels to that format? Using setRGB() with a 16 bit integer doesn't seem to work, when I open the saved png file I see a lot of banding happening.

我试过保存一个从0到65535的简单渐变,然后在灰度图像上使用setRGB(),并在Photoshop中检查结果。我可以看到图像由每256行的较小渐变组成。我猜测setRGB()或imageIO不能正常工作。

I tried saving a simple gradient from 0 to 65535 and then using the setRGB() on the grayscale image, and checked the results in Photoshop. I can see the image consists of smaller gradients every 256 rows. I'm guessing either setRGB() or imageIO doesn't work as I would like it to.

这有解决方法吗? imageIO甚至支持BufferedImage.TYPE_USHORT_GRAY格式吗?或者它只能保存8位数据?如果它可以保存16位数据,我将如何保存像素数据,最好以setRGB()的方式工作(所以对于某个x,y坐标)?

Are there workarounds for this? Does imageIO even support the BufferedImage.TYPE_USHORT_GRAY format? Or can it only save 8 bit data? And if it can save 16bit data, how would I go about saving pixel data, preferably in a way like setRGB() works (so for a certain x,y coordinate)?

推荐答案

正如pst已经在我的问题下面评论过,我所要做的就是直接访问Raster。问题解决了!

As pst already commented below my question all I had to do was accessing the Raster directly. Problem solved!

这篇关于写入16位BufferedImage TYPE_USHORT_GRAY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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