2013-02-27 50 views
-1

我正在開發一個帶有人臉檢測的MP3播放器。我有兩種形式。如何訪問其他表單的列表

第一種形式是媒體播放器和人臉檢測代碼,第二種形式是創建播放列表的代碼。

我希望當我點擊創建按鈕時,播放列表的名稱應顯示在第一個表單的列表框中。

這是我的一些爲Form1代碼

private Capture cap; 
    public Form1() 
    { 
     InitializeComponent(); 

     cap = new Capture(0); 
     _dataBasePath = Directory.GetCurrentDirectory() + @"\db1.mdb"; 


     // adjust path to find your xml 

     haar = new HaarCascade("haarcascade_frontalface_alt_tree.xml"); 
     mouth = new HaarCascade("Mouth.xml"); 

     lefteye = new HaarCascade("eye_left.xml"); 
     righteye = new HaarCascade("haarcascade_righteye_2splits.xml"); 
    } 

    public void DataBasePath(string path) 
    { 
     _dataBasePath = path; 
    } 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
     using (Image<Bgr, byte> nextFrame = cap.QueryFrame()) 
     { 
      if (nextFrame != null) 
      { 
       // there's only one channel (greyscale), hence the zero index 
       //var faces = nextFrame.DetectHaarCascade(haar)[0]; 
       Image<Gray, byte> grayframe = nextFrame.Convert<Gray, byte>(); 
       Image<Gray, Byte> gray = nextFrame.Convert<Gray, Byte>(); 
       Image<Gray, Byte> gray1 = nextFrame.Convert<Gray, Byte>(); 
       Image<Gray, Byte> gray2 = nextFrame.Convert<Gray, Byte>(); 
       var faces = grayframe.DetectHaarCascade(
         haar, 1.4, 4, 
         HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, 
         new Size(nextFrame.Width/8, nextFrame.Height/8) 
         )[0]; 
       foreach (MCvAvgComp f in faces) 
       { 
        //draw the face detected in the 0th (gray) channel with blue color 
        nextFrame.Draw(f.rect, new Bgr(Color.Blue), 2); 
        facesnap = f.rect; 


        int halfheight = facesnap.Height/2; 
        int start = facesnap.X; 
        int start1 = facesnap.Y; 

        Rectangle top = new Rectangle(start,start1,facesnap.Width,halfheight); 
        int start2 = top.Bottom; 

        Rectangle bottom = new Rectangle(start, start2, facesnap.Width, halfheight); 


        //Set the region of interest on the faces 
        gray.ROI = bottom; 
        MCvAvgComp[][] mouthsDetected = gray.DetectHaarCascade(mouth, 
                1.1, 10, 
                Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, 
                new Size(40,30)); 
        gray.ROI = Rectangle.Empty; 


        foreach (MCvAvgComp m in mouthsDetected[0]) 
        { 
         Rectangle mouthRect = m.rect; 
         mouthRect.Offset(bottom.X, bottom.Y); 
         nextFrame.Draw(mouthRect, new Bgr(Color.Red), 2); 
         Rectangle mouthphoto = new Rectangle(mouthRect.X - 5, mouthRect.Y - 10, mouthRect.Width + 5, mouthRect.Height +10); 
         detectedmouth = mouthphoto; 
        } 
        int halfwidth =facesnap.Width/2; 

        Rectangle toprighteye =new Rectangle(start,start1,halfwidth,halfheight); 
        int leftx = toprighteye.Right; 
        Rectangle toplefteye = new Rectangle(leftx, start1, halfwidth, halfheight); 

        gray1.ROI =toplefteye; 

        MCvAvgComp[][] detectedlefteye = gray1.DetectHaarCascade(lefteye, 1.1, 1, Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(30,10)); 
        gray1.ROI = Rectangle.Empty; 

        foreach (MCvAvgComp eyesnapleft in detectedlefteye[0]) 
        { 
         Rectangle eyeRectleft = eyesnapleft.rect; 
         eyeRectleft.Offset(toplefteye.X, toplefteye.Y); 
         nextFrame.Draw(eyeRectleft, new Bgr(Color.Green), 2); 

         lefteyesnap = eyeRectleft; 


        } 

        gray2.ROI = toprighteye; 

        MCvAvgComp[][] detectedrighteye = gray2.DetectHaarCascade(righteye, 1.1, 1, Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(30, 10)); 
        gray2.ROI = Rectangle.Empty; 

        foreach (MCvAvgComp eyesnapright in detectedrighteye[0]) 
        { 
         Rectangle eyeRectright = eyesnapright.rect; 
         eyeRectright.Offset(toprighteye.X, toprighteye.Y); 
         nextFrame.Draw(eyeRectright, new Bgr(Color.Yellow), 2); 

         righteyesnap = eyeRectright; 


        } 




       } 


       pictureBox1.Image = nextFrame.ToBitmap(); 
      } 
     } 

    } 
private void Form1_Load(object sender, EventArgs e) 
    { 
     _dataBasePath = Directory.GetCurrentDirectory() + @"\db1.mdb"; 


     // adjust path to find your xml 

     haar = new HaarCascade("haarcascade_frontalface_alt_tree.xml"); 
     mouth = new HaarCascade("Mouth.xml"); 

     lefteye = new HaarCascade("eye_left.xml"); 
     righteye = new HaarCascade("haarcascade_righteye_2splits.xml"); 
    } 

這是一些用於窗口2

namespace Face_Detection_Concept 
{ 
    public partial class Form2 : Form 
{ 
    #region Global Variables 

    public const string Separator = ","; 

    public static ArrayList searchAudio = new ArrayList(); 
    public static ArrayList searchVideo = new ArrayList(); 



    public static StreamWriter sw; 
    public string fnm; 
    #endregion 

    public Form2() 
    { 
     InitializeComponent(); 
    } 

    private void Form2_Load(object sender, EventArgs e) 
    { 
     // Set default values 
     cmbType.SelectedIndex = 0; 
     chkIncSubDir.Checked = true; 

     LoadFormats(); 


     txtFilename.Text = "PlayList1.wpl"; 
    } 

的代碼時,即時通訊使用的按鈕單擊事件訪問Form1的列表框的代碼

new Form1().playlistviewbar.items.add(playlistname); 

但這不顯示播放列表的名稱。有沒有解決方案?

+0

http://www.codeproject.com/Articles/14122/Passing-Data-Between-Forms這也許是一個答案,你的題。 – 2013-02-27 14:24:26

+0

你的第二個形式是你的第一個孩子還是他們分開? – 2013-02-27 16:02:08

+0

單獨形式.. – vidhi 2013-02-27 16:09:49

回答

0

設定ListBox的修飾語財產公開,你可以在已知的方式對其進行訪問。

將調用表單的引用傳遞給被調用表單,並將其用於訪問LB.

// Call: 
Form2 test = new Form2(this); 

//Constructor Form2: 
public Form2 (Form1 FormToAccess) 

//Access: 
FormToAccess.listBox1.... = ... ; 
+0

他從訪問的孩子呢?父母,而不是孩子的父母,所以這根本不需要做。另外,這個ent不鼓勵他們的做法;對於兒童形式來說,直接瞭解父母或父母公開暴露其控制(即列表框)是很差的設計。 – Servy 2013-02-27 14:51:06

+0

但在form1上的一個按鈕點擊事件我要把這個代碼來顯示form2 private void toolStripButton4_Click(object sender,EventArgs e) { {Form2()。Show(); } 所以,如果我用這個代碼,它顯示了像facedetection.Form2錯誤不包含構造函數,thakes 0參數 – vidhi 2013-02-27 14:55:36

+0

我得到它你是對的。我用'新的窗口2()顯示();'讓我出現錯誤,但現在使用'form2 test = new form2(this);'它實際上工作.. thnx很多 – vidhi 2013-02-28 15:18:30

0

Capture對象cap爲空,啓動定時器,例如之前應該初始化:

public void form_load() 
    { 

     cap = new Capture(); 

     //Init and start Timer 

    } 
+0

但在form_load事件中,我已初始化它 – vidhi 2013-02-27 14:26:34

+0

你什麼時候開始你的計時器?我給你一個例子,因爲你沒有提供定時器和捕獲對象的初始化例程 – 2013-02-27 14:28:09

+0

在InitializeComponent()方法中,我已初始化定時器.. okk我有這樣做並刪除** cap = new capture(); **來自form_load事件,它不會給出錯誤。但現在它也不會在列表框中顯示播放列表名稱。有沒有解決方法? – vidhi 2013-02-27 14:40:10

1

雖然你可以通過在public直接暴露控制,更好的方法是創建一個操作ListBox內容的方法。 ,讓您靈活地更改了控制而不會破壞調用形式:

public void AddPlayList(string playlistname) 
{ 
    this.playlistviewbar.Items.Add(playlistname); 
} 
public string[] GetPlayLists() 
{ 
    return this.playlistviewbar.Items; // converting to strings if necessary. 
} 
+0

雖然這種改變是一個好主意,但目前他有問題,他試圖在初始化之前訪問列表框,因爲它是在表單加載時創建的,而不是它的構造函數。你的答案不能解決這個問題。 – Servy 2013-02-27 14:52:06

+0

抱歉,您能否在本代碼中解釋** **會得到什麼保證? ,以便我可以很容易地理解我能夠實現它 – vidhi 2013-02-27 15:22:22

+0

,我想調用此按鈕單擊事件的其他形式..所以如果我在form1中定義它,所以我怎麼能在form2中調用它。 – vidhi 2013-02-27 15:29:56