开发者

Transforming an image to gray-scale in Java

开发者 https://www.devze.com 2023-02-14 08:20 出处:网络
I want to display a gray scale image (16 bits per pixel). So far, I have this: DataInputStream aFile = new DataInputStream(new FileInputStream(\"filename.raw\"));

I want to display a gray scale image (16 bits per pixel). So far, I have this:

DataInputStream aFile = new DataInputStream(new FileInputStream("filename.raw"));
BufferedImage aBufferedImage = new BufferedImage(2000, 2000, BufferedImage.TYPE_USHORT_GRAY);
WritableRaster aRaster = aBufferedImage.getRaster();
byte[]开发者_如何学Python aRow = new byte[2000*2];
aFile.readFully(aRow, 0, 2000*2);

Now, my question is that how do I set the 16-bit intensity values from aRow to aBufferedImage?


Based on http://java.itags.org/java-tech/17212/, you could convert your byte array into an int or double array (to have one array cell per pixel), and then use WritableRaster.setSamples() or WritableRaster.setPixels(). To avoid doing the byte-to-ushort conversion by yourself, you can use DataInputStream.readUnsignedShort().

0

精彩评论

暂无评论...
验证码 换一张
取 消