保存捕获的图像时出现JavaCV错误

时间:2017-10-21 10:02:13

标签: java netbeans camera webcam javacv

单击“捕获”按钮时会出现此错误。

  

OpenCV错误:未指定错误(无法找到作者   在cv :: imwrite_,file中指定的扩展名   C:\构建\ 2_4_winpack-绑定-Win64的-VCl 4静电\的OpenCV \模块\ highgui \ SRC \ loadsave.cpp,   第275行       线程中的异常" AWT-EventQueue-0" CvException [org.opencv.core.CvException:cv :: Exception:   C:\构建\ 2_4_winpack-绑定-Win64的-VCl 4静电\的OpenCV \模块\ highgui \ SRC \ loadsave.cpp:275:   错误:( - 2)找不到指定扩展名的编写器   function cv :: imwrite_       ]

这是我的捕获按钮的代码。

if (evt.getSource() == btnCapture) {
    webSource = new VideoCapture(0);
    myThread = new DaemonThread();
    Thread t = new Thread(myThread);
            t.setDaemon(true);
            myThread.runnable = true;
            t.start();

        }

myThread.runnable = false;
            webSource.release();



                    JFileChooser jFileChooser1 = new JFileChooser("./images");
                 int returnVal = jFileChooser1.showSaveDialog(this);

                if(returnVal == JFileChooser.APPROVE_OPTION){
                     File  file = jFileChooser1.getSelectedFile().getAbsoluteFile();
                     String path = jFileChooser1.getSelectedFile().getAbsolutePath();
                      f = new File(file.toURI());
                     Highgui.imwrite(file.getPath(), frame);
                }

                FileInputStream fis;
    try {
        fis = new FileInputStream(f);
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
                byte[] buf = new byte[1024];
                for(int readNum; (readNum = fis.read(buf)) != -1;){
                    bos.write(buf, 0, readNum);
    } 
 byte[] convict_image = bos.toByteArray();
    }catch (FileNotFoundException ex) {
        Logger.getLogger(cam.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(cam.class.getName()).log(Level.SEVERE, null, ex);
    }

1 个答案:

答案 0 :(得分:0)

使用imwrite编写图像时,必须指定扩展名,只需执行以下操作:

Highgui.imwrite(file.getPath()+"\\image.jpg", frame);

此处 .jpg 是扩展程序。