模拟鼠标拖动plus前景图片调整图像显示区域

By admin at 2018-05-12 • 0人收藏 • 1459人看过

基本算是花了一天时间实现的

准备用于摄像头图像显示区域调整上面,这个是简易版功能

blob.png

import win.ui;
/*DSG{{*/
var winform = win.form(text="鼠标拖动图像调整显示区域测试";right=639;bottom=377)
winform.add(
plus={cls="plus";left=126;top=102;right=490;bottom=268;border={color=-16777216;width=1};clipBk=false;clipch=1;foreRepeat="center";notify=1;z=1}
)
/*}}*/

var bmp = gdip.bitmap( "http://bbs.aardio.com/data/attachment/forum/201804/28/234719vy3uyoqq65bb22lb.jpg.thumb.jpg" );
//定义显示图像的左上点初始坐标
var leftUpX = (bmp.width-winform.plus.width)/2;
var leftUpY = (bmp.height-winform.plus.height)/2;

//设置图像显示区域
function 图像调整(){
    var posX,posY = -leftUpX,-leftUpY;
    var moveImg = function(graphics,x=0,y=0){
        //X轴调整
        if(x>=0){
        	if( posX>=0 ){ }
        	else {
        		posX = posX + x;
        		if(posX>0) posX = 0;
        	};	
        }else {
        	if( posX <= (winform.plus.width-bmp.width) ){}
        	else {
        		posX = posX + x;
        		if(posX <= (winform.plus.width-bmp.width)) posX = winform.plus.width-bmp.width;
        	}
        }
        //Y轴调整
        if(y>=0){
        	if( posY>=0 ){ }
        	else {
        		posY = posY + y;
        		if( posY>=0 ) posY = 0;
        	};	
        }else {
        	if( posY<=(winform.plus.height - bmp.height) ){}
        	else {
        		posY = posY + y;
        		if( posY<=(winform.plus.height - bmp.height) ) posY = winform.plus.height - bmp.height;
        	}
        }
        graphics.drawImage( bmp,posX,posY);
    }
    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 = x-startX;
        drawY = y-startY;
        winform.plus.redraw();
        startX,startY = x,y;
    }
}

//winform.plus.background = 0xFFFFFFFF;
winform.plus.onDrawContent = function(graphics,rc){
    moveImg(graphics,drawX,drawY);
}

winform.show() 
win.loopMessage();


1 个回复 | 最后更新于 2018-05-13
2018-05-13   #1

整合到halcon摄像头里去了,plus自绘图像和摄像头图像区域设置,方向刚好相反的,

顺手加了摄像头的一些东西。


import win.ui;
/*DSG{{*/
mainForm = win.form(text="Halcon摄像头功能测试";right=969;bottom=646;parent=...)
mainForm.add(
button={cls="button";text="开启摄像头";left=31;top=549;right=193;bottom=635;z=1};
button2={cls="button";text="关闭摄像头";left=208;top=549;right=370;bottom=635;z=2};
plus={cls="plus";left=0;top=141;right=966;bottom=366;notify=1;z=3};
shangxia={cls="checkbox";text="上下镜像";left=400;top=613;right=501;bottom=634;z=5};
zuoyou={cls="checkbox";text="左右镜像";left=400;top=586;right=501;bottom=607;checked=1;z=4}
)
/*}}*/

import com;
var HOperatorSetX = com.CreateObject("{6ebd90e2-d219-11d2-ade5-0000c00f4ef9}")
 
import console
console.open()

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 , mainForm.plus.width , mainForm.plus.height , mainForm.plus.hwnd , "transparent","" );
 
var videoShow = true;

//定义显示图像的左上点初始坐标
var leftUpX = (Width-mainForm.plus.width)/2;
var leftUpY = (Height-mainForm.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-mainForm.plus.width) ){}
            else {
                posX = posX + x;
                if(posX >(Width-mainForm.plus.width)) posX = Width-mainForm.plus.width;
            }
        }
        //Y轴调整
        if(y<0){
            if( posY<=0 ){ }
            else {
                posY = posY + y;
                if( posY<0 ) posY = 0;
            };  
        }else {
            if( posY>=(Height-mainForm.plus.height) ){}
            else {
                posY = posY + y;
                if( posY>(Height-mainForm.plus.height) ) posY = Height-mainForm.plus.height;
            }
        }
        HOperatorSetX.SetPart( WindowHandle , posY , posX , mainForm.plus.height+posY , mainForm.plus.width+posX );
    }
    return moveImg;
 }
moveImg = 图像调整();

var startX,startY;
var drawX,drawY = 0,0;
mainForm.plus.onMouseDown = function(wParam,lParam){ 
    mainForm.plus.capture = true;
    startX,startY = ..win.getMessagePos(lParam);
    win.ui.waitCursor(true,::User32.LoadCursor(null,0x7F86/*_IDC_SIZEALL*/));
}
 
mainForm.plus.onMouseClick = function(wParam,lParam){
    mainForm.plus.capture = false;
    win.ui.waitCursor(false);
}
 
mainForm.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;
    }
}

mainForm.button.oncommand = function(id,event){
    moveImg(0,0);
    var checkflag = 0;
    while(win.delay(30)&&videoShow){
        Image = HOperatorSetX.GrabImageAsync( AcqHandle , -1 );
        //镜像图像
        if(mainForm.zuoyou.checked){
        	if(mainForm.shangxia.checked){
        		checkflag = 3;	//全选
        	}else {
        		checkflag = 1;	//左右
        	}
        		
        }else {
        	if(mainForm.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 {
        	}
        }

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

mainForm.enableDpiScaling();
mainForm.show();

return win.loopMessage();

这里需要注意:

上面获取check的部分,需要放置到halcon操作摄像头操作外面,要不然可能正在操作摄像头过程中变动check导致内存释放错误而崩溃,放到外面就是类似异步调整了。

登录后方可回帖

登 录
信息栏
 私人小站

本站域名

ChengXu.XYZ

投诉联系:  popdes@126.com



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

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

友情链接
Aardio官方
Aardio资源网


才仁机械


网站地图SiteMap

Loading...