Skip to content

ReadImage15Java

anna-dodd edited this page Jun 3, 2015 · 1 revision
import nitf.IOHandle;
import nitf.ImageReader;
import nitf.ImageSegment;
import nitf.ImageSubheader;
import nitf.NITFException;
import nitf.Reader;
import nitf.Record;
import nitf.SubWindow;

public class ReadImageSample
{

    public static void main(String[] args) throws NITFException
    {
        if (args.length < 1)
        {
            System.err.println("args: <filename>");
            System.exit(0);
        }

        Reader reader = new Reader();

        IOHandle handle = new IOHandle(args[0], IOHandle.NITF_ACCESS_READONLY,
                IOHandle.NITF_OPEN_EXISTING);

        Record record = reader.read(handle);

        // get the image segments
        ImageSegment[] segments = record.getImages();

        // let's read in just the first image
        if (segments.length > 0)
        {
            ImageSegment segment = segments[0];

            // get a new ImageReader
            ImageReader deserializer = reader.getNewImageReader(0);

            // get some metadata regarding the image
            ImageSubheader subheader = segment.getSubheader();
            int nBits = subheader.getNumBitsPerPixel().getIntData();
            int nBands = subheader.getNumImageBands().getIntData();
            int xBands = subheader.getNumMultispectralImageBands().getIntData();
            // get the number of bands
            nBands += xBands;

            int nRows = subheader.getNumRows().getIntData();
            int nColumns = subheader.getNumCols().getIntData();
            int numBytesPerPixel = ((nBits - 1) / 8 + 1);

            int imageBandSize = nRows * nColumns * numBytesPerPixel;

            byte[][] buffer = new byte[nBands][imageBandSize];

            // set the band list
            int[] bands = new int[nBands];
            for (int i = 0; i < bands.length; i++)
                bands[i] = i;

            // let's read the entire image in one fell swoop
            SubWindow imageRequest = new SubWindow();
            imageRequest.setStartCol(0);
            imageRequest.setStartRow(0);
            imageRequest.setNumCols(nColumns);
            imageRequest.setNumRows(nRows);
            imageRequest.setBandList(bands);
            imageRequest.setNumBands(nBands);

            // read the image data to the buffer
            boolean readStatus = deserializer.read(imageRequest, buffer);

            // TODO -- do something with the image data now...
            // you could write it to disk, display it, etc.

            // be nice and cleanup
            imageRequest.destruct();

        }

        handle.close();
    }
}
Clone this wiki locally