界面中用线程控件操作摄像头(threadCtrl)

By admin at 2018-05-14 • 0人收藏 • 1621人看过

以前从没用过这个线程控件, 这次因为用到了摄像头,常规的写法如果界面中有两个摄像头窗口,那么就需要开启多线程来分别操作。

嗯,我之前就是这么做的,这个时候就有个问题了,如果也使用了前面写的那个plus拖动摄像头区域的功能,那么代码是非常多的,两个都这样做的话,代码量非常恐怖。

关键是这样的话不利于管理。

那么这个线程控件刚好可以解决这个问题,

工程中新建一个窗体A,里面拖出一个plus控件,然后把之前写的halcon摄像头那个复制到这个里面。 然后在主界面中拖动出一个custom控件,打开这个cusntom代码编辑模式,手动修改里面的代码,增加loadform=“res/A.aardio”这个属性,再把这个控件的cls="thread",这样就启用了线程控件了。

用同样的办法,生成一个B的线程控件。

B的窗体代码也像A的一样,不过,这里开启摄像头就修改了另外一个就可以了。

这时候,运行主界面,就会看到两个摄像头窗口,分别都可以进行操作,不会干扰。

threadCtrl={cls="thread";text="custom";left=1;top=3;right=594;bottom=493;loadForm="\res\videoy.aardio";z=13};


额,先记录到这里,以后再更新下。

睡觉

2 个回复 | 最后更新于 2018-05-20
2018-05-19   #1

摄像头winform子界面代码:

import win.ui;
/*DSG{{*/
var winform = win.form(text="aardio form";right=1033;bottom=652;parent=...)
winform.add(
plus={cls="plus";left=0;top=0;right=1034;bottom=653;db=1;dl=1;dr=1;dt=1;notify=1;z=1}
)
/*}}*/

import com;
var HOperatorSetX = com.CreateObject("{6ebd90e2-d219-11d2-ade5-0000c00f4ef9}")
  
var AcqHandle = HOperatorSetX.OpenFramegrabber( 'DirectShow', 1, 1, 0, 0, 0, 0, 'default', 8, 'rgb', -1, 'false', 'default', '', 0, -1 );
HOperatorSetX.GrabImageStart( AcqHandle , -1 );
var Image = HOperatorSetX.GrabImageAsync( AcqHandle , -1 );
//获取图像宽和高
var Width , Height = HOperatorSetX.GetImageSize( Image );
//console.log(Width , Height)
//调整画面大小和图像一致
var WindowHandle = HOperatorSetX.OpenWindow( 0 , 0 , winform.plus.width , winform.plus.height , winform.plus.hwnd , "transparent","" );
  
var videoShow = true;
 
//定义显示图像的左上点初始坐标
var leftUpX = (Width-winform.plus.width)/2;
var leftUpY = (Height-winform.plus.height)/2;
 
//设置图像显示区域
function 图像调整(){
    var posX,posY = leftUpX,leftUpY;
    var moveImg = function(x=0,y=0){
        //X轴调整
        if(x<0){
            if( posX<=0 ){ }
            else {
                posX = posX + x;
                if(posX<0) posX = 0;
            };  
        }else {
            if( posX >= (Width-winform.plus.width) ){}
            else {
                posX = posX + x;
                if(posX >(Width-winform.plus.width)) posX = Width-winform.plus.width;
            }
        }
        //Y轴调整
        if(y<0){
            if( posY<=0 ){ }
            else {
                posY = posY + y;
                if( posY<0 ) posY = 0;
            };  
        }else {
            if( posY>=(Height-winform.plus.height) ){}
            else {
                posY = posY + y;
                if( posY>(Height-winform.plus.height) ) posY = Height-winform.plus.height;
            }
        }
        HOperatorSetX.SetPart( WindowHandle , posY , posX , winform.plus.height+posY , winform.plus.width+posX );
    }
    return moveImg;
 }
moveImg = 图像调整();
 
var startX,startY;
var drawX,drawY = 0,0;
winform.plus.onMouseDown = function(wParam,lParam){ 
    winform.plus.capture = true;
    startX,startY = ..win.getMessagePos(lParam);
    win.ui.waitCursor(true,::User32.LoadCursor(null,0x7F86/*_IDC_SIZEALL*/));
}
  
winform.plus.onMouseClick = function(wParam,lParam){
    winform.plus.capture = false;
    win.ui.waitCursor(false);
}
  
winform.plus.onMouseMove = function(wParam,lParam){
    if( wParam & 0x1/*_MK_LBUTTON*/ ){
        var x,y = win.getMessagePos(lParam); 
        drawX = startX-x;
        drawY = startY-y;
        moveImg(drawX,drawY);
        startX,startY = x,y;
    }
}
 
 var tmId = winform.addtimer( 
 	30,
 	function(hwnd,msg,id,tick){
 		moveImg(0,0);
 		while(win.delay(30)&&videoShow){
        	Image = HOperatorSetX.GrabImageAsync( AcqHandle , -1 );
            HOperatorSetX.DispObj( Image , WindowHandle );
            HOperatorSetX.ClearObj( Image );

 	
    	}
 	} 
 )
 
 
/*
winform.button.oncommand = function(id,event){
    moveImg(0,0);
    var checkflag = 0;
    while(win.delay(30)&&videoShow){
        Image = HOperatorSetX.GrabImageAsync( AcqHandle , -1 );
        //镜像图像
        if(winform.zuoyou.checked){
            if(winform.shangxia.checked){
                checkflag = 3;    //全选
            }else {
                checkflag = 1;    //左右
            }
                 
        }else {
            if(winform.shangxia.checked){
                checkflag = 2;    //上下
            }else {
                checkflag = 0;    //未选
            }
        }
 
        select(checkflag) {
            case 0 {
                HOperatorSetX.DispObj( Image , WindowHandle );
                HOperatorSetX.ClearObj( Image );
            }
            case 1 {
                ZImageMirror = HOperatorSetX.MirrorImage( Image , "column" );
                HOperatorSetX.DispObj( ZImageMirror , WindowHandle );
                HOperatorSetX.ClearObj( ZImageMirror );
                HOperatorSetX.ClearObj( Image );
            }
            case 2 {
                RImageMirror = HOperatorSetX.MirrorImage( Image , "row" );
                HOperatorSetX.DispObj( RImageMirror , WindowHandle );
                HOperatorSetX.ClearObj( RImageMirror );
                HOperatorSetX.ClearObj( Image );
            }
            case 3 {
                ZImageMirror = HOperatorSetX.MirrorImage( Image , "column" );
                RImageMirror = HOperatorSetX.MirrorImage( ZImageMirror , "row" );
                HOperatorSetX.DispObj( RImageMirror , WindowHandle );
                HOperatorSetX.ClearObj( ZImageMirror );
                HOperatorSetX.ClearObj( RImageMirror );
                HOperatorSetX.ClearObj( Image );
            }
            else {
            }
        }
 
    }
  
}
*/

  
winform.wndproc = function(hwnd,message,wParam,lParam){
    select( message ) { 
        case 0x10/*_WM_CLOSE*/{
            if(videoShow){
                videoShow = false;
                win.delay(500)
                HOperatorSetX.CloseFramegrabber( AcqHandle );
                }
              
        }
        else{
              
        }
    }
    //无返回值则继续调用默认回调函数
}
  





winform.enableDpiScaling();
winform.show();

win.loopMessage();
return winform;


2018-05-20   #2

更新:

增加videoX中可以画线功能

import win.ui;
/*DSG{{*/
var winform = win.form(text="aardio form";right=597;bottom=401;parent=...)
winform.add(
plus={cls="plus";left=0;top=0;right=598;bottom=402;db=1;dl=1;dr=1;dt=1;notify=1;z=1}
)
/*}}*/

import com;
var HOperatorSetX = com.CreateObject("{6ebd90e2-d219-11d2-ade5-0000c00f4ef9}")
  
var AcqHandle = HOperatorSetX.OpenFramegrabber( 'DirectShow', 1, 1, 0, 0, 0, 0, 'default', 8, 'rgb', -1, 'false', 'default', '[1]', 0, -1 );
HOperatorSetX.GrabImageStart( AcqHandle , -1 );
var Image = HOperatorSetX.GrabImageAsync( AcqHandle , -1 );
//获取图像宽和高
var Width , Height = HOperatorSetX.GetImageSize( Image );
//console.log(Width , Height)
//调整画面大小和图像一致
var WindowHandle = HOperatorSetX.OpenWindow( 0 , 0 , winform.plus.width , winform.plus.height , winform.plus.hwnd , "transparent","" );
  
var videoShow = true;
 
//定义显示图像的左上点初始坐标
var leftUpX = (Width-winform.plus.width)/2;
var leftUpY = (Height-winform.plus.height)/2;
 
//设置图像显示区域
function 图像调整(){
    var posX,posY = leftUpX,leftUpY;
    var moveImg = function(x=0,y=0){
        //X轴调整
        if(x<0){
            if( posX<=0 ){ }
            else {
                posX = posX + x;
                if(posX<0) posX = 0;
            };  
        }else {
            if( posX >= (Width-winform.plus.width) ){}
            else {
                posX = posX + x;
                if(posX >(Width-winform.plus.width)) posX = Width-winform.plus.width;
            }
        }
        //Y轴调整
        if(y<0){
            if( posY<=0 ){ }
            else {
                posY = posY + y;
                if( posY<0 ) posY = 0;
            };  
        }else {
            if( posY>=(Height-winform.plus.height) ){}
            else {
                posY = posY + y;
                if( posY>(Height-winform.plus.height) ) posY = Height-winform.plus.height;
            }
        }
        HOperatorSetX.SetPart( WindowHandle , posY , posX , winform.plus.height+posY , winform.plus.width+posX );
    }
    return moveImg;
 }
moveImg = 图像调整();
 
var startX,startY;
var drawX,drawY = 0,0;
winform.plus.onMouseDown = function(wParam,lParam){ 
    winform.plus.capture = true;
    if(thread.get("划线")){
    	win.ui.waitCursor(true,::User32.LoadCursor(null,0x7F03/*_IDC_CROSS*/));
    }else {
    	startX,startY = ..win.getMessagePos(lParam);
    	win.ui.waitCursor(true,::User32.LoadCursor(null,0x7F86/*_IDC_SIZEALL*/));
    }
}
  
winform.plus.onMouseClick = function(wParam,lParam){
    winform.plus.capture = false;
    win.ui.waitCursor(false);
}
  
winform.plus.onMouseMove = function(wParam,lParam){
    if(!thread.get("划线")){
    	if( wParam & 0x1/*_MK_LBUTTON*/ ){
        	var x,y = win.getMessagePos(lParam); 
        	drawX = startX-x;
        	drawY = startY-y;
        	moveImg(drawX,drawY);
        	startX,startY = x,y;
    	}
    }
}
 
//var hasLine = false;
var tmId = winform.addtimer( 
	30,
	function(hwnd,msg,id,tick){
 		moveImg(0,0);
 		while(win.delay(30)&&videoShow){
        	Image = HOperatorSetX.GrabImageAsync( AcqHandle , -1 );
        	HOperatorSetX.DispObj( Image , WindowHandle );
        	HOperatorSetX.ClearObj( Image );
        	if(thread.get("划线")){
        		HOperatorSetX.SetColor( WindowHandle , "red" );
        		HOperatorSetX.SetLineWidth( WindowHandle , 10 );
            	Row1 , Column1 , Row2 , Column2 = HOperatorSetX.DrawLine( WindowHandle );
            	//HOperatorSetX.DispLine( WindowHandle , Row1 , Column1 , Row2 , Column2 );
            	thread.set("划线", false);
            	hasLine = true;
        	}
        	if(hasLine){
        		
        		HOperatorSetX.DispLine( WindowHandle , Row1 , Column1 , Row2 , Column2 );
        	}
	
	
    	}
	} 
)
 
 
/*
winform.button.oncommand = function(id,event){
    moveImg(0,0);
    var checkflag = 0;
    while(win.delay(30)&&videoShow){
        Image = HOperatorSetX.GrabImageAsync( AcqHandle , -1 );
        //镜像图像
        if(winform.zuoyou.checked){
            if(winform.shangxia.checked){
                checkflag = 3;    //全选
            }else {
                checkflag = 1;    //左右
            }
                 
        }else {
            if(winform.shangxia.checked){
                checkflag = 2;    //上下
            }else {
                checkflag = 0;    //未选
            }
        }
 
        select(checkflag) {
            case 0 {
                HOperatorSetX.DispObj( Image , WindowHandle );
                HOperatorSetX.ClearObj( Image );
            }
            case 1 {
                ZImageMirror = HOperatorSetX.MirrorImage( Image , "column" );
                HOperatorSetX.DispObj( ZImageMirror , WindowHandle );
                HOperatorSetX.ClearObj( ZImageMirror );
                HOperatorSetX.ClearObj( Image );
            }
            case 2 {
                RImageMirror = HOperatorSetX.MirrorImage( Image , "row" );
                HOperatorSetX.DispObj( RImageMirror , WindowHandle );
                HOperatorSetX.ClearObj( RImageMirror );
                HOperatorSetX.ClearObj( Image );
            }
            case 3 {
                ZImageMirror = HOperatorSetX.MirrorImage( Image , "column" );
                RImageMirror = HOperatorSetX.MirrorImage( ZImageMirror , "row" );
                HOperatorSetX.DispObj( RImageMirror , WindowHandle );
                HOperatorSetX.ClearObj( ZImageMirror );
                HOperatorSetX.ClearObj( RImageMirror );
                HOperatorSetX.ClearObj( Image );
            }
            else {
            }
        }
 
    }
  
}
*/

  
winform.wndproc = function(hwnd,message,wParam,lParam){
    select( message ) { 
        case 0x10/*_WM_CLOSE*/{
            if(videoShow){
                videoShow = false;
                win.delay(500)
                HOperatorSetX.CloseFramegrabber( AcqHandle );
                }
              
        }
        else{
              
        }
    }
    //无返回值则继续调用默认回调函数
}
  


//winform.enableDpiScaling();
winform.show();

win.loopMessage();
return winform;


登录后方可回帖

登 录
信息栏
 私人小站

本站域名

ChengXu.XYZ

投诉联系:  popdes@126.com



快速上位机开发学习,本站主要记录了学习过程中遇到的问题和解决办法及上位机代码分享

这里主要专注于学习交流和经验分享.
纯私人站,当笔记本用的,学到哪写到哪.
如果侵权,联系 Popdes@126.com

友情链接
Aardio官方
Aardio资源网


才仁机械


网站地图SiteMap

Loading...