 import win.ui;
/*DSG{{*/
var frmOcr = win.form(text="百度API测试";right=757;bottom=513;bgcolor=16777215)
frmOcr.add(
btnClipOcr={cls="button";text="剪贴板图像转文本";left=401;top=460;right=610;bottom=498;db=1;dr=1;z=5};
btnOcr={cls="button";text="文字识别";left=596;top=411;right=731;bottom=449;db=1;dr=1;z=3};
edit={cls="edit";left=16;top=18;right=731;bottom=379;autohscroll=false;db=1;dl=1;dr=1;dt=1;edge=1;multiline=1;vscroll=1;z=1};
editUrl={cls="edit";left=16;top=415;right=591;bottom=447;align="right";db=1;dl=1;dr=1;edge=1;z=2};
static={cls="static";text="请输入图像文件路径(可直接拖放文件到窗口上)：";left=21;top=392;right=360;bottom=413;db=1;dl=1;transparent=1;z=4}
)
/*}}*/

var orc = function(frmOcr,image){
    import baidu.client;
    var http =  baidu.client(); 
    
    /*
    支持下面三种写法：
    
    http.setAuth( 这里直接写access_token )
    http.setAuth( 这里写一个可以获取access_token的网址 )
    http.setAuth( "你自己的API Key","你自己的Secret Key" )
    ) 
    */
    http.setAuth("http://update.aau.cn/v10/test/test-baidu.aardio")
        
    //OCR识别接口
    var ocr = http.api("https://aip.baidubce.com/rest/2.0/ocr/v1/");
    var result = ocr.accurate_basic( 
        image = image;
    )
    
    if(result.words_result){
    	for i,v in table.eachIndex(result.words_result){
        	frmOcr.edit.appendText(v.words) 
    	}    	
    }
    
    frmOcr.btnOcr.disabledText = null;	
    frmOcr.btnClipOcr.disabledText = null;	
}


frmOcr.btnOcr.oncommand = function(id,event){
    
    frmOcr.btnOcr.disabledText = "正在识别中"
	thread.invoke( orc,frmOcr,frmOcr.editUrl.text );
}

frmOcr.onDropFiles = function(files){
    frmOcr.editUrl.text = files[1]
}

import win.clip;
frmOcr.btnClipOcr.oncommand = function(id,event){
    var hbmp = win.clip.readBitmap();
    if(!hbmp){
    	frmOcr.msgboxErr("剪贴板中没有图像,请先复制图像!")
    	frmOcr.btnClipOcr.disabledText = null;
    	return;
    }
    
    import gdip.bitmap
    var img  = gdip.bitmap(hbmp);
    var buffer = img.saveToBuffer();
    		
    frmOcr.btnClipOcr.disabledText = "正在识别中"    	 
    thread.invoke( orc,frmOcr,buffer );
}

frmOcr.setValue = function(v){
	frmOcr.translateText = v;
}

frmOcr.getValue = function(){
    if( #string.trim(frmOcr.edit.text) ){
    	return frmOcr.edit.text; 
    }
	
	return frmOcr.translateText;
}

frmOcr.show() 
win.loopMessage();