web.kit获取图像数据的几种方法
编辑时间:2018-06-28 作者:金满斗 浏览量:2091 来源:原创

好长时间没做过模拟了,据说现在前端技术升级了,模拟也不好做了,没管它。

以前模拟web库是很好用的,但后来因为ie被越来越多的人舍弃,网站兼容性就没考虑ie了,这时候就需要kit了。

kit取网页上的图片就没用ie方便,这是我以前一般的方法

	imgScreen = soImage();
	
	//根据id网页上截取验证码
	getcode_id = function(id){
		var eCodeImg = wb.jQuery(id);
		x = eCodeImg.offset().left;
		y = eCodeImg.offset().top ;
		imgScreen.capture(..mainForm.custom.hwnd,x,y,eCodeImg.width(),eCodeImg.height());
		imgScreen.resize(60,20)
		imgScreen.save("/yzm.jpg");
	}

后来发现有人在论坛问,校长提供了方法,当然更科学更好。干脆和我第一种方法都融合一下。

import win.ui;
/*DSG{{*/
mainForm = win.form(text="WebKit - 获取img元素的数据";right=1108;bottom=716)
mainForm.add(
button={cls="button";text="方法1";left=293;top=636;right=407;bottom=690;z=3};
button2={cls="button";text="方法2";left=441;top=637;right=555;bottom=691;z=4};
custom={cls="custom";text="custom";left=20;top=17;right=890;bottom=530;z=2};
picturebox={cls="plus";left=13;top=631;right=255;bottom=694;edge=1;foreRepeat="stretch";transparent=1;z=1}
)
/*}}*/

import web.kit.form;
import console;
var wb = web.kit.form(mainForm.custom);
mainForm.show();

wb.go("http://jingang.yto56.com.cn/mdm_2/logout.action#");
wb.wait();





getIMG = function(id){
    var img = wb.getEle(id); 
	var canvas = wb.document.createElement("canvas");
	canvas.width = img.width;
	canvas.height = img.height;
	
	var ctx = canvas.getContext("2d");
	ctx.drawImage(img, 0, 0,canvas.width,canvas.height);
	var base64 = canvas.toDataURL("image/png");
	base64 = string.replace(base64,"^data\:image\/(\w+);base64,", "");
	
	import crypt.bin;
	var bmp = crypt.bin.decodeBase64(base64);
	console.dump(bmp)
	return bmp; 
}



	
	//根据id网页上截取验证码
getcode_id = function(id){
    import soImage;
	imgScreen = soImage();
	var eCodeImg = wb.jQuery(id);
	x = eCodeImg.offset().left;
	y = eCodeImg.offset().top ;
	imgScreen.capture(..mainForm.custom.hwnd,x,y,eCodeImg.width(),eCodeImg.height());
	imgScreen.resize(eCodeImg.width(),eCodeImg.height());
	bmp = imgScreen.getBytes("1.bmp")
	return bmp; 
}



mainForm.button.oncommand = function(id,event){
	bmp = getIMG("vildateImg");
	mainForm.picturebox.background = bmp;
}

mainForm.button2.oncommand = function(id,event){
	
	bmp = getcode_id("#vildateImg");
	mainForm.picturebox.background = bmp;
}

//mainForm.picturebox.orphanWindow();


win.loopMessage();


来说两句吧