Transforming an image to gray-scale in Java
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()
.
精彩评论