2017-10-21 170 views
0

單擊我的拍攝按鈕時會出現此錯誤。保存拍攝圖像時出現JavaCV錯誤

OpenCV Error: Unspecified error (could not find a writer for the specified extension) in cv::imwrite_, file C:\build\2_4_winpack-bindings-win64-vc14-static\opencv\modules\highgui\src\loadsave.cpp, line 275 Exception in thread "AWT-EventQueue-0" CvException [org.opencv.core.CvException: cv::Exception: C:\build\2_4_winpack-bindings-win64-vc14-static\opencv\modules\highgui\src\loadsave.cpp:275: error: (-2) could not find a writer for the specified extension in 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); 
    } 
+0

代碼中的邏輯是什麼?在第一次啓動VideoCapture後,您立即釋放它?並且在Highgui.imwrite(file.getPath(),frame)行中;'你在哪裏以及如何捕獲了框架?等等問題...... –

+0

// btnPause Action事件 (myThread.runnable = FALSE; btnOpen.setEnabled(假); btnCapture.setEnabled(真); webSource.release();) –

+0

// btnOpen行動event {webSource = new VideoCapture(0); myThread = new DaemonThread();線程t =新線程(myThread); t.setDaemon(true); myThread.runnable = true; t.start(); btnCapture.setEnabled(false); btnOpen.setEnabled(true); } –

回答

0

當你寫使用imwrite您必須指定擴展名的圖像,只是做這樣的事情:

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

這裏的.jpg是擴展。

+0

這個錯誤發生在我運行時代碼。 {java.io.FileNotFoundException:C:\ Users \ Kurt \ Desktop \ asd(系統找不到指定的文件)} –

+0

嗨!這個錯誤不屬於OpenCV,所以你可以檢查給定的路徑OK。 –