Class ThermalStreamer

java.lang.Object
com.flir.thermalsdk.live.streaming.Streamer
com.flir.thermalsdk.live.streaming.ThermalStreamer
All Implemented Interfaces:
Colorizer, Renderer, Closeable, AutoCloseable

public class ThermalStreamer extends Streamer implements Colorizer
Class for handling for streaming thermal images.
  • Constructor Details

    • ThermalStreamer

      public ThermalStreamer(Stream stream)
      Create a ThermalStreamer instance associated with the particular Stream.
      Parameters:
      stream - an existing Stream instance to associate with
  • Method Details

    • withThermalImage

      public void withThermalImage(Consumer<ThermalImage> functionToRun)
      This function allows to safely access ThermalImage during streaming. It ensures the ThermalImage will not change during the function execution.
      Typical use case would be similar to the following:
       Stream aStream = // a thermal stream obtained from a camera using Camera#getStreams()
       ThermalStreamer thermalStreamer = new ThermalStreamer(aStream);
       aStream.start(
           onReceivedResult -> {
               // notify UI or render thread about new frame - don't call thermalStreamer.update() directly on the stream thread
               updateUiOnSeparateThread();
           },
           error -> {
               // handle streaming error
           });
       ...
      
        private void updateUiOnSeparateThread() {
            // you can also run most of the processing on a background thread and run only the UI-related code on the main/UI thread
            runOnUiThread(() -> {
                // update the streamer data with the new frame
                thermalStreamer.update();
                // for rendering purposes we can read out the colorized pixels easily
                ImageBuffer imageBuffer = thermalStreamer.getImage();
                android.graphics.Bitmap androidBmp = BitmapAndroid.createBitmap(imageBuffer).getBitMap();
                // you can use androidBmp instance to render to ImageView for example
                mImageView.setImageBitmap(androidBmp); // UI thread required
      
                // if you want to access ThermalImage object, use the ThermalStreamer#withThermalImage() helper
                thermalStreamer.withThermalImage(thermalImage -> {
                    // here you can safely read/modify different ThermalImage parameters, like palette, fusion mode, color distribution, measurements, etc.
                    // for example you can read out the measurements data and draw them on the UI thread on top of the androidBmp as an overlay
                    // show the current palette name on the UI
                    mTextView.setText(thermalImage.getPalette().name); // UI thread required
                });
            });
       }
       
      Parameters:
      functionToRun - the ThermalImage instance can be safely accessed under this function, which ensures ThermalImage is not changed during the function execution
    • getVividIrParameters

      public VividIrParameters getVividIrParameters()
      Get parameters for the VividIR filter.
      Note: in order to set new parameters you have to use setVividIrParameters(VividIrParameters)
      Returns:
      null if this streamer doesn't use VividIR.
      See Also:
    • setVividIrParameters

      public void setVividIrParameters(VividIrParameters parameters) throws ErrorCodeException
      Set parameters for the VividIR filter.
      Throws:
      ErrorCodeException - if this streamer doesn't use VividIR
      See Also:
    • update

      public void update()
      Description copied from interface: Renderer
      Executes rendering by polling image data from the source (e.g. a stream or ThermalImage), then performing image processing. Result can be obtained using Renderer.getImage().
      Specified by:
      update in interface Renderer
      See Also:
    • getImage

      public ImageBuffer getImage()
      Description copied from interface: Renderer
      Get "rendered result" as ImageBuffer.
      Specified by:
      getImage in interface Renderer
      Returns:
      Returns a deep copy of the render output or null if Renderer.update() has not been called previously or no source image was available yet.
      See Also:
    • getScaleImage

      public ImageBuffer getScaleImage()
      Description copied from interface: Colorizer
      Get render result produced by Renderer.update() the last time scale rendering was enabled (see Colorizer.setRenderScale(boolean)) when Renderer.update() was called.
      The "render result" is a temperature range/span image (or bitmap if you like) colorized accordingly to the current palette and fusion mode settings.
      Note: If scale rendering was disabled during last call to Renderer.update(), the result from this function may be out of sync with the result of Colorizer.getScaleRangeMin(), Colorizer.getScaleRangeMax().
      Note: For getting rendered image without scale, see Renderer.getImage().
      Specified by:
      getScaleImage in interface Colorizer
      Returns:
      Returns a deep copy of the render output, or null if Renderer.update() has not been called previously with scale rendering enabled (see Colorizer.setRenderScale(boolean)) or no source image was available yet.
      See Also:
    • isAutoScale

      public boolean isAutoScale()
      Description copied from interface: Colorizer
      Gets if new rendered images will have scale (temperature range/span) automatically adjusted based on the min and max values in the image.
      Note: auto-adjusted scale is disabled by default
      Specified by:
      isAutoScale in interface Colorizer
      Returns:
      true if auto-adjusted scale (temperature range/span) is enabled, otherwise false.
    • setAutoScale

      public void setAutoScale(boolean autoScale)
      Description copied from interface: Colorizer
      When enabled, new rendered images will have scale (temperature range/span) automatically adjusted based on the min and max values in the image.
      Note: Calling this function does not trigger any render. Use Renderer.update() if necessary.
      Specified by:
      setAutoScale in interface Colorizer
      Parameters:
      autoScale - Use true to enable auto-adjusted scale (temperature range/span), false to disable.
    • getScaleRangeMin

      public ThermalValue getScaleRangeMin()
      Description copied from interface: Colorizer
      Get the min range of the scale when scale auto-adjust is set.
      Note: Calling this function is only valid if Colorizer.isAutoScale() was enabled at the time of the last render.
      Specified by:
      getScaleRangeMin in interface Colorizer
      Returns:
      Scale min thermal value if Renderer.update() has been called and Colorizer.isAutoScale() was enabled when Renderer.update() was called, otherwise throws RuntimeException.
      See Also:
    • getScaleRangeMax

      public ThermalValue getScaleRangeMax()
      Description copied from interface: Colorizer
      Get the max range of the scale when scale auto-adjust is set.
      Note: Calling this function is only valid if Colorizer.isAutoScale() was enabled at the time of the last render.
      Specified by:
      getScaleRangeMax in interface Colorizer
      Returns:
      Scale max thermal value if Renderer.update() has been called and Colorizer.isAutoScale() was enabled when Renderer.update() was called, otherwise throws RuntimeException.
      See Also:
    • isRenderScale

      public boolean isRenderScale()
      Description copied from interface: Colorizer
      Gets whether or not scale (temperature range/span image) should be rendered when rendering a new image.
      This setting corresponds to Colorizer.getScaleImage() in the way that when this option is disabled the Colorizer.getScaleImage() will return null.
      Note: Scale rendering is disabled by default for performance reasons.
      Specified by:
      isRenderScale in interface Colorizer
      Returns:
      true if scale will be rendered, otherwise false.
    • setRenderScale

      public void setRenderScale(boolean renderScale)
      Description copied from interface: Colorizer
      Sets whether or not scale (temperature range/span image) should be rendered when rendering a new image.
      This setting corresponds to Colorizer.getScaleImage() in the way that when this option is disabled the Colorizer.getScaleImage() will return null.
      Note: Scale rendering is disabled by default for performance reasons.
      Specified by:
      setRenderScale in interface Colorizer
      Parameters:
      renderScale - Use true to enable, false to disable.
    • getRegionOfInterest

      public Rectangle getRegionOfInterest()
      Description copied from interface: Colorizer
      Get the region of interest (ROI) that is used to calculate the histogram in the Agc filter.
      Specified by:
      getRegionOfInterest in interface Colorizer
    • setRegionOfInterest

      public void setRegionOfInterest(Rectangle roi)
      Description copied from interface: Colorizer
      Set the region of interest (ROI) that is used to calculate the histogram in the Agc filter. Note: To disable the ROI and use the entire image contents, set an empty ROI Rectangle{0,0,0,0}.
      Specified by:
      setRegionOfInterest in interface Colorizer
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • isTemporaryStreamError

      public static boolean isTemporaryStreamError(ErrorCode errorCode)
      This exception may occur when live streaming from the camera when using update().
      Usually this is temporary error when the incoming frame is invalid/corrupted due to connectivity problems and cannot be parsed by the native layer. Is it suggested to use try-catch block, in example:
       try {
            thermalStreamer.update()
       } catch (ErrorCodeException e) {
            if (ThermalStreamer.isTemporaryStreamError(e.errorCode)) {
                  // ignore this frame and await next correct frame - early return
                  return;
            }
       }
       // continue processing the frame if thermalStreamer.update() was successful