⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 camcontrolpanel.java

📁 控制移到机器人的例子程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	java.awt.TextField textField20;
	java.awt.Label label15;
	java.awt.TextField textField21;
	java.awt.Label label16;
	java.awt.TextField textField22;
	java.awt.Label label17;
	java.awt.TextField textField23;
	java.awt.Label label18;
	java.awt.TextField textField24;
	java.awt.Label label19;
	java.awt.TextField textField25;
	java.awt.Label label20;
	//}}

	//{{DECLARE_MENUS
	//}}

	class SymWindow extends java.awt.event.WindowAdapter
	{
		public void windowClosing(java.awt.event.WindowEvent event)
		{
			Object object = event.getSource();
			if (object == camControlPanel.this)
				Frame1_WindowClosing(event);
		}
	}
	
	void Frame1_WindowClosing(java.awt.event.WindowEvent event)
	{
		hide();		 // hide the Frame
	}

	void getInputs()
	{    
	    inputport = Integer.parseInt(textField1.getText());
	    inputbaud = Integer.parseInt(textField15.getText());
	    inputrmin = Integer.parseInt(textField20.getText());
	    inputrmax = Integer.parseInt(textField21.getText());
	    inputgmin = Integer.parseInt(textField22.getText());
	    inputgmax = Integer.parseInt(textField23.getText());
	    inputbmin = Integer.parseInt(textField24.getText());
	    inputbmax = Integer.parseInt(textField25.getText());
	    inputx1 = Integer.parseInt(textField16.getText());
	    inputy1 = Integer.parseInt(textField17.getText());
	    inputx2 = Integer.parseInt(textField18.getText());
	    inputy2 = Integer.parseInt(textField19.getText());	    	    
	} // getInputs() //
	
	class SymMouse extends java.awt.event.MouseAdapter
	{
		public void mouseClicked(java.awt.event.MouseEvent event)
		{
			Object object = event.getSource();
			if (object == button1)
				button1_MouseClick(event);
			else if (object == button2)
				button2_MouseClick(event);
			else if (object == button3)
				button3_MouseClick(event);
			else if (object == button4)
				button4_MouseClick(event);
			else if (object == button5)
				button5_MouseClick(event);
			else if (object == button8)
				button8_MouseClick(event);
			else if (object == button6)
				button6_MouseClick(event);
			else if (object == button10)
				button10_MouseClick(event);
			else if (object == button11)
				button11_MouseClick(event);
				
		}
	}

	void button1_MouseClick(java.awt.event.MouseEvent event)
	{
		// initCamera button
		boolean returnBool;
		this.getInputs();
		returnBool = myCam.initCamera(inputport, inputbaud);
		if (returnBool) {
		    textField2.setText("initCamera succeeded. Serial port open.");
		} else {
		    textField2.setText("initCamera failed!");
		}		
	}
	
	void button2_MouseClick(java.awt.event.MouseEvent event)
	{
		// getVersion button
		String stringRet;
		this.getInputs();
		stringRet = myCam.getVersion();
		textField2.setText(stringRet);				
	}

	void button3_MouseClick(java.awt.event.MouseEvent event)
	{
	    // trackColor
		boolean returnVal;
		this.getInputs();
		returnVal = myCam.trackColor(inputrmin, inputrmax, 
		                             inputgmin, inputgmax,
		                             inputbmin, inputbmax);
        if (returnVal) {
            textField2.setText("trackColor succeeded");
            this.updateMOnScreen();
            this.drawTarget();
        } else {
            textField2.setText("trackColor command failed");
        }		
	}

    void updateMOnScreen()
    {
	    textField10.setText(String.valueOf(myCam.dataM.x1));
	    textField9.setText(String.valueOf(myCam.dataM.x2));
	    textField11.setText(String.valueOf(myCam.dataM.y2));
	    textField12.setText(String.valueOf(myCam.dataM.y1));
	    textField13.setText(String.valueOf(myCam.dataM.pixels));
	    textField14.setText(String.valueOf(myCam.dataM.conf));   
    } // updateCOnScreen() 
    
	void button4_MouseClick(java.awt.event.MouseEvent event)
	{
		// getMean (become evil?)
		boolean returnVal;
		this.getInputs();		
		returnVal = myCam.getMean();		
		if (returnVal) { 
		    textField2.setText("getMean succeeded!");
		    this.updateAOnScreen();		    		    
		} else {
		    textField2.setText("getMean command failed");
		}
	}
	
	void updateAOnScreen()
	{ // updates the fields on the screen for most recent A data
		    textField3.setText(String.valueOf(myCam.dataA.i1mean));		    
		    textField4.setText(String.valueOf(myCam.dataA.i2mean));		    
		    textField5.setText(String.valueOf(myCam.dataA.i3mean));		    
		    textField6.setText(String.valueOf(myCam.dataA.i1dev));		    
		    textField7.setText(String.valueOf(myCam.dataA.i2dev));		    
		    textField8.setText(String.valueOf(myCam.dataA.i3dev));		    		    
	} // updateAOnScreen

	void button5_MouseClick(java.awt.event.MouseEvent event)
	{
		//checkCamera button
		boolean returnbool;
		returnbool = myCam.checkCamera();
		if (returnbool) textField2.setText("checkCamera succeeded!");
		else textField2.setText("checkCamera failed!");						
	}

	void button8_MouseClick(java.awt.event.MouseEvent event)
	{
		// setWindow
		boolean returnBool;
		this.getInputs();
		returnBool = myCam.setWindow(inputx1, inputy1, inputx2, inputy2);
		if (returnBool) {
		    textField2.setText("setWindow succeeded");
		    this.winx1 = inputx1; this.winy1 = inputy1;
		    this.winx2 = inputx2; this.winy2 = inputy2;
		    this.drawWindow();
		    
		} else {
		    textField2.setText("setWindow command failed!");
		}				
	}
	
	// draws a huge green box to show the window set by setWindow
	void drawWindow()
	{
	    int tx1, tx2, ty1, ty2; // rotated coordinate frame
	    tx1 = 80 - winx2;
	    tx2 = 80 - winx1;
	    ty1 = winy1;
	    ty2 = winy2;
	    Graphics g = this.getGraphics();
	    g.setColor(Color.green);
	    g.fillRect(anchorX+(multX*tx1),anchorY+(multY*ty1),
	               multX*(tx2-tx1),multY*(ty2-ty1));	
        g.dispose();	               
	} // drawWindow() //
	
	// draws a big red box at the target found by trackColor //
	void drawTarget()
	{
	    int tx1, tx2, ty1, ty2;
	    if (myCam.dataM == null) return;
	    if (myCam.dataM.conf < 5) return; // if too low confidence, forget it
	    tx1 = 80 - myCam.dataM.x2;
	    tx2 = 80 - myCam.dataM.x1;
	    ty1 = myCam.dataM.y1;
	    ty2 = myCam.dataM.y2;	    
	    Graphics g = this.getGraphics();
	    g.setColor(Color.red);
	    g.fillRect(anchorX+(multX*tx1),anchorY+(multY*ty1),
	               multX*(tx2-tx1),multY*(ty2-ty1));
	    g.dispose();	    
	} // drawTarget() //
			
	void refreshGraphics()
	{
	    //redraw all top-left graphics //
	    //Graphics g = this.getGraphics();
	    //g.setColor(Color.black);
	    //g.fillRect(anchorX-9, anchorY-9, (80*multX)+20, (143*multY)+20);
	    //this.drawWindow();
	    //this.drawTarget();
	    //g.dispose();
	} // refreshGraphics() //
	
	public void paint(Graphics g)
	{
	    this.refreshGraphics();
	} // paint()
	
	void button6_MouseClick(java.awt.event.MouseEvent event)
	{
		// resetCamera
		boolean retval;
		retval = myCam.resetCamera();
		if (retval) {
		    textField2.setText("resetCamera succeeded");
		    winx1 = 1; winy1 = 1; winx2 = 80; winy2 = 143;
		    this.drawWindow();
		} else {
		    textField2.setText("resetCamera command failed");
		}		
	}
	
	void button11_MouseClick(java.awt.event.MouseEvent event)
	{
		// Grab frame -or a frame's worth of columns over many camera
		// frames- and then draw on screen color picture and RGB bands
		boolean retval;
		retval = myCam.grabFrame(winx1, winy1, winx2, winy2);
		if (retval) {
            textField2.setText("Frame Grabbed successfully. Drawing.");        		
		    this.paintSingleFrame();
		    this.paint3Bands();
		} else {
		    textField2.setText("grabFrame command failed!");
		}
    }
    
	public void paintSingleFrame() {
	    // this draws whatever's in FrameMem to the screen, single image //
	    // note that it limits the image only to the part within the last
	    // set window!
	    int xpos, ypos, yindex, rval, gval, bval;
	    int ymax = anchorY + (multY * 143);
	    Graphics g = this.getGraphics();
	    for (xpos = 0; xpos < 80; xpos++)
	        for (yindex = 0, ypos=0; yindex < 430; yindex+=3, ypos++) {
	            rval = myCam.frameMem[xpos][yindex];
	            gval = myCam.frameMem[xpos][yindex+1];
	            bval = myCam.frameMem[xpos][yindex+2];
	    	    g.setColor(new Color(rval, gval, bval));	    	   
	            g.fillRect(anchorX+(multX*xpos),ymax-(multY*ypos),
	                    multX,multY);
	        } // end for (yindex...)
	} // paintSingleFrame	
	
	public void paint3Bands() {
	    // this draws whatever's in FrameMem to the screen, 
	    // doing 3 frames vertically placed on top of one-another
	    int xpos, ypos, yindex, rval, gval, bval;
	    int xSize, newAnchorX, ySize;    
	    Graphics g = this.getGraphics();
	    xSize = (multX * 80) / 3;
	    ySize = 144;
	    newAnchorX = (multX * 80) + anchorX + 20;
	    int ymax1 = anchorY + 143;
	    int ymax2 = ySize + anchorY + 143;
	    int ymax3 = (2*ySize) + anchorY + 143;	    
	    for (xpos = 0; xpos < 80; xpos++)
	        for (yindex = 0, ypos=0; yindex < 430; yindex+=3, ypos++) {
	            rval = myCam.frameMem[xpos][yindex];
	            gval = myCam.frameMem[xpos][yindex+1];
	            bval = myCam.frameMem[xpos][yindex+2];
	    	    g.setColor(new Color(rval, rval, rval));
	            g.fillRect(newAnchorX+(2*xpos),ymax1 -(1*ypos),
	               2,1);	
	    	    g.setColor(new Color(gval, gval, gval));
	            g.fillRect(newAnchorX+(2*xpos),ymax2-(1*ypos),
	               2,1);	               	              
	    	    g.setColor(new Color(bval, bval, bval));
	            g.fillRect(newAnchorX+(2*xpos),ymax3-(1*ypos),
	               2,1);	               	              
	               
	        }
	} // paint3Bands	

	// this is a cool demo program to track red objects about!
	void button10_MouseClick(java.awt.event.MouseEvent event)
	{
		// This searches in the space of TC parameters, starting with the most
		// red possible and relaxing from there. If it fails to find red, it starts
		// relaxing the G and B bounds up and keeps trying, eventually giving up or
		// succeeding!
		// We assume you've already setup the camera (resetCamera)
		boolean retval;
		textField2.setText("Beginning FindRed demo loop. wave something red!");
		retval = this.findRed();
		for (int i=0; i<30; i++) {		
		    retval = myCam.commandTypeM("tc", myCam.dataM); // this calls TC with same parms //
		    if (retval) {
		     if (myCam.dataM.conf > 8) {
	            this.updateMOnScreen(); // refresh fields on screen
	            this.drawWindow();
	            this.drawTarget();                               
             } else {
		        retval = this.findRed(); // no joy. search again for red.
		     }
		    } else {  
		    retval = this.findRed(); }
		} // end for
        textField2.setText("FindRed loop finished.");        		
	} // end button10_MouseClick
	
	boolean findRed()
	{
		// This searches in the space of TC parameters, starting with the most
		// red possible and relaxing from there. If it fails to find red, it starts
		// relaxing the G and B bounds up and keeps trying, eventually giving up or
		// succeeding!
		// We assume you've already setup the camera (autogain on) with the setup 
		// camera command..
		int rthresh = 200; int gbthresh = 50; 
		boolean retval;
		while (true) {		
		    retval = myCam.trackColor(rthresh, 240, 16, gbthresh, 16, gbthresh);
	        // if no real target, keep trying...
	        if (retval) {
	            
	         if ((myCam.dataM.conf > 10) && (myCam.dataM.x2-myCam.dataM.x1>4) && 
			    (myCam.dataM.y2-myCam.dataM.y1>4)) {			   			    
			    bestRThresh = rthresh;
			    this.updateMOnScreen(); // refresh fields on screen
	            this.drawWindow();
	            this.drawTarget(); 
	            return true;    			    
			 } // end if (myCam.dataM.conf > 10) //
			  //no target found yet. If red threshold is too low, quit
			    if (rthresh < 130) { 
			                        return false; }
			    //otherwise reduce rthresh and keep trying!//
			    if (gbthresh > 100) {
			     rthresh -= 25;
			     gbthresh = 50;
			    } else { 
			    gbthresh += 30;	}
			} // end if (retval) //
		} // end while (true) //	    
	} // findRed() //		
		
} // CLASS camControlPanel //


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -