在 Matlab 中从 Java GUI 调用 2 个类

发布于 2024-11-30 11:14:36 字数 2477 浏览 1 评论 0原文

由于Java在使用后无法卸载本机库(在Matlab中;请参阅SO问题),我是尝试从 Matlab 中调用 2 个 GUI 类。我正在努力从相机中抓取图像,然后将其保存在磁盘上。我想使用一个 Java 类与相机通信,而另一个类 (GUI) 仍在 Matlab 中打开。这可能吗?代码如下:

1.

 public class GUI
    {

    public static void main(String[] args)
    {
    // Just open up the window and start things running
    MainWindow mWindow = new MainWindow();
    }

    public static void main2()
    {
        MainWindow.grabImage(0);
    }
}

2.

public class MainWindow

{

static volatile int commandVal;
        Thread updateThread;
        static CameraImage cImage;
static int fs_c =1;
    MainWindow(){

    JFrame main_f = new JFrame("M");
        main_f.getContentPane().setLayout(new BorderLayout());
        main_f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        main_f.getContentPane().add(tabPane1, "Center");

        main_f.pack();
        main_f.setVisible(true);
                kkk = 1;

        mySerial = new CameraSerial(cWindow.getPort());
}
}

3.

public static void grabImage(int commandVal){

                   while (MainWindow.kkk == 1) {
                       if (MainWindow.fs_c == 1) {
                        MainWindow.commandVal = 5;
            }
            if (MainWindow.commandVal == 5 || MainWindow.commandVal == 6){

                cImage.sendFrame(0);
                                JFileChooser save_d = new JFileChooser();
                                File saveFile = save_d.getSelectedFile();
                cImage.writeImage(saveFile + ".jpg");

                                MainWindow.fs_c = 0;
                                MainWindow.commandVal = 0;
                                mySerial.write("\r");
                                System.out.println("Camera Ready...");

break;

                        }

                else if (commandVal == -1) {
                MainWindow.commandVal = 0;
                        mySerial.write("\r");
                                status_t.setText("Camera Ready...");
            }
                       else {
                try {
                    Thread.sleep(100);

                } catch (Exception e) {
                }
            }
        }

}

在 Matlab 中,我首先调用 Gui.main([]),然后调用 Gui.main2()。这是第一次起作用。但是当我再次调用 Gui.main2() 时,Matlab 什么也不做。我认为某处代码很糟糕。感谢您的回复!

Since Java cannot unload native libraries when once used (in Matlab; see SO question), i am trying to call 2 GUI classes from within Matlab. I am working on grabbing an image from camera and then saving it on disk. I want to use one Java class for communicating with camera while another class (GUI) is still open in Matlab. Is this possible? Here's the code:

1.

 public class GUI
    {

    public static void main(String[] args)
    {
    // Just open up the window and start things running
    MainWindow mWindow = new MainWindow();
    }

    public static void main2()
    {
        MainWindow.grabImage(0);
    }
}

2.

public class MainWindow

{

static volatile int commandVal;
        Thread updateThread;
        static CameraImage cImage;
static int fs_c =1;
    MainWindow(){

    JFrame main_f = new JFrame("M");
        main_f.getContentPane().setLayout(new BorderLayout());
        main_f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        main_f.getContentPane().add(tabPane1, "Center");

        main_f.pack();
        main_f.setVisible(true);
                kkk = 1;

        mySerial = new CameraSerial(cWindow.getPort());
}
}

3.

public static void grabImage(int commandVal){

                   while (MainWindow.kkk == 1) {
                       if (MainWindow.fs_c == 1) {
                        MainWindow.commandVal = 5;
            }
            if (MainWindow.commandVal == 5 || MainWindow.commandVal == 6){

                cImage.sendFrame(0);
                                JFileChooser save_d = new JFileChooser();
                                File saveFile = save_d.getSelectedFile();
                cImage.writeImage(saveFile + ".jpg");

                                MainWindow.fs_c = 0;
                                MainWindow.commandVal = 0;
                                mySerial.write("\r");
                                System.out.println("Camera Ready...");

break;

                        }

                else if (commandVal == -1) {
                MainWindow.commandVal = 0;
                        mySerial.write("\r");
                                status_t.setText("Camera Ready...");
            }
                       else {
                try {
                    Thread.sleep(100);

                } catch (Exception e) {
                }
            }
        }

}

From Matlab I'm calling first Gui.main([]), and then Gui.main2(). It works for the first time. But when I call Gui.main2() again, Matlab does nothing. I think it's a bad code somewhere. Thanks for replying!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〆凄凉。 2024-12-07 11:14:36

只是一个假设,没有更多信息:

此检查:

if (MainWindow.fs_c == 1) {
  MainWindow.commandVal = 5;
}

以及稍后此(请注意,第一次检查成功后 commandVal 将为 5):

MainWindow.fs_c = 0;
MainWindow.commandVal = 0;

问题是:

在第一次运行中,commandVal 设置为 5,假设 MainWindow.fs_c 最初为 1。
因此MainWindow.fs_c = 0;被执行。

在第二次运行中,MainWindow.fs_c == 1 为 false(MainWindow.fs_c 现在为 0),并且 MainWindow.commandVal 也设置为 0因此,该方法除了休眠 100 毫秒之外不执行任何操作。

Just an assumption without having more information:

This check:

if (MainWindow.fs_c == 1) {
  MainWindow.commandVal = 5;
}

And later this (note that commandVal would be 5 after the check succeeded for the first time):

MainWindow.fs_c = 0;
MainWindow.commandVal = 0;

Here's the problem:

In the first run, commandVal is set to 5, assuming MainWindow.fs_c is initially 1.
Thus MainWindow.fs_c = 0; is executed.

In the second run MainWindow.fs_c == 1 is false (MainWindow.fs_c is now 0) and MainWindow.commandVal was also set to 0. Thus the method doesn't do anything, except sleeping for 100 milliseconds.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文