簡體   English   中英

Java Netbeans顯示opencv3錯誤:“錯誤:找不到或加載主類庫”

[英]Java Netbeans show error for opencv3 “ Error: Could not find or load main class library”

我試圖通過鏈接在Netbeans中的Java中執行opencv項目

這是代碼:

/*
* To change this license header, choose License Headers in Project     Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
  package gui;
  import java.awt.Graphics;
  import java.awt.Image;
  import java.awt.image.BufferedImage;
  import java.io.ByteArrayInputStream;
  import javax.imageio.ImageIO;
  import org.opencv.core.Core;
  import org.opencv.core.Mat;
  import org.opencv.core.MatOfByte;
  import org.opencv.core.MatOfRect;
  import org.opencv.core.Point;
  import org.opencv.core.Rect;
  import org.opencv.core.Scalar;
  //import org.opencv.core.Size;
  import org.opencv.imgcodecs.Imgcodecs;
  import org.opencv.videoio.VideoCapture;
  import org.opencv.imgproc.Imgproc;
  import org.opencv.objdetect.CascadeClassifier;


/**
*
* @author Tawfiq Chowdhury
*/
 public class FaceDetection extends javax.swing.JFrame {
 private DaemonThread myThread = null;
 int count = 0;
 VideoCapture webSource = null;
 Mat frame = new Mat();
 MatOfByte mem = new MatOfByte();
 CascadeClassifier faceDetector = new CascadeClassifier("C:/Users/Tawfiq  Chowdhury/Documents/NetBeansProjects/FaceDetection/src/gui /haarcascade_frontalface_alt.xml");
 MatOfRect faceDetections = new MatOfRect();
 /**
 * Creates new form FaceDetection
 */
 class DaemonThread implements Runnable {

    protected volatile boolean runnable = false;

    @Override
    public void run() {
        synchronized (this) {
            while (runnable) {
                if (webSource.grab()) {
                    try {
                        webSource.retrieve(frame);
                        Graphics g = jPanel1.getGraphics();
                        faceDetector.detectMultiScale(frame, faceDetections);
                        for (Rect rect : faceDetections.toArray()) {
                           // System.out.println("ttt");
                            Imgproc.rectangle(frame, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
                                    new Scalar(0, 255,0));
                        }
                        Imgcodecs.imencode(".bmp", frame, mem);
                        Image im = ImageIO.read(new ByteArrayInputStream(mem.toArray()));
                        BufferedImage buff = (BufferedImage) im;
                        if (g.drawImage(buff, 0, 0, getWidth(), getHeight()-150 , 0, 0, buff.getWidth(), buff.getHeight(), null)) {
                            if (runnable == false) {
                                System.out.println("Paused ..... ");
                                this.wait();
                            }
                        }
                    } catch (Exception ex) {
                        System.out.println("Error!!");
                        ex.printStackTrace();
                    }
                }
            }
        }
    }
}

public FaceDetection() {
    initComponents();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jButton1.setText("Start");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    jButton2.setText("Pause");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton2ActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGap(153, 153, 153)
            .addComponent(jButton1)
            .addGap(156, 156, 156)
            .addComponent(jButton2)
            .addContainerGap(198, Short.MAX_VALUE))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
            .addContainerGap(296, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jButton1)
                .addComponent(jButton2))
            .addGap(86, 86, 86))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(50, 50, 50)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(115, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(59, 59, 59)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addContainerGap())
    );

    pack();
}// </editor-fold>                        

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    webSource = new VideoCapture(0); // video capture from default cam
    myThread = new DaemonThread(); //create object of threat class
    Thread t = new Thread(myThread);
    t.setDaemon(true);
    myThread.runnable = true;
    t.start();                 //start thrad
    jButton1.setEnabled(true);  // deactivate start button
    jButton2.setEnabled(false);  
}                                        

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    myThread.runnable = false;            // stop thread
    jButton2.setEnabled(true);   // activate start button 
    jButton1.setEnabled(false);     // deactivate stop button

    webSource.release();  // stop caturing fron cam


}                                        

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(FaceDetection.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(FaceDetection.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(FaceDetection.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(FaceDetection.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new FaceDetection().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JPanel jPanel1;
// End of variables declaration                   
}

我還使用添加了庫

-DJava library path="C:\opencv\build\java\x86"

但是,當我按下運行按鈕時,它會顯示BUILD FAILED和

無法找到或加載主類庫。

我正在NetBeans 8中使用最新版本的opencv和最新版本的JDK。 該代碼在opencv3中存在一些問題,因此我根據鏈接中提供的建議進行了一些更改。

嘗試移動System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 加載本機庫時,主要方法之外的方法應該是第一個加載。 所以把你的加載語句放在一個靜態存根中

public class FaceDetection extends javax.swing.JFrame {
   static{
      System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
   }
   ....
   ....
   ....
   // rest of your code goes here 
   ....
   ....
   ....
}

解決方案在於“ VM選項”。 我已經添加了VM Option -Djava庫path =“ / usr / lib / jni /”來使librxtx-java正常工作(根據這篇文章)。 刪除此VM選項解決了我的問題。 我還沒有測試我是否仍然可以連接到我的串行設備(這就是我使用librxtx-java的原因),因為我的串口設備不在這里。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM