在 Matlab 中从 Java GUI 调用 2 个类
由于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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是一个假设,没有更多信息:
此检查:
以及稍后此(请注意,第一次检查成功后
commandVal
将为 5):问题是:
在第一次运行中,
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:
And later this (note that
commandVal
would be 5 after the check succeeded for the first time):Here's the problem:
In the first run,
commandVal
is set to 5, assumingMainWindow.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) andMainWindow.commandVal
was also set to 0. Thus the method doesn't do anything, except sleeping for 100 milliseconds.