Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用静止图像重试捕获java_Java_Image_Try Catch - Fatal编程技术网

使用静止图像重试捕获java

使用静止图像重试捕获java,java,image,try-catch,Java,Image,Try Catch,我有一个循环。在while循环中有一个try-catch。 在“尝试”中,每2秒从IP摄像头获取一次静止图像 但有时我会遇到一个异常,因为代码没有得到所有的数据包。。 现在,我希望当我得到异常时,我的代码可以在我的代码中重试 你能帮我吗 while(true){ Image image = null; try { String path = "http://10.13.8.14/media/cam0/still.jpg?res=800

我有一个循环。在while循环中有一个try-catch。 在“尝试”中,每2秒从IP摄像头获取一次静止图像

但有时我会遇到一个异常,因为代码没有得到所有的数据包。。 现在,我希望当我得到异常时,我的代码可以在我的代码中重试

你能帮我吗

while(true){

           Image image = null;
  try {

                String path = "http://10.13.8.14/media/cam0/still.jpg?res=800";

                URL url = new URL(path);
                image = ImageIO.read(url);
                Thread.sleep(2000);

            } catch (Exception e) {
                 JOptionPane.showMessageDialog(null, "image niet in orde");
            }
            ImageIcon lic = new ImageIcon(image);
            label.setIcon(lic);

        }

将代码放入一个方法中,并为两个调用调用该方法

public void tryBlock() throws Exception{
        Image image=null;
        String path = "http://10.13.8.14/media/cam0/still.jpg?res=800";
        URL url = new URL(path);
        image = ImageIO.read(url);
        Thread.sleep(2000);
    }

继续调用此方法,直到没有异常抛出

查看代码后只需一条注释。看起来你有一个无限循环,在某个地方设置了一个中断。这就是我的意思,我希望每2秒从我的IP摄像头URL中获得一个新图像。确保你不会在内存不足的情况下结束…你应该将你的“尝试捕捉”块放入无限循环中。并尝试给出一个终止条件来中断循环。因此,它将继续调用try,直到捕捉到异常后满足终止条件。您可以举个例子吗?继续调用此方法,直到没有异常出现。您应该将try和catch块放入无限while循环中。在try块中,给出一个中断循环的终止条件。因此,它将继续调用try块,直到捕获异常后满足终止条件为止。请告诉我它是否有效,并查看以下内容: