Skip to content


# PNGReader

VTKEx/Java/IO/PNGReader

### Description

vtkPNGReader is a source object that reads PNG files.

Other languages

See (Cxx), (CSharp)

Question

If you have a simple question about this example contact us at VTKExProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum

### Code

PNGReader.java

    import vtk.vtkImageViewer2;
    import vtk.vtkPNGReader;
    import vtk.vtkNativeLibrary;
    import vtk.vtkRenderWindowInteractor;

    public class PNGReader
    {
      //-----------------------------------------------------------------
      // Load VTK library and print which library was not properly loaded
      static
      {
        if (!vtkNativeLibrary.LoadAllNativeLibraries())
        {
          for (vtkNativeLibrary lib : vtkNativeLibrary.values())
          {
            if (!lib.IsLoaded())
            {
              System.out.println(lib.GetLibraryName() + " not loaded");
            }
          }
        }
        vtkNativeLibrary.DisableOutputWindow(null);
      }
      // -----------------------------------------------------------------

      public static void main(String args[])
      {
        //parse command line arguments
        if (args.length != 1) {
          System.err.println("Usage: java -classpath ... Filename(.png) e.g Gourds.png");
          return;
        }
        //  Read the image
        vtkPNGReader reader = new vtkPNGReader();
        reader.SetFileName(args[0]);

        //  Visualize
        vtkImageViewer2 imageViewer = new vtkImageViewer2();
        imageViewer.SetInputConnection(reader.GetOutputPort());
        vtkRenderWindowInteractor renderWindowInteractor = new vtkRenderWindowInteractor();
        imageViewer.SetupInteractor(renderWindowInteractor);
        imageViewer.Render();
        imageViewer.GetRenderer().ResetCamera();
        imageViewer.Render();

        renderWindowInteractor.Start();
      }
    }