Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

86
LINES

< > BotCompany Repo | #1016673 // Live WebCam [Dyn Module]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 2547K of libraries. Click here for Pure Java version (13885L/72K).

!7

// Trying to dispose of web cam properly when module is exited
// (see https://github.com/sarxos/webcam-capture/issues/848)

cm LiveWebCam > DynSCP {
  transient WebcamPanel panel;
  int fpsLimit = -1;
  bool autoStart;
  switchable bool alwaysOnTop; // TODO: alwaysOnTop without restart
  
  enhanceFrame {
    minimalFrameSize(f/JInternalFrame, 320, 240);
    internalFramePopupMenuItem(f, "Save to AI Album",
      rThread { dm_saveToAIAlbum(getImage()) });
    internalFramePopupMenuItem(f, "Set FPS...", r {
      final JTextField tf = jtextfield(str(fpsLimit));
      showFormTitled("Set FPS Limit",
        "FPS Limit (-1 for no limit, 0 for no updates):", tf,
        rThread { setFPSLimit(parseInt(gtt(tf))) });
    });
    dm_boolFieldMenuItem(f, 'autoStart);
    internalFramePopupMenuItem(f, "Natural size", r dm_packFrame);
    if (alwaysOnTop) dm_alwaysOnTop(f);
  }

  JComponent visualize2() {
    ret jcenteredbutton("Start", rThread startIt);
  }
  
  afterVisualize { if (autoStart) startIt(); }
  
  void startIt {
    selectWebCam(vf<Webcam> selectCam);
  }
  
  void startPreferredOrSelect {
    selectWebCam_withPreferred(vf<Webcam> selectCam);
  }
  
  void selectCam(Webcam cam) enter {
    final WebcamPanel panel = new WebcamPanel(cam, false);
    LiveWebCam.this.panel = panel;
    bindToComponent(panel, null, r { panel.stop() });
    updateFPSLimitInPanel();
    setComponent(panel);
    //dm_packFrame();
  }
  
  void stopIt {
    if (panel == null) ret;
    panel.stop();
  }
  
  void cleanMeUp {
    var cam = panel?.getWebcam();
    if (cam != null) {
      print("Disposing of web cam");
      call(cam, "dispose");
    }
  }
  
  BufferedImage getImage() {
    ret imageFromWebcamPanel(panel);
  }
  
  void setFPSLimit(int limit) {
    setField(fpsLimit := limit);
    updateFPSLimitInPanel();
  }
  
  void updateFPSLimitInPanel {
    setModuleName((fpsLimit >= 0 ? fpsLimit + " FPS - " : "") + "Live WebCam");
    if (panel == null) ret;
    panel.stop();
    if (fpsLimit == 0) ret; // TODO - want to keep last image there anyway
    panel.setFPSDisplayed(fpsLimit < 0);
    if (fpsLimit > 0) panel.setFPSLimit(fpsLimit);
    panel.setFPSLimited(fpsLimit > 0);
    panel.start();
  }
  
  bool isWebCamLive() {
    ret panel != null && fpsLimit != 0;
  }
}

Author comment

Began life as a copy of #1013489

download  show line numbers  debug dex  old transpilations   

Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, wdffvflfhhdx

No comments. add comment

Snippet ID: #1016673
Snippet name: Live WebCam [Dyn Module]
Eternal ID of this version: #1016673/32
Text MD5: a4b2a66eac77e20e94b146f3dfec3770
Transpilation MD5: 0a2c71aa1627e38e2ba29db2e5fd59b1
Author: stefan
Category: javax / desktop / camera
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-25 17:08:18
Source code size: 2387 bytes / 86 lines
Pitched / IR pitched: No / No
Views / Downloads: 590 / 3032
Version history: 31 change(s)
Referenced in: [show references]