TSFindPicture.cpp 165 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456
  1. /*
  2. 本源码由TC简单软件科技有限公司开源,功能可以自由修改、发布、
  3. 长沙简单软件科技有限公司对于源码不做后期维护,,请大家在使用过程中遵循开源协议
  4. */
  5. #include "stdafx.h"
  6. #include "TSRuntime.h"
  7. #include "TSFindPicture.h"
  8. #include "D3dx9tex.h"
  9. #include "DXBind.h"
  10. #include <GdiPlus.h>
  11. using namespace Gdiplus;
  12. #pragma comment(lib, "gdiplus.lib")
  13. extern HWND g_currentHwnd;
  14. MyFindPictureClass::MyFindPictureClass(void)
  15. {
  16. IsLastFindStr = false;
  17. m_displayMode = 0;
  18. m_hwnd = NULL;
  19. hDXBitmap = NULL;
  20. pLoadBmpBuffer = NULL;
  21. pWndBmpBuffer = NULL;
  22. pBuffer = NULL;
  23. pbuffer = NULL;
  24. loadWidth = 0;
  25. loadHeight = 0;
  26. wndWidth = 0;
  27. wndHeight = 0;
  28. nbuffersize = 0;
  29. m_helpXpoint = 0;
  30. m_helpYpoint = 0;
  31. m_parenthwnd = 0;
  32. memset(SetPath, 0, MAX_PATH);
  33. strColor = 0;
  34. //strColoroff=0;
  35. usingindex = 0;
  36. for (int i = 0; i < MAX_PATH * 10; i++)
  37. memset(addrxy[i], 0, 3);
  38. memset(strColors, 0, MAX_PATH);
  39. memset(strColoroff, 0, MAX_PATH);
  40. memset(m_colorOffR, 0, MAX_PATH);
  41. memset(m_colorOffG, 0, MAX_PATH);
  42. memset(m_colorOffB, 0, MAX_PATH);
  43. //memset(pWndBmpBuffer,0,sizeof(pWndBmpBuffer));
  44. //memset(SetPicPwdString,0,MAX_PATH);
  45. isprocessColor = false;//默认不是找色或者取色
  46. dictindex = 0;
  47. }
  48. MyFindPictureClass::~MyFindPictureClass(void)
  49. {
  50. }
  51. int MyFindPictureClass::GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
  52. {
  53. // Initialize GDI+.(初始化GDI,否则调用GDI函数会导致异常)
  54. GdiplusStartupInput gdiplusStartupInput;
  55. ULONG_PTR gdiplusToken;
  56. GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
  57. // Initialize GDI+.(初始化GDI,否则调用GDI函数会导致异常)
  58. UINT num = 0; // number of image encoders
  59. UINT size = 0; // size of the image encoder array in bytes
  60. ImageCodecInfo* pImageCodecInfo = NULL;
  61. GetImageEncodersSize(&num, &size);
  62. if (size == 0)
  63. {
  64. return -1; // Failure
  65. }
  66. pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
  67. if (pImageCodecInfo == NULL)
  68. return -1; // Failure
  69. GetImageEncoders(num, size, pImageCodecInfo);
  70. for (UINT j = 0; j < num; ++j)
  71. {
  72. if (wcscmp(pImageCodecInfo[j].MimeType, format) == 0)
  73. {
  74. *pClsid = pImageCodecInfo[j].Clsid;
  75. free(pImageCodecInfo);
  76. return j; // Success
  77. }
  78. }
  79. free(pImageCodecInfo);
  80. return -1; // Failure
  81. }
  82. bool MyFindPictureClass::BMP2PNG(wchar_t* strBMPFile, wchar_t* strPNGFile)
  83. {
  84. CLSID encoderClsid;
  85. WCHAR strGuid[39];
  86. if (GetEncoderClsid(L"image/png", &encoderClsid) >= 0)
  87. StringFromGUID2(encoderClsid, strGuid, 39);
  88. Image image(strBMPFile);
  89. image.Save(strPNGFile, &encoderClsid, NULL);
  90. return true;
  91. }
  92. bool MyFindPictureClass::BMP2JPG(wchar_t* strBMPFile, wchar_t* strJPGFile)
  93. {
  94. CLSID encoderClsid;
  95. WCHAR strGuid[39];
  96. if (GetEncoderClsid(L"image/jpeg", &encoderClsid) >= 0)
  97. StringFromGUID2(encoderClsid, strGuid, 39);
  98. Image image(strBMPFile);
  99. image.Save(strJPGFile, &encoderClsid, NULL);
  100. //关闭GDI
  101. //GdiplusShutdown(gdiplusToken);
  102. return true;
  103. }
  104. bool MyFindPictureClass::loadBitmap(wchar_t* path)
  105. {
  106. //std::string rstr;
  107. BYTE* rstr = NULL;
  108. int rstrlen = 0;
  109. BITMAPFILEHEADER fileheader = { 0 };
  110. HANDLE file_Handle = CreateFile(path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  111. if (file_Handle == INVALID_HANDLE_VALUE)
  112. {
  113. int err = ::GetLastError();
  114. if (TSRuntime::IsShowErrorMsg)
  115. {
  116. char erro[MAX_PATH] = { 0 };
  117. USES_CONVERSION;
  118. sprintf(erro, "加载:%s,位图失败", W2A(path));
  119. ::MessageBoxA(NULL, erro, "TS", 0);
  120. }
  121. CloseHandle(file_Handle);
  122. return false;
  123. }
  124. DWORD dword = 0, ReadFileSize = 0;
  125. DWORD fileszie = ::GetFileSize(file_Handle, NULL);
  126. rstr = new BYTE[fileszie + 1];
  127. memset(rstr, 0, sizeof(BYTE) * fileszie);
  128. {
  129. ::ReadFile(file_Handle, rstr, fileszie, &ReadFileSize, NULL);
  130. }
  131. CloseHandle(file_Handle);
  132. BITMAPINFOHEADER head;
  133. DWORD headSize = sizeof(BITMAPINFOHEADER);
  134. memcpy(&head, &rstr[sizeof(BITMAPFILEHEADER)], sizeof(BITMAPINFOHEADER));
  135. loadWidth = head.biWidth;
  136. loadHeight = head.biHeight;
  137. WORD biBitCount = head.biBitCount;
  138. if (biBitCount != 24)
  139. {
  140. if (TSRuntime::IsShowErrorMsg)
  141. ::MessageBoxA(NULL, "请选择24位位图!", "TS", 0);
  142. return false;
  143. }
  144. int totalSize = (loadWidth * biBitCount / 8 + 3) / 4 * 4 * loadHeight;
  145. //BYTE *pBmpBuf = new BYTE[totalSize];
  146. //memset(pBmpBuf,0,sizeof(BYTE)*totalSize);
  147. size_t size = 0;
  148. size = rstrlen - headSize - sizeof(BITMAPFILEHEADER);
  149. BYTE* pBmpBuf = &rstr[headSize + sizeof(BITMAPFILEHEADER)];
  150. //memcpy(pBmpBuf,&rstr[headSize+sizeof(BITMAPFILEHEADER)],size);
  151. pLoadBmpBuffer = new COLORREF * [loadHeight]; ///// 二维数组 用来存储图像的颜色值
  152. for (int i = 0; i < loadHeight; i++)
  153. {
  154. pLoadBmpBuffer[i] = new COLORREF[loadWidth];
  155. }
  156. COLORREF helpcolor = 0;
  157. int pitch = loadWidth % 4;
  158. //HDC hDC = ::GetWindowDC(::GetDesktopWindow());
  159. for (int i = 0; i < loadHeight; i++)
  160. {
  161. int realPitch = i * pitch;
  162. for (int j = 0; j < loadWidth; j++)
  163. {
  164. UCHAR b = pBmpBuf[(i * loadWidth + j) * 3 + realPitch];
  165. UCHAR g = pBmpBuf[(i * loadWidth + j) * 3 + 1 + realPitch];
  166. UCHAR r = pBmpBuf[(i * loadWidth + j) * 3 + 2 + realPitch];
  167. helpcolor = RGB(r, g, b);
  168. pLoadBmpBuffer[loadHeight - i - 1][j] = helpcolor;
  169. }
  170. }
  171. delete[] rstr;
  172. return true;
  173. }
  174. //
  175. //bool MyFindPictureClass::loadBitmap(wchar_t* path)
  176. // {
  177. // //TSRuntime::add_log( "loadBitmap");
  178. // ///////////////////////////加载的位图 //////////////////////////
  179. // HBITMAP hbmp = (HBITMAP)LoadImage(NULL,path,IMAGE_BITMAP,0,0,LR_LOADFROMFILE );
  180. // if(hbmp==NULL)
  181. // {
  182. // //::MessageBox(NULL,L"加载位图失败",path,0);
  183. // return false;
  184. // }
  185. //
  186. // BITMAP bitmap;
  187. // GetObject(hbmp, sizeof(BITMAP), &bitmap);
  188. //
  189. // BITMAPINFO info;
  190. // HDC dc;
  191. // dc=::CreateDC(L"DISPLAY",NULL,NULL,NULL);
  192. //
  193. // // 24位图的BITMAPINFO
  194. // BITMAPINFO *pBITMAPINFO = (BITMAPINFO*)malloc(sizeof(BITMAPINFOHEADER));
  195. // memset(pBITMAPINFO, 0, sizeof(BITMAPINFOHEADER));
  196. // BITMAPINFOHEADER *pInfo_Header = (BITMAPINFOHEADER *)pBITMAPINFO;
  197. // pInfo_Header->biSize = sizeof(BITMAPINFOHEADER);
  198. // pInfo_Header->biWidth =bitmap.bmWidth;
  199. // pInfo_Header->biHeight = bitmap.bmHeight;
  200. // pInfo_Header->biPlanes = 1;
  201. // pInfo_Header->biBitCount = 24;
  202. // pInfo_Header->biCompression = BI_RGB;
  203. //
  204. // long width = bitmap.bmWidth;
  205. // long height = bitmap.bmHeight;
  206. // info.bmiHeader.biBitCount=24;
  207. //
  208. // loadWidth = width;
  209. // loadHeight = height;
  210. //
  211. // DWORD bufSize = ( width * 3 + 3) / 4 * 4 * height;
  212. // BYTE *buffer=new BYTE[bufSize];
  213. // memset(buffer,0,sizeof(BYTE)*bufSize);
  214. // if(!GetDIBits(dc, hbmp, 0, bitmap.bmHeight, buffer, pBITMAPINFO, DIB_RGB_COLORS))
  215. // {
  216. // if(TSRuntime::IsShowErrorMsg)
  217. // ::MessageBox(NULL,L"加载位图失败",L"TC",0);
  218. // return false;
  219. // }
  220. // pLoadBmpBuffer = new COLORREF*[height]; ///// 二维数组 用来存储图像的颜色值
  221. // for(int i=0;i<height;i++)
  222. // {
  223. // pLoadBmpBuffer[i]=new COLORREF[width];
  224. // }
  225. // COLORREF helpcolor=0;
  226. // int pitch=width%4;
  227. // HDC hDC = ::GetWindowDC(::GetDesktopWindow());
  228. // for(int i=0;i<height;i++)
  229. // {
  230. // int realPitch=i*pitch;
  231. // for(int j=0;j<width;j++)
  232. // {
  233. // UCHAR b=buffer[(i*width+j)*3+realPitch];
  234. // UCHAR g=buffer[(i*width+j)*3+1+realPitch];
  235. // UCHAR r=buffer[(i*width+j)*3+2+realPitch];
  236. // helpcolor=RGB(r,g,b);
  237. // SetPixel(hDC,j,i,helpcolor);
  238. // pLoadBmpBuffer[height-i-1][j]=helpcolor;
  239. // }
  240. // }
  241. // ///////////////////////////获得加载位图 颜色成功 //////////////////////////
  242. // delete [] buffer;
  243. // ::DeleteDC(dc);
  244. // ::free(pBITMAPINFO);
  245. // ::DeleteObject(hbmp);
  246. // return true;
  247. // }
  248. bool MyFindPictureClass::SaveGDIBitmap(HWND hWnd, RECT rect, wchar_t* savepath)
  249. {
  250. HDC hDC;
  251. //hDC=::GetWindowDC(hWnd); //定义绘图用的设备环境
  252. hDC = GetDCEx(hWnd, NULL, DCX_PARENTCLIP);
  253. HDC hMemDC; //内存缓冲设备环境
  254. HBITMAP hbmMem, hbmOld; //内存缓冲设备环境中的位图
  255. RECT rc = rect;
  256. //判断边境值
  257. RECT clientrc;
  258. ::GetClientRect(hWnd, &clientrc);
  259. int xc = 0;
  260. int cx = 0;
  261. int cy = 0;
  262. if (rc.bottom > clientrc.bottom)
  263. rc.bottom = clientrc.bottom;
  264. if (rc.right > clientrc.right)
  265. rc.right = clientrc.right;
  266. //TSRuntime::add_log( "clientrc.right:%d,clientrc.bottom:%d,rect.left:%d,rect.top:%d,rect.right:%d,rect.bottom:%d",clientrc.right, clientrc.bottom,rc.left,rc.top,rc.right,rc.bottom);
  267. // 24位图的BITMAPINFO
  268. BITMAPINFO* pBITMAPINFO = (BITMAPINFO*)malloc(sizeof(BITMAPINFOHEADER));
  269. memset(pBITMAPINFO, 0, sizeof(BITMAPINFOHEADER));
  270. BITMAPINFOHEADER* pInfo_Header = (BITMAPINFOHEADER*)pBITMAPINFO;
  271. pInfo_Header->biSize = sizeof(BITMAPINFOHEADER);
  272. pInfo_Header->biWidth = rc.right - rc.left;
  273. pInfo_Header->biHeight = (rc.bottom - rc.top);
  274. pInfo_Header->biPlanes = 1;
  275. pInfo_Header->biBitCount = 24;
  276. pInfo_Header->biCompression = BI_RGB;
  277. hMemDC = CreateCompatibleDC(hDC); //创建内存兼容设备环境
  278. //创建内存兼容位图
  279. hbmMem = CreateCompatibleBitmap(hDC, pInfo_Header->biWidth, pInfo_Header->biHeight);
  280. hbmOld = (HBITMAP)SelectObject(hMemDC, hbmMem);
  281. if ((m_displayMode & DISPLAY_GDI2) == DISPLAY_GDI2)
  282. {
  283. RECT winret;
  284. ::GetWindowRect(hWnd, &winret);
  285. POINT point;
  286. point.x = 0;
  287. point.y = 0;
  288. ::ClientToScreen(hWnd, &point);
  289. cx = point.x - winret.left;
  290. cy = point.y - winret.top;
  291. HDC T_hMemDC = CreateCompatibleDC(hDC); //创建内存兼容设备环境
  292. //创建内存兼容位图
  293. HBITMAP T_hbmMem = CreateCompatibleBitmap(hDC, pInfo_Header->biWidth + cx + rc.left, pInfo_Header->biHeight + cy + rc.top);
  294. HBITMAP T_hbmOld = (HBITMAP)SelectObject(T_hMemDC, T_hbmMem);
  295. ::UpdateWindow(hWnd);
  296. ::RedrawWindow(hWnd, NULL, NULL, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN | RDW_FRAME);
  297. ::PrintWindow(hWnd, T_hMemDC, 0);
  298. BitBlt(hMemDC, 0, 0, pInfo_Header->biWidth, pInfo_Header->biHeight, T_hMemDC, cx + rc.left, xc + cy + rc.top, CAPTUREBLT | SRCCOPY);
  299. DeleteDC(T_hMemDC);
  300. DeleteObject(T_hbmMem);
  301. DeleteObject(T_hbmOld);
  302. }
  303. else
  304. {
  305. //将内存设备环境中的内容绘制到物理设备环境 hDC
  306. BitBlt(hMemDC, 0, 0, pInfo_Header->biWidth, pInfo_Header->biHeight, hDC, cx + rc.left, xc + cy + rc.top, CAPTUREBLT | SRCCOPY);
  307. }
  308. HBITMAP hBitmap = (HBITMAP)SelectObject(hMemDC, hbmOld);
  309. // 获得数据buf
  310. DWORD bufSize = (pInfo_Header->biWidth * 3 + 3) / 4 * 4 * pInfo_Header->biHeight;
  311. BYTE* pBuffer = new BYTE[bufSize];
  312. int aHeight = pInfo_Header->biHeight;
  313. if (::GetDIBits(hMemDC, hBitmap, 0, aHeight, pBuffer, pBITMAPINFO, DIB_RGB_COLORS) == 0)
  314. {
  315. if (TSRuntime::IsShowErrorMsg)
  316. ::MessageBox(NULL, L"加载位图失败", L"TS", 0);
  317. return false;
  318. }
  319. bool bret = SaveBitmapToFile(hBitmap, savepath);
  320. ReleaseDC(hWnd, hDC);
  321. //释放资源
  322. DeleteObject(hbmMem);
  323. DeleteObject(hbmOld);
  324. DeleteDC(hMemDC);
  325. free(pBITMAPINFO);
  326. ::DeleteObject(hBitmap);
  327. delete[] pBuffer;
  328. return bret;
  329. }
  330. bool SaveDXBitmapToFile(PVOID pBuffer, DWORD nbuffersize, LPCWSTR lpFileName)
  331. {
  332. //////////32位BMP转换24位BMP 2012-8-14
  333. BYTE* pTemp = (BYTE*)pBuffer; //已知 32位 数据
  334. BYTE* pData24, * tpData24;
  335. DWORD dwSize32 = nbuffersize; //已知,32位大小
  336. DWORD dwSize24;
  337. dwSize24 = (dwSize32 * 3) / 4;
  338. pData24 = new BYTE[dwSize24];
  339. tpData24 = pData24;
  340. for (int index = 0; index < dwSize32 / 4; index++) //像素总个数
  341. {
  342. unsigned char r = *(pTemp++);
  343. unsigned char g = *(pTemp++);
  344. unsigned char b = *(pTemp++);
  345. (pTemp++); //去掉alpha
  346. *(pData24++) = r;
  347. *(pData24++) = g;
  348. *(pData24++) = b;
  349. }
  350. return true;
  351. }
  352. bool MyFindPictureClass::CopyScreenToBitmap(LPRECT lpRect, wchar_t* savepath)
  353. {
  354. HDC hScrDC, hMemDC;
  355. // 屏幕和内存设备描述表
  356. HBITMAP hBitmap, hOldBitmap;
  357. // 位图句柄
  358. int nX, nY, nX2, nY2;
  359. // 选定区域坐标
  360. int nWidth, nHeight;
  361. // 位图宽度和高度
  362. int xScrn, yScrn;
  363. // 屏幕分辨率
  364. // 确保选定区域不为空矩形
  365. if (IsRectEmpty(lpRect))
  366. return NULL;
  367. //为屏幕创建设备描述表
  368. hScrDC = CreateDC(L"DISPLAY", NULL, NULL, NULL);
  369. //为屏幕设备描述表创建兼容的内存设备描述表
  370. hMemDC = CreateCompatibleDC(hScrDC);
  371. // 获得选定区域坐标
  372. nX = lpRect->left;
  373. nY = lpRect->top;
  374. nX2 = lpRect->right;
  375. nY2 = lpRect->bottom;
  376. //nX = 10;
  377. //nY = 10;
  378. //nX2 = 500;
  379. //nY2 = 600;
  380. // 获得屏幕分辨率
  381. xScrn = GetDeviceCaps(hScrDC, HORZRES);
  382. yScrn = GetDeviceCaps(hScrDC, VERTRES);
  383. //确保选定区域是可见的
  384. if (nX < 0)
  385. nX = 0;
  386. if (nY < 0)
  387. nY = 0;
  388. if (nX2 > xScrn)
  389. nX2 = xScrn;
  390. if (nY2 > yScrn)
  391. nY2 = yScrn;
  392. nWidth = nX2 - nX;
  393. nHeight = nY2 - nY;
  394. // 创建一个与屏幕设备描述表兼容的位图
  395. hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight);
  396. // 把新位图选到内存设备描述表中
  397. hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);
  398. // 把屏幕设备描述表拷贝到内存设备描述表中
  399. BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, nX, nY, CAPTUREBLT | SRCCOPY);
  400. //得到屏幕位图的句柄
  401. hBitmap = (HBITMAP)SelectObject(hMemDC, hOldBitmap);
  402. bool bret = SaveBitmapToFile(hBitmap, savepath);
  403. //清除
  404. DeleteDC(hScrDC);
  405. DeleteDC(hMemDC);
  406. DeleteObject(hOldBitmap);
  407. DeleteObject(hBitmap);
  408. // 返回位图句柄
  409. return bret;
  410. }
  411. //LPSTR lpFileName
  412. bool MyFindPictureClass::SaveBitmapToFile(HBITMAP hBitmap, LPCWSTR lpFileName)
  413. {
  414. HDC hDC;
  415. //设备描述表
  416. int iBits;
  417. //当前显示分辨率下每个像素所占字节数
  418. WORD wBitCount;
  419. //位图中每个像素所占字节数
  420. //定义调色板大小, 位图中像素字节大小 , 位图文件大小 , 写入文件字节数
  421. DWORD dwPaletteSize = 0, dwBmBitsSize, dwDIBSize, dwWritten;
  422. BITMAP Bitmap;
  423. //位图属性结构
  424. BITMAPFILEHEADER bmfHdr;
  425. //位图文件头结构
  426. BITMAPINFOHEADER bi;
  427. //位图信息头结构
  428. LPBITMAPINFOHEADER lpbi;
  429. //指向位图信息头结构
  430. HANDLE fh, hDib, hPal;
  431. HPALETTE hOldPal = NULL;
  432. //定义文件,分配内存句柄,调色板句柄
  433. //计算位图文件每个像素所占字节数
  434. hDC = CreateDC(L"DISPLAY", NULL, NULL, NULL);
  435. iBits = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES);
  436. DeleteDC(hDC);
  437. if (iBits <= 1)
  438. wBitCount = 1;
  439. else if (iBits <= 4)
  440. wBitCount = 4;
  441. else if (iBits <= 8)
  442. wBitCount = 8;
  443. else if (iBits <= 24)
  444. wBitCount = 24;
  445. else
  446. //wBitCount = 32;
  447. wBitCount = 24;
  448. //计算调色板大小
  449. if (wBitCount <= 8)
  450. dwPaletteSize = (1 << wBitCount) * sizeof(RGBQUAD);
  451. //设置位图信息头结构
  452. GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&Bitmap);
  453. bi.biSize = sizeof(BITMAPINFOHEADER);
  454. bi.biWidth = Bitmap.bmWidth;
  455. bi.biHeight = Bitmap.bmHeight;
  456. bi.biPlanes = 1;
  457. bi.biBitCount = wBitCount;
  458. bi.biCompression = BI_RGB;
  459. bi.biSizeImage = 0;
  460. bi.biXPelsPerMeter = 0;
  461. bi.biYPelsPerMeter = 0;
  462. bi.biClrUsed = 0;
  463. bi.biClrImportant = 0;
  464. dwBmBitsSize = ((Bitmap.bmWidth * wBitCount + 31) / 32) * 4 * Bitmap.bmHeight;
  465. //为位图内容分配内存
  466. /*xxxxxxxx计算位图大小分解一下(解释一下上面的语句)xxxxxxxxxxxxxxxxxxxx
  467. //每个扫描行所占的字节数应该为4的整数倍,具体算法为:
  468. int biWidth = (Bitmap.bmWidth*wBitCount) / 32;
  469. if((Bitmap.bmWidth*wBitCount) % 32)
  470. biWidth++; //不是整数倍的加1
  471. biWidth *= 4;//到这里,计算得到的为每个扫描行的字节数。
  472. dwBmBitsSize = biWidth * Bitmap.bmHeight;//得到大小
  473. xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
  474. //TSRuntime::add_log("wBitCount:%d",wBitCount);
  475. hDib = GlobalAlloc(GHND, dwBmBitsSize + dwPaletteSize + sizeof(BITMAPINFOHEADER));
  476. lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);
  477. *lpbi = bi;
  478. // 处理调色板
  479. hPal = GetStockObject(DEFAULT_PALETTE);
  480. if (hPal)
  481. {
  482. hDC = ::GetDC(NULL);
  483. hOldPal = SelectPalette(hDC, (HPALETTE)hPal, FALSE);
  484. RealizePalette(hDC);
  485. }
  486. // 获取该调色板下新的像素值
  487. GetDIBits(hDC, hBitmap, 0, (UINT)Bitmap.bmHeight, (LPSTR)lpbi + sizeof(BITMAPINFOHEADER) + dwPaletteSize, (BITMAPINFO*)lpbi, DIB_RGB_COLORS);
  488. //恢复调色板
  489. if (hOldPal)
  490. {
  491. SelectPalette(hDC, hOldPal, TRUE);
  492. RealizePalette(hDC);
  493. ::ReleaseDC(NULL, hDC);
  494. }
  495. //创建位图文件
  496. fh = CreateFile(lpFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
  497. if (fh == INVALID_HANDLE_VALUE)
  498. return FALSE;
  499. // 设置位图文件头
  500. bmfHdr.bfType = 0x4D42; // "BM"
  501. dwDIBSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwPaletteSize + dwBmBitsSize;
  502. bmfHdr.bfSize = dwDIBSize;
  503. bmfHdr.bfReserved1 = 0;
  504. bmfHdr.bfReserved2 = 0;
  505. bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER) + dwPaletteSize;
  506. bool bw1 = false;
  507. bool bw2 = false;
  508. // 写入位图文件头
  509. bw1 = WriteFile(fh, (LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER), &dwWritten, NULL);
  510. // 写入位图文件其余内容
  511. bw2 = WriteFile(fh, (LPSTR)lpbi, sizeof(BITMAPINFOHEADER) + dwPaletteSize + dwBmBitsSize, &dwWritten, NULL);
  512. //清除
  513. GlobalUnlock(hDib);
  514. GlobalFree(hDib);
  515. CloseHandle(fh);
  516. //return TRUE;
  517. if (bw1 && bw2 == true)
  518. {
  519. return true;
  520. }
  521. else
  522. {
  523. return false;
  524. }
  525. }
  526. bool MyFindPictureClass::getGDIBitmap(HWND hWnd, RECT rc, int typemode)
  527. {
  528. //TSRuntime::add_log( "getGDIBitmap:hWnd:%d",hWnd);
  529. //HDC hDC=::GetWindowDC(hWnd); //定义绘图用的设备环境
  530. m_hwnd = hWnd;
  531. HDC hDC;
  532. if (hWnd != GetDesktopWindow())
  533. //hDC=::GetWindowDC(hWnd); //定义绘图用的设备环境
  534. hDC = GetDCEx(hWnd, NULL, DCX_PARENTCLIP);
  535. else
  536. hDC = CreateDC(L"DISPLAY", NULL, NULL, NULL);
  537. HDC hMemDC; //内存缓冲设备环境
  538. HBITMAP hbmMem, hbmOld; //内存缓冲设备环境中的位图
  539. int xc = 0;
  540. int cx = 0;
  541. int cy = 0;
  542. wndWidth = m_Right - m_Left;
  543. wndHeight = m_bottom - m_Top;
  544. if (wndWidth >= 2000 || wndHeight >= 2000)//大于边界值就返回错误,以防坐标越界
  545. return false;
  546. BITMAPINFO pBITMAPINFO;
  547. pBITMAPINFO.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  548. pBITMAPINFO.bmiHeader.biWidth = wndWidth;
  549. pBITMAPINFO.bmiHeader.biHeight = wndHeight;
  550. pBITMAPINFO.bmiHeader.biPlanes = 1;
  551. pBITMAPINFO.bmiHeader.biBitCount = 24;
  552. pBITMAPINFO.bmiHeader.biCompression = BI_RGB;
  553. hMemDC = CreateCompatibleDC(hDC); //创建内存兼容设备环境
  554. //创建内存兼容位图
  555. hbmMem = CreateCompatibleBitmap(hDC, wndWidth, wndHeight);
  556. hbmOld = (HBITMAP)SelectObject(hMemDC, hbmMem);
  557. if ((m_displayMode & DISPLAY_GDI2) == DISPLAY_GDI2)
  558. {
  559. RECT winret;
  560. ::GetWindowRect(hWnd, &winret);
  561. POINT point;
  562. point.x = 0;
  563. point.y = 0;
  564. ::ClientToScreen(hWnd, &point);
  565. cx = point.x - winret.left;
  566. cy = point.y - winret.top;
  567. HDC T_hMemDC = CreateCompatibleDC(hDC); //创建内存兼容设备环境
  568. //创建内存兼容位图
  569. HBITMAP T_hbmMem = CreateCompatibleBitmap(hDC, wndWidth + cx + rc.left, wndHeight + cy + rc.top);
  570. HBITMAP T_hbmOld = (HBITMAP)SelectObject(T_hMemDC, T_hbmMem);
  571. ::UpdateWindow(hWnd);
  572. ::RedrawWindow(hWnd, NULL, NULL, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN | RDW_FRAME);
  573. ::PrintWindow(hWnd, T_hMemDC, 0);
  574. BitBlt(hMemDC, 0, 0, wndWidth, wndHeight, T_hMemDC, cx + rc.left, xc + cy + rc.top, CAPTUREBLT | SRCCOPY);
  575. DeleteDC(T_hMemDC);
  576. DeleteObject(T_hbmMem);
  577. DeleteObject(T_hbmOld);
  578. }
  579. else
  580. {
  581. //将内存设备环境中的内容绘制到物理设备环境 hDC
  582. BitBlt(hMemDC, 0, 0, wndWidth, wndHeight, hDC, cx + m_Left, xc + cy + m_Top, CAPTUREBLT | SRCCOPY);
  583. }
  584. HBITMAP hBitmap = (HBITMAP)SelectObject(hMemDC, hbmOld);
  585. // 获得数据buf
  586. //DWORD bufSize=(pInfo_Header->biWidth * 3 + 3) / 4 * 4 * pInfo_Header->biHeight;
  587. //BYTE * pBuffer = new BYTE[bufSize];
  588. int aHeight = wndHeight;
  589. if (::GetDIBits(hMemDC, hBitmap, 0, aHeight, pBuffer, &pBITMAPINFO, DIB_RGB_COLORS) == 0)
  590. {
  591. if (TSRuntime::IsShowErrorMsg)
  592. ::MessageBox(NULL, L"加载位图失败", L"TS", 0);
  593. return false;
  594. }
  595. //HDC dd = ::GetWindowDC(::GetDesktopWindow());
  596. COLORREF helpcolor = 0;
  597. int pitch = wndWidth % 4;
  598. if (typemode == 1)//找字
  599. {
  600. for (int i = 0; i < wndHeight; i++)
  601. {
  602. int realPitch = i * pitch;
  603. for (int j = 0; j < wndWidth; j++)
  604. {
  605. UCHAR b = pBuffer[(i * wndWidth + j) * 3 + realPitch];
  606. UCHAR g = pBuffer[(i * wndWidth + j) * 3 + 1 + realPitch];
  607. UCHAR r = pBuffer[(i * wndWidth + j) * 3 + 2 + realPitch];
  608. //helpcolor=RGB(r,g,b);
  609. bool dwfind = false;
  610. for (int n = 0; n < ncolors; n++)
  611. {
  612. BYTE colorR = GetRValue(strColors[n]);
  613. BYTE colorG = GetGValue(strColors[n]);
  614. BYTE colorB = GetBValue(strColors[n]);
  615. //处理色偏,把图二值化
  616. ////处理色偏
  617. if (abs(colorR - r) <= m_colorOffR[n] && abs(colorG - g) <= m_colorOffG[n] && abs(colorB - b) <= m_colorOffB[n])
  618. {
  619. helpcolor = RGB(255, 255, 255);
  620. dwfind = true;
  621. }
  622. else
  623. {
  624. helpcolor = RGB(0, 0, 0);
  625. }
  626. //SetPixel(dd,j,(wndHeight-i-1),helpcolor);
  627. pWndBmpBuffer[wndHeight - i - 1][j] = helpcolor;
  628. if (dwfind)
  629. break;
  630. }
  631. }
  632. }
  633. }
  634. else //找图
  635. {
  636. for (int i = 0; i < wndHeight; i++)
  637. {
  638. int realPitch = i * pitch;
  639. for (int j = 0; j < wndWidth; j++)
  640. {
  641. UCHAR b = pBuffer[(i * wndWidth + j) * 3 + realPitch];
  642. UCHAR g = pBuffer[(i * wndWidth + j) * 3 + 1 + realPitch];
  643. UCHAR r = pBuffer[(i * wndWidth + j) * 3 + 2 + realPitch];
  644. helpcolor = RGB(r, g, b);
  645. pWndBmpBuffer[wndHeight - i - 1 + m_Top][j + m_Left] = helpcolor;
  646. //::SetPixel(dd,j,i,helpcolor);
  647. }
  648. }
  649. }
  650. if (hWnd == GetDesktopWindow())
  651. DeleteDC(hDC);
  652. else
  653. ReleaseDC(hWnd, hDC);
  654. //释放资源
  655. DeleteObject(hbmMem);
  656. DeleteObject(hbmOld);
  657. DeleteDC(hMemDC);
  658. DeleteDC(hDC);
  659. //free(pBITMAPINFO);
  660. ::DeleteObject(hBitmap);
  661. //delete [] pBuffer;
  662. return true;
  663. }
  664. bool MyFindPictureClass::getDXBitmap(PVOID pBmp, DWORD(*ColorDataInfo)[2000], int typemode)
  665. {
  666. if (wndWidth >= 2000 || wndHeight >= 2000)//大于边界值就返回错误,以防坐标越界
  667. return false;
  668. //HDC dd = ::GetWindowDC(::GetDesktopWindow());
  669. //TSRuntime::add_log( "wndHeight:%d wndWidth:%d,m_left:%d,m_top:%d,m_Right:%d,m_bottom:%d",wndHeight,wndWidth,m_Left,m_Top,m_Right,m_bottom);
  670. COLORREF helpcolor = 0;
  671. //int pitch=wndWidth%4;
  672. if (typemode == 1)//找字
  673. {
  674. for (int i = 0; i < wndHeight; i++)
  675. {
  676. //int realPitch=i*pitch;
  677. for (int j = 0; j < wndWidth; j++)
  678. {
  679. UCHAR r = GetRValue(ColorDataInfo[i][j]);
  680. UCHAR g = GetGValue(ColorDataInfo[i][j]);
  681. UCHAR b = GetBValue(ColorDataInfo[i][j]);
  682. bool dwfind = false;
  683. for (int n = 0; n < ncolors; n++)
  684. {
  685. BYTE colorR = GetRValue(strColors[n]);
  686. BYTE colorG = GetGValue(strColors[n]);
  687. BYTE colorB = GetBValue(strColors[n]);
  688. //处理色偏,把图二值化
  689. if (abs(colorR - r) <= m_colorOffR[n] && abs(colorG - g) <= m_colorOffG[n] && abs(colorB - b) <= m_colorOffB[n])
  690. {
  691. helpcolor = RGB(255, 255, 255);
  692. dwfind = true;
  693. }
  694. else
  695. {
  696. helpcolor = RGB(0, 0, 0);
  697. }
  698. //COLORREF helpcolor1=RGB(r,g,b);
  699. //SetPixel(dd,j,i,helpcolor1);
  700. pWndBmpBuffer[i][j] = helpcolor;
  701. //pWndBmpBuffer[wndHeight-i-1+m_Top][j+m_Left]=helpcolor;
  702. if (dwfind)
  703. break;
  704. }
  705. }
  706. }
  707. }
  708. else //找图
  709. {
  710. for (int i = 0; i < wndHeight; i++)
  711. {
  712. for (int j = 0; j < wndWidth; j++)
  713. {
  714. //helpcolor=(COLORREF)ColorDataInfo[i][j];
  715. //SetPixel(dd,j,i,helpcolor);
  716. pWndBmpBuffer[m_Top + i][j + m_Left] = (COLORREF)ColorDataInfo[i][j];
  717. }
  718. }
  719. }
  720. return true;
  721. ///////////////////////////获取窗口位图的 颜色成功 //////////////////////////
  722. }
  723. //
  724. //
  725. //
  726. int MyFindPictureClass::findPicture(PVOID pbuffer, HWND hwnd, int left, int top, int right, int bottom,
  727. wchar_t* path, wchar_t* color, double simi, int dir,
  728. long& xpos, long& ypos, DWORD(*ColorDataInfo)[2000], wchar_t* retstring, int type)
  729. {
  730. int index = -1;
  731. int retIndex = -1;
  732. bool isFind = false;
  733. if (hwnd != 0)
  734. {
  735. if (::IsIconic(hwnd))
  736. {
  737. //::MessageBox(0,L"超过边界值,GDI模式不支持最小化找图",L"TS",0);
  738. return -1;
  739. }
  740. }
  741. ////////处理颜色和色偏
  742. WCHAR colorR[4] = { 0 };//字的颜色
  743. WCHAR colorG[4] = { 0 };//字的颜色
  744. WCHAR colorB[4] = { 0 };//字的颜色
  745. int count = 0;
  746. int colorL = 0;
  747. while (color[colorL] != L'\0')
  748. {
  749. if (count > 6) //判断是否是非法颜色值
  750. break;
  751. if (count < 2)
  752. colorR[count] = color[colorL++];
  753. else if (count > 1 && count < 4)
  754. colorG[count - 2] = color[colorL++];
  755. else if (count > 3)
  756. colorB[count - 4] = color[colorL++];
  757. count++;
  758. }
  759. //将传入的RGB转换为BGR
  760. WCHAR colorBGR[16] = { 0 };//字的颜色
  761. swprintf(colorBGR, L"%s%s%s", colorB, colorG, colorR);
  762. strColor = wcstol(colorBGR, NULL, 16);//整形颜色值
  763. m_colorOffsR = GetRValue(strColor);
  764. m_colorOffsG = GetGValue(strColor);
  765. m_colorOffsB = GetBValue(strColor);
  766. ////////处理颜色和色偏
  767. /*m_sim = simi;*/ //未使用该变量
  768. m_simColor = 0;
  769. if (simi == 0.5)
  770. m_simColor = 49;
  771. else if (simi == 0.6)
  772. m_simColor = 38;
  773. else if (simi == 0.7)
  774. m_simColor = 29;
  775. else if (simi == 0.8)
  776. m_simColor = 18;
  777. else if (simi == 0.9)
  778. m_simColor = 9;
  779. /////////////////////////////////////
  780. m_Dir = dir;
  781. /////////////////////////////控制四点的边界值以防止越界//////////////////////////
  782. wchar_t pathBuffer[MAX_PATH] = { 0 };
  783. wchar_t RCPath[MAX_PATH] = { 0 };
  784. if (m_parenthwnd != 0 && m_parenthwnd != ::GetDesktopWindow())//normol找图
  785. {
  786. RECT rec;
  787. RECT clientrec;
  788. ::GetClientRect(m_parenthwnd, &clientrec);
  789. int clienthight = clientrec.bottom - clientrec.top;
  790. int clientwide = clientrec.right - clientrec.left;
  791. ::GetWindowRect(m_parenthwnd, &rec);
  792. POINT point;
  793. point.x = 0;
  794. point.y = 0;
  795. ::ClientToScreen(m_parenthwnd, &point);
  796. m_Left = point.x;
  797. m_Top = point.y;
  798. m_Right = rec.right;
  799. m_bottom = rec.bottom;
  800. if (left < 0)
  801. left = 0;
  802. if (left >= clientwide)
  803. left = clientwide - 1;
  804. if (top < 0)
  805. top = 0;
  806. if (top >= clienthight)
  807. top = clienthight - 1;
  808. if (right >= clientwide)
  809. right = clientwide - 1;
  810. if (bottom >= clienthight)
  811. bottom = clienthight - 1;
  812. if (m_Left < 0)
  813. {
  814. if ((left + m_Left) <= 0)//越界
  815. left = 0;
  816. else
  817. left = m_Left + left;
  818. }
  819. else
  820. left = m_Left + left;
  821. if (m_Top >= 0)
  822. top = m_Top + top;
  823. if (m_Right >= ::GetSystemMetrics(SM_CXSCREEN))
  824. {
  825. if ((right + m_Left) > ::GetSystemMetrics(SM_CXSCREEN))
  826. right = ::GetSystemMetrics(SM_CXSCREEN) - 1;
  827. else
  828. right = right + m_Left;
  829. }
  830. else
  831. right = right + m_Left;
  832. if (m_bottom >= ::GetSystemMetrics(SM_CYSCREEN))
  833. {
  834. if ((bottom + m_Top) >= ::GetSystemMetrics(SM_CYSCREEN))
  835. bottom = ::GetSystemMetrics(SM_CYSCREEN) - 1;
  836. else
  837. bottom = bottom + m_Top;
  838. }
  839. else
  840. bottom = bottom + m_Top;
  841. m_Left = left;
  842. m_Top = top;
  843. m_Right = right;
  844. m_bottom = bottom;
  845. }
  846. else
  847. {
  848. RECT clientrec;
  849. if (ColorDataInfo != NULL)//DX
  850. ::GetClientRect(m_hwnd, &clientrec);
  851. else//gdi
  852. ::GetClientRect(hwnd, &clientrec);
  853. int clienthight = clientrec.bottom - clientrec.top;
  854. int clientwide = clientrec.right - clientrec.left;
  855. m_Left = left;
  856. m_Top = top;
  857. m_Right = right;
  858. m_bottom = bottom;
  859. //TSRuntime::add_log( "clientwide:%d,clienthight:%d,m_Left:%d,m_Top:%d,m_Right:%d,m_bottom:%d",clientwide,clienthight,left,top,right,bottom);
  860. if (left < 0)
  861. m_Left = left = 0;
  862. if (left >= clientwide)
  863. m_Left = clientwide - 1;
  864. if (top < 0)
  865. m_Top = top = 0;
  866. if (top >= clienthight)
  867. m_Top = clienthight - 1;
  868. if (right >= clientwide)
  869. m_Right = clientwide - 1;
  870. if (bottom >= clienthight)
  871. m_bottom = clienthight - 1;
  872. //TSRuntime::add_log( "m_Left:%d,m_Top:%d,m_Right:%d,m_bottom:%d",m_Left,m_Top,m_Right,m_bottom);
  873. }
  874. RECT rc;
  875. rc.bottom = m_bottom;
  876. rc.left = m_Left;
  877. rc.right = m_Right;
  878. rc.top = m_Top;
  879. if (ColorDataInfo != NULL)
  880. {
  881. wndWidth = m_Right - m_Left;
  882. wndHeight = m_bottom - m_Top;
  883. //DX模式找字
  884. if (!getDXBitmap(pbuffer, ColorDataInfo))
  885. {
  886. return retIndex;
  887. }
  888. }
  889. else
  890. {
  891. ////GDI模式找字
  892. if (!getGDIBitmap(hwnd, rc))
  893. {
  894. return retIndex;
  895. }
  896. }
  897. /////////////////// 处理多张图 /////////////////////
  898. int col = 0;
  899. while (*path)
  900. {
  901. IsFindPicNext = false;
  902. if (*path != L'|')
  903. {
  904. pathBuffer[col++] = *path;
  905. path++;
  906. continue;
  907. }
  908. else
  909. {
  910. //StrTrimW(pathBuffer,L" ");//移除前后空格符
  911. wchar_t* findpath = wcsstr(pathBuffer, L":");
  912. if (wcslen(SetPath) > 0 && findpath == NULL)
  913. {
  914. wchar_t pathstr[MAX_PATH] = { 0 };
  915. wcscpy(pathstr, pathBuffer);
  916. swprintf(RCPath, L"%s\\%s", SetPath, pathstr); //如果设置SetPath全路径的时候,在这里拼接图片全路径
  917. }
  918. else
  919. wcscpy(RCPath, pathBuffer);
  920. xpos_offset = 0;
  921. ypos_offset = 0;
  922. while (true)
  923. {
  924. if (findImage(RCPath, xpos, ypos))
  925. {
  926. xpos = xpos;
  927. ypos = ypos;
  928. if (m_parenthwnd != 0 && m_parenthwnd != ::GetDesktopWindow())
  929. {
  930. POINT pt;
  931. pt.x = xpos;
  932. pt.y = ypos;
  933. ::ScreenToClient(m_parenthwnd, &pt);
  934. xpos = pt.x;
  935. ypos = pt.y;
  936. }
  937. if ((ypos_offset != 0 || xpos_offset != 0) && IsFindPicNext)
  938. {
  939. if ((xpos_offset + m_Left - 1) == xpos && (ypos_offset + m_Top) == ypos)
  940. {
  941. xpos = -1;
  942. ypos = -1;
  943. retIndex = -1;
  944. IsFindPicNext = false;
  945. break;
  946. }
  947. // TSRuntime::add_log( "xpos_offset:%d,ypos_offset:%d,xpos:%d,ypos:%d",xpos_offset,ypos_offset,xpos,ypos);
  948. }
  949. if (!IsFindPicNext)
  950. index++;
  951. retIndex = index;
  952. if (!retstring)
  953. break;
  954. if (retstring && type == 2)
  955. {
  956. swprintf(retstring, L"%s,%d,%d", pathBuffer, xpos, ypos);
  957. break;
  958. }
  959. if (retstring && type != 2)
  960. {
  961. int len = wcslen(retstring);
  962. if (len > (MAX_PATH * 100 - 16))//防止找到太多,字符串越界,最多返回是260*4个字符
  963. break;
  964. else if (len > 1 && len < (MAX_PATH * 100 - 16) && type == 1) //FindPicExS
  965. swprintf(retstring, L"%s|%s,%d,%d", retstring, pathBuffer, xpos, ypos);
  966. else if (len == 0 && type == 1)//FindPicExS
  967. swprintf(retstring, L"%s,%d,%d", pathBuffer, xpos, ypos);
  968. else if (len > 1 && len < (MAX_PATH * 100 - 16))//FindPicEx
  969. swprintf(retstring, L"%s|%d,%d,%d", retstring, index, xpos, ypos);
  970. else if (len == 0)//FindPicEx
  971. swprintf(retstring, L"%d,%d,%d", index, xpos, ypos);
  972. }
  973. else
  974. break;
  975. }
  976. else
  977. {
  978. if (!IsFindPicNext)
  979. index++;
  980. xpos = -1;
  981. ypos = -1;
  982. retIndex = -1;
  983. IsFindPicNext = false;
  984. break;
  985. }
  986. IsFindPicNext = true;
  987. if (m_parenthwnd != 0)//normal
  988. {
  989. POINT pt;
  990. pt.x = xpos;
  991. pt.y = ypos;
  992. ::ClientToScreen(m_parenthwnd, &pt);
  993. xpos = pt.x;
  994. ypos = pt.y;
  995. }
  996. if (m_Dir == 0)//leftToRightFromTop
  997. {
  998. xpos_offset = xpos - m_Left + 1;
  999. ypos_offset = ypos - m_Top;
  1000. }
  1001. else if (m_Dir == 1)//leftToRightFromBottom
  1002. {
  1003. xpos_offset = xpos - m_Left + 1;
  1004. ypos_offset = m_bottom - ypos;
  1005. }
  1006. else if (m_Dir == 2)//rightToLeftFromTop
  1007. {
  1008. xpos_offset = m_Right - xpos - 1;
  1009. ypos_offset = ypos - m_Top;
  1010. }
  1011. else if (m_Dir == 3)//rightToLeftFromBottom
  1012. {
  1013. xpos_offset = m_Right - xpos + 1;
  1014. ypos_offset = m_bottom - ypos;
  1015. }
  1016. }
  1017. if ((!retstring || type == 2) && retIndex != -1)
  1018. break;
  1019. ::memset(pathBuffer, 0, sizeof(wchar_t) * MAX_PATH);
  1020. col = 0;
  1021. path++;
  1022. //index++;
  1023. }
  1024. }
  1025. //////////////最后一张图或者是只有一张图///////////
  1026. if (retIndex == -1 || (retstring != NULL && type != 2))
  1027. {
  1028. //StrTrimW(pathBuffer,L" ");//移除前后空格符
  1029. wchar_t* findpath = wcsstr(pathBuffer, L":");
  1030. if (wcslen(SetPath) > 0 && findpath == NULL)
  1031. {
  1032. if (wcsstr(pathBuffer, L":") == NULL)
  1033. {
  1034. wchar_t pathstr[MAX_PATH] = { 0 };
  1035. wcscpy(pathstr, pathBuffer);
  1036. swprintf(RCPath, L"%s\\%s", SetPath, pathstr); //如果设置SetPath全路径的时候,在这里拼接图片全路径
  1037. }
  1038. }
  1039. else
  1040. wcscpy(RCPath, pathBuffer);
  1041. IsFindPicNext = false;
  1042. xpos_offset = 0;
  1043. ypos_offset = 0;
  1044. while (true)
  1045. {
  1046. if (findImage(RCPath, xpos, ypos))
  1047. {
  1048. xpos = xpos;
  1049. ypos = ypos;
  1050. //TSRuntime::add_log( "xpos_offset:%d,ypos_offset:%d,xpos:%d,ypos:%d",xpos_offset,ypos_offset,xpos,ypos);
  1051. if (m_parenthwnd != 0 && m_parenthwnd != ::GetDesktopWindow())
  1052. {
  1053. xpos_offset = xpos - m_Left + 1;
  1054. ypos_offset = ypos - m_Top;
  1055. POINT pt;
  1056. pt.x = xpos;
  1057. pt.y = ypos;
  1058. ::ScreenToClient(m_parenthwnd, &pt);
  1059. xpos = pt.x;
  1060. ypos = pt.y;
  1061. }
  1062. if ((ypos_offset != 0 || xpos_offset != 0) && IsFindPicNext)
  1063. {
  1064. if ((xpos_offset + m_Left - 1) == xpos && (ypos_offset + m_Top) == ypos)
  1065. {
  1066. xpos = -1;
  1067. ypos = -1;
  1068. retIndex = -1;
  1069. IsFindPicNext = false;
  1070. break;
  1071. }
  1072. }
  1073. if (!IsFindPicNext)
  1074. index++;
  1075. retIndex = index;
  1076. if (!retstring)
  1077. break;
  1078. if (retstring && type == 2)
  1079. {
  1080. swprintf(retstring, L"%s,%d,%d", pathBuffer, xpos, ypos);
  1081. break;
  1082. }
  1083. if (retstring && type != 2)
  1084. {
  1085. int len = wcslen(retstring);
  1086. if (len > (MAX_PATH * 100 - 16))//防止找到太多,字符串越界,最多返回是260*4个字符
  1087. break;
  1088. else if (len > 1 && len < (MAX_PATH * 100 - 16) && type == 1) //FindPicExS
  1089. swprintf(retstring, L"%s|%s,%d,%d", retstring, pathBuffer, xpos, ypos);
  1090. else if (len == 0 && type == 1)//FindPicExS
  1091. swprintf(retstring, L"%s,%d,%d", pathBuffer, xpos, ypos);
  1092. else if (len > 1 && len < (MAX_PATH * 100 - 16))//FindPicEx
  1093. swprintf(retstring, L"%s|%d,%d,%d", retstring, index, xpos, ypos);
  1094. else if (len == 0)//FindPicEx
  1095. swprintf(retstring, L"%d,%d,%d", index, xpos, ypos);
  1096. }
  1097. }
  1098. else
  1099. {
  1100. xpos = -1;
  1101. ypos = -1;
  1102. retIndex = -1;
  1103. IsFindPicNext = false;
  1104. break;
  1105. }
  1106. IsFindPicNext = true;
  1107. if (m_parenthwnd != 0)//normal
  1108. {
  1109. POINT pt;
  1110. pt.x = xpos;
  1111. pt.y = ypos;
  1112. ::ClientToScreen(m_parenthwnd, &pt);
  1113. xpos = pt.x;
  1114. ypos = pt.y;
  1115. }
  1116. if (m_Dir == 0)//leftToRightFromTop
  1117. {
  1118. xpos_offset = xpos - m_Left + 1;
  1119. ypos_offset = ypos - m_Top;
  1120. }
  1121. else if (m_Dir == 1)//leftToRightFromBottom
  1122. {
  1123. xpos_offset = xpos - m_Left + 1;
  1124. ypos_offset = m_bottom - ypos;
  1125. }
  1126. else if (m_Dir == 2)//rightToLeftFromTop
  1127. {
  1128. xpos_offset = m_Right - xpos - 1;
  1129. ypos_offset = ypos - m_Top;
  1130. }
  1131. else if (m_Dir == 3)//rightToLeftFromBottom
  1132. {
  1133. xpos_offset = m_Right - xpos + 1;
  1134. ypos_offset = m_bottom - ypos;
  1135. }
  1136. if (!retstring || type == 2)
  1137. break;
  1138. }
  1139. //TSRuntime::add_log( "end,path:%s",W2A(path));
  1140. //::SetCursorPos(xpos,ypos);
  1141. }
  1142. endfind:
  1143. return retIndex;
  1144. }
  1145. bool MyFindPictureClass::findImage(wchar_t* path, long& xpos, long& ypos, int type)
  1146. {
  1147. bool isFind = false;
  1148. if (loadBitmap(path) == 0)
  1149. return false;
  1150. switch (m_Dir)
  1151. {
  1152. case 0:
  1153. {
  1154. isFind = leftToRightFromTop(xpos, ypos); //0:从左到右,从上倒下
  1155. break;
  1156. }
  1157. case 1:
  1158. isFind = leftToRightFromBottom(xpos, ypos);//1:从右到左,从上倒下
  1159. break;
  1160. case 2:
  1161. isFind = rightToLeftFromTop(xpos, ypos);//2:从左到右,从下倒上
  1162. break;
  1163. case 3:
  1164. isFind = rightToLeftFromBottom(xpos, ypos);//3:从右到左,从下倒上
  1165. break;
  1166. }
  1167. //TSRuntime::add_log( "findImage-xpos:%d,ypos:%d,xpos_offset:%d,ypos_offset:%d,m_Dir:%d",xpos,ypos,xpos_offset,ypos_offset,m_Dir);
  1168. for (int i = 0; i < loadHeight; i++)
  1169. {
  1170. delete[] pLoadBmpBuffer[i];
  1171. pLoadBmpBuffer[i] = NULL;
  1172. }
  1173. delete[] pLoadBmpBuffer;
  1174. pLoadBmpBuffer = NULL;
  1175. //TSRuntime::add_log( "findImage-end");
  1176. return isFind;
  1177. }
  1178. ////////////////// 检索 方式 //////////////////
  1179. ////////////////////////////////////////////////0:从左到右,从上倒下//////////////////////////
  1180. bool MyFindPictureClass::leftToRightFromTop(long& xpos, long& ypos)
  1181. {
  1182. //TSRuntime::add_log( "leftToRightFromTop,loadHeight:%d,loadWidth:%d",loadHeight,loadWidth);
  1183. bool isFind = false;
  1184. bool isfirst = true;
  1185. COLORREF pColor = pLoadBmpBuffer[0][0]; //// 目标图片的背景色
  1186. if (pLoadBmpBuffer[0][0] == pLoadBmpBuffer[0][loadWidth - 1] &&
  1187. pLoadBmpBuffer[0][0] == pLoadBmpBuffer[loadHeight - 1][loadWidth - 1] &&
  1188. pLoadBmpBuffer[0][0] == pLoadBmpBuffer[loadHeight - 1][0])
  1189. {
  1190. int startx = 0, starty = 0;
  1191. for (int m = 0; m < loadHeight; m++)
  1192. {
  1193. for (int n = 0; n < loadWidth; n++)
  1194. {
  1195. if (pLoadBmpBuffer[m][n] == pColor)
  1196. {
  1197. //////过滤掉不处理的背景透明色
  1198. continue;
  1199. }
  1200. else
  1201. {
  1202. //2012.2.28 修改相似的图无法识别的问题.
  1203. startx = n;
  1204. starty = m;
  1205. goto toSerch;
  1206. }
  1207. }
  1208. if ((m + 1) == loadWidth)//如果全图是一个颜色,说明不是透明图
  1209. goto Serch;
  1210. }
  1211. toSerch:
  1212. for (int i = m_Top + ypos_offset; i <= m_bottom - 1; i++)
  1213. {
  1214. if (isfirst)
  1215. isfirst = false;
  1216. else if (xpos_offset != 0)
  1217. xpos_offset = 0;
  1218. if (m_bottom - i < loadHeight)
  1219. {
  1220. goto end;
  1221. }
  1222. for (int j = m_Left + xpos_offset; j <= m_Right - 1; j++)
  1223. {
  1224. if (m_Right - j < loadWidth)
  1225. {
  1226. break;
  1227. }
  1228. if (comparaLeftToRightFromTop(i, j, startx, starty))
  1229. {
  1230. isFind = true;
  1231. xpos = j;
  1232. ypos = i;
  1233. goto end;
  1234. }
  1235. }
  1236. }
  1237. }
  1238. else
  1239. {
  1240. Serch:
  1241. //TSRuntime::add_log( "comparaLeftToRightFromTopEx-start");
  1242. for (int i = m_Top + ypos_offset; i <= m_bottom - 1; i++)
  1243. {
  1244. if (isfirst)
  1245. isfirst = false;
  1246. else if (xpos_offset != 0)
  1247. xpos_offset = 0;
  1248. if (m_bottom - i < loadHeight)
  1249. {
  1250. goto end;
  1251. }
  1252. for (int j = m_Left + xpos_offset; j <= m_Right - 1; j++)
  1253. {
  1254. if (m_Right - j < loadWidth)
  1255. {
  1256. break;
  1257. }
  1258. if (comparaLeftToRightFromTopEx(i, j))
  1259. {
  1260. isFind = true;
  1261. xpos = j;
  1262. ypos = i;
  1263. goto end;
  1264. }
  1265. }
  1266. }
  1267. }
  1268. end:
  1269. return isFind;
  1270. }
  1271. ////////处理背景色为透明的图片////////////////
  1272. bool MyFindPictureClass::comparabmp(int startX, int startY, int endX, int endY)
  1273. {
  1274. int fristX = 0;
  1275. int fristY = 0;
  1276. bool isFrist = true;
  1277. COLORREF pColor = pLoadBmpBuffer[0][0]; //// 目标图片的背景色
  1278. BYTE loadR = 0, loadG = 0, loadB = 0;
  1279. BYTE wndR = 0, wndG = 0, wndB = 0;
  1280. int absResult = 0;
  1281. COLORREF pLoadColor = 0, pWndColor = 0;
  1282. for (int i = startY; i < endY; i++)
  1283. {
  1284. for (int j = startX; j < endX; j++)
  1285. {
  1286. if (pLoadBmpBuffer[i][j] == pColor)
  1287. {
  1288. //////过滤掉不处理的背景透明色
  1289. continue;
  1290. }
  1291. else
  1292. {
  1293. pLoadColor = pLoadBmpBuffer[i][j];
  1294. loadR = GetRValue(pLoadColor);
  1295. loadG = GetGValue(pLoadColor);
  1296. loadB = GetBValue(pLoadColor);
  1297. pWndColor = pWndBmpBuffer[i][j];
  1298. wndR = GetRValue(pWndColor);
  1299. wndG = GetGValue(pWndColor);
  1300. wndB = GetBValue(pWndColor);
  1301. ///////下面的是或关系。
  1302. if (abs(loadR - wndR) == 0 && abs(loadG - wndG) == 0 && abs(loadB - wndB) == 0)
  1303. {
  1304. continue;
  1305. }
  1306. else
  1307. {
  1308. return false;
  1309. }
  1310. }
  1311. }
  1312. }
  1313. return true;
  1314. }
  1315. ////////处理背景色为透明的图片////////////////
  1316. bool MyFindPictureClass::comparaLeftToRightFromTop(int h, int w, int startX, int startY)
  1317. {
  1318. //TSRuntime::add_log( "comparaLeftToRightFromTop-h:%d,w:%d,startX:%d,startY:%d",h,w,startX,startY);
  1319. int fristX = 0;
  1320. int fristY = 0;
  1321. bool isFrist = true;
  1322. COLORREF pColor = pLoadBmpBuffer[0][0]; //// 目标图片的背景色
  1323. BYTE loadR = 0, loadG = 0, loadB = 0;
  1324. BYTE wndR = 0, wndG = 0, wndB = 0;
  1325. int absResult = 0;
  1326. COLORREF pLoadColor = 0, pWndColor = 0;
  1327. for (int i = startY; i < loadHeight; i++)
  1328. {
  1329. for (int j = 0; j < loadWidth; j++)
  1330. {
  1331. if (pLoadBmpBuffer[i][j] == pColor)
  1332. {
  1333. //////过滤掉不处理的背景透明色
  1334. continue;
  1335. }
  1336. else
  1337. {
  1338. pLoadColor = pLoadBmpBuffer[i][j];
  1339. loadR = GetRValue(pLoadColor);
  1340. loadG = GetGValue(pLoadColor);
  1341. loadB = GetBValue(pLoadColor);
  1342. pWndColor = pWndBmpBuffer[h + i/*-fristY*/][w + j/*-fristX*/];
  1343. wndR = GetRValue(pWndColor);
  1344. wndG = GetGValue(pWndColor);
  1345. wndB = GetBValue(pWndColor);
  1346. absResult = abs(loadR - wndR) + abs(loadG - wndG) + abs(loadB - wndB); ///用于相似度比较
  1347. ///////下面的是或关系。
  1348. //if(abs(loadR-wndR)<=m_colorOffsR && abs(loadG-wndG)<=m_colorOffsG && abs(loadB-wndB)<=m_colorOffsB ||absResult<=m_simColor)
  1349. if (abs(loadR - wndR) <= (m_colorOffsR + m_simColor) && abs(loadG - wndG) <= (m_colorOffsG + m_simColor) && abs(loadB - wndB) <= (m_colorOffsB + m_simColor))
  1350. {
  1351. continue;
  1352. }
  1353. else
  1354. {
  1355. return false;
  1356. }
  1357. }
  1358. }
  1359. }
  1360. return true;
  1361. }
  1362. ////////处理背景色为不透明的图片////////////////
  1363. bool MyFindPictureClass::comparaLeftToRightFromTopEx(int h, int w)
  1364. {
  1365. BYTE loadR = 0, loadG = 0, loadB = 0;
  1366. BYTE wndR = 0, wndG = 0, wndB = 0;
  1367. int absResult = 0;
  1368. COLORREF pLoadColor = 0, pWndColor = 0;
  1369. for (int i = 0; i < loadHeight; i++)
  1370. {
  1371. for (int j = 0; j < loadWidth; j++)
  1372. {
  1373. pLoadColor = pLoadBmpBuffer[i][j];
  1374. loadR = GetRValue(pLoadColor);
  1375. loadG = GetGValue(pLoadColor);
  1376. loadB = GetBValue(pLoadColor);
  1377. pWndColor = pWndBmpBuffer[h + i][w + j];
  1378. wndR = GetRValue(pWndColor);
  1379. wndG = GetGValue(pWndColor);
  1380. wndB = GetBValue(pWndColor);
  1381. //absResult=(loadR-wndR)+abs(loadG-wndG)+abs(loadB-wndB); ///用于相似度比较
  1382. //if(pLoadBmpBuffer[i][j] == pWndBmpBuffer[h+i][w+j])
  1383. if (abs(loadR - wndR) <= (m_colorOffsR + m_simColor) && abs(loadG - wndG) <= (m_colorOffsG + m_simColor) && abs(loadB - wndB) <= (m_colorOffsB + m_simColor))
  1384. {
  1385. continue;
  1386. }
  1387. else
  1388. {
  1389. return false;
  1390. }
  1391. }
  1392. }
  1393. return true;
  1394. }
  1395. ///////////////////////////////////////////////1:从左到右,从下倒上//////////////////////////
  1396. //bool MyFindPictureClass::leftToRightFromBottom(int &xpos,int &ypos)
  1397. bool MyFindPictureClass::leftToRightFromBottom(long& xpos, long& ypos)
  1398. {
  1399. bool isFind = false;
  1400. bool isfirst = true;
  1401. COLORREF pColor = pLoadBmpBuffer[0][0]; //// 目标图片的背景色
  1402. if (pLoadBmpBuffer[0][0] == pLoadBmpBuffer[0][loadWidth - 1] &&
  1403. pLoadBmpBuffer[0][0] == pLoadBmpBuffer[loadHeight - 1][loadWidth - 1] &&
  1404. pLoadBmpBuffer[0][0] == pLoadBmpBuffer[loadHeight - 1][0])
  1405. {
  1406. int startx = 0, starty = 0;
  1407. for (int m = loadHeight - 1; m >= 0; m--)
  1408. {
  1409. for (int n = 0; n < loadWidth; n++)
  1410. {
  1411. if (pLoadBmpBuffer[m][n] == pColor)
  1412. {
  1413. //////过滤掉不处理的背景透明色
  1414. continue;
  1415. }
  1416. else
  1417. {
  1418. //2012.2.28 修改相似的图无法识别的问题.
  1419. startx = n;
  1420. starty = m;
  1421. goto toSerch;
  1422. }
  1423. }
  1424. if ((m + 1) == loadWidth)//如果全图是一个颜色,说明不是透明图
  1425. goto Serch;
  1426. }
  1427. toSerch:
  1428. for (int i = m_bottom - ypos_offset; i >= m_Top - 1; i--)
  1429. {
  1430. if (isfirst)
  1431. isfirst = false;
  1432. else if (xpos_offset != 0)
  1433. xpos_offset = 0;
  1434. if (i - m_Top < loadHeight) goto end;
  1435. for (int j = m_Left + xpos_offset; j < m_Right - 1; j++)
  1436. {
  1437. if (m_Right - j < loadWidth) break;
  1438. if (comparaLeftToRightFromBottom(i, j, startx, starty))
  1439. {
  1440. isFind = true;
  1441. xpos = j;
  1442. ypos = i - starty;
  1443. goto end;
  1444. }
  1445. }
  1446. }
  1447. }
  1448. else
  1449. {
  1450. Serch:
  1451. for (int i = m_bottom - ypos_offset; i >= m_Top - 1; i--)
  1452. {
  1453. if (isfirst)
  1454. isfirst = false;
  1455. else if (xpos_offset != 0)
  1456. xpos_offset = 0;
  1457. if (i - m_Top < loadHeight) goto end;
  1458. for (int j = m_Left + xpos_offset; j < m_Right - 1; j++)
  1459. {
  1460. if (m_Right - j < loadWidth) break;
  1461. if (comparaLeftToRightFromBottomEx(i, j))
  1462. {
  1463. isFind = true;
  1464. xpos = j;
  1465. ypos = i;
  1466. goto end;
  1467. }
  1468. }
  1469. }
  1470. }
  1471. end:
  1472. return isFind;
  1473. }
  1474. ////////处理背景色为透明的图片////////////////
  1475. //bool MyFindPictureClass::comparaLeftToRightFromBottom(int h,int w,int startX,int startY)
  1476. bool MyFindPictureClass::comparaLeftToRightFromBottom(int h, int w, int startX, int startY)
  1477. {
  1478. int fristX = 0;
  1479. int fristY = 0;
  1480. bool isFrist = true;
  1481. COLORREF pColor = pLoadBmpBuffer[0][0]; //// 目标图片的背景色
  1482. BYTE loadR = 0, loadG = 0, loadB = 0;
  1483. BYTE wndR = 0, wndG = 0, wndB = 0;
  1484. int absResult = 0;
  1485. //int colAdd =-1;
  1486. COLORREF pLoadColor = 0, pWndColor = 0;
  1487. for (int i = startY; i >= 0; i--)
  1488. {
  1489. //colAdd++;
  1490. for (int j = startY; j < loadWidth; j++)
  1491. {
  1492. if (pLoadBmpBuffer[i][j] == pColor)
  1493. {
  1494. //////过滤掉不处理的背景透明色
  1495. continue;
  1496. }
  1497. else
  1498. {
  1499. pLoadColor = pLoadBmpBuffer[i][j];
  1500. loadR = GetRValue(pLoadColor);
  1501. loadG = GetGValue(pLoadColor);
  1502. loadB = GetBValue(pLoadColor);
  1503. pWndColor = pWndBmpBuffer[h - (loadHeight - i - 1)][w + j];
  1504. wndR = GetRValue(pWndColor);
  1505. wndG = GetGValue(pWndColor);
  1506. wndB = GetBValue(pWndColor);
  1507. absResult = abs(loadR - wndR) + abs(loadG - wndG) + abs(loadB - wndB); ///用于相似度比较
  1508. //if(pLoadBmpBuffer[i][j] == pWndBmpBuffer[h-colAdd][w+(j-fristX)])
  1509. //if(abs(loadR-wndR)<=m_colorOffsR && abs(loadG-wndG)<=m_colorOffsG && abs(loadB-wndB)<=m_colorOffsB )
  1510. if (abs(loadR - wndR) <= (m_colorOffsR + m_simColor) && abs(loadG - wndG) <= (m_colorOffsG + m_simColor) && abs(loadB - wndB) <= (m_colorOffsB + m_simColor))
  1511. {
  1512. continue;
  1513. }
  1514. else
  1515. {
  1516. return false;
  1517. }
  1518. }
  1519. }
  1520. }
  1521. return true;
  1522. }
  1523. ////////处理背景色为不透明的图片////////////////
  1524. //bool MyFindPictureClass::comparaLeftToRightFromBottomEx(int h,int w)
  1525. bool MyFindPictureClass::comparaLeftToRightFromBottomEx(int h, int w)
  1526. {
  1527. BYTE loadR = 0, loadG = 0, loadB = 0;
  1528. BYTE wndR = 0, wndG = 0, wndB = 0;
  1529. int absResult = 0;
  1530. //int colAdd =-1;
  1531. COLORREF pLoadColor = 0, pWndColor = 0;
  1532. for (int i = 0; i < loadHeight; i++)
  1533. {
  1534. for (int j = 0; j < loadWidth; j++)
  1535. {
  1536. pLoadColor = pLoadBmpBuffer[i][j];
  1537. loadR = GetRValue(pLoadColor);
  1538. loadG = GetGValue(pLoadColor);
  1539. loadB = GetBValue(pLoadColor);
  1540. pWndColor = pWndBmpBuffer[h + i][w + j];
  1541. wndR = GetRValue(pWndColor);
  1542. wndG = GetGValue(pWndColor);
  1543. wndB = GetBValue(pWndColor);
  1544. absResult = (loadR - wndR) + abs(loadG - wndG) + abs(loadB - wndB); ///用于相似度比较
  1545. //if(pLoadBmpBuffer[i][j] == pWndBmpBuffer[h+i][w+j])
  1546. //if(abs(loadR-wndR)<=m_colorOffsR && abs(loadG-wndG)<=m_colorOffsG && abs(loadB-wndB)<=m_colorOffsB )
  1547. if (abs(loadR - wndR) <= (m_colorOffsR + m_simColor) && abs(loadG - wndG) <= (m_colorOffsG + m_simColor) && abs(loadB - wndB) <= (m_colorOffsB + m_simColor))
  1548. {
  1549. continue;
  1550. }
  1551. else
  1552. {
  1553. return false;
  1554. }
  1555. }
  1556. }
  1557. return true;
  1558. }
  1559. ////////////////////////////////////////////////2:从右到左,从上倒下////////////////////////
  1560. //bool MyFindPictureClass::rightToLeftFromTop(int &xpos,int &ypos)
  1561. bool MyFindPictureClass::rightToLeftFromTop(long& xpos, long& ypos)
  1562. {
  1563. bool isFind = false;
  1564. bool isfirst = true;
  1565. COLORREF pColor = pLoadBmpBuffer[0][0]; //// 目标图片的背景色
  1566. if (pLoadBmpBuffer[0][0] == pLoadBmpBuffer[0][loadWidth - 1] &&
  1567. pLoadBmpBuffer[0][0] == pLoadBmpBuffer[loadHeight - 1][loadWidth - 1] &&
  1568. pLoadBmpBuffer[0][0] == pLoadBmpBuffer[loadHeight - 1][0])
  1569. {
  1570. int startx = 0, starty = 0;
  1571. for (int m = 0; m < loadHeight; m++)
  1572. {
  1573. for (int n = loadWidth - 1; n >= 0; n--)
  1574. {
  1575. if (pLoadBmpBuffer[m][n] == pColor)
  1576. {
  1577. //////过滤掉不处理的背景透明色
  1578. continue;
  1579. }
  1580. else
  1581. {
  1582. //2012.2.28 修改相似的图无法识别的问题.
  1583. startx = n;
  1584. starty = m;
  1585. goto toSerch;
  1586. }
  1587. }
  1588. if ((m + 1) == loadWidth)//如果全图是一个颜色,说明不是透明图
  1589. goto Serch;
  1590. }
  1591. toSerch:
  1592. for (int i = m_Top + ypos_offset; i <= m_bottom - 1; i++)
  1593. {
  1594. if (isfirst)
  1595. isfirst = false;
  1596. else if (xpos_offset != 0)
  1597. xpos_offset = 0;
  1598. if (m_bottom - i < loadHeight) goto end;
  1599. for (int j = m_Right - 1 - xpos_offset; j >= m_Left - 1; j--)
  1600. {
  1601. if (j - m_Left < loadWidth) break;
  1602. if (comparaRightToLeftFromTop(i, j, startx, starty))
  1603. {
  1604. isFind = true;
  1605. //xpos=j/*+m_helpXpoint-loadWidth+1*/;
  1606. xpos = j - loadWidth + 1;
  1607. ypos = i/*-m_helpYpoint*/;
  1608. goto end;
  1609. }
  1610. }
  1611. }
  1612. }
  1613. else
  1614. {
  1615. Serch:
  1616. for (int i = m_Top + ypos_offset; i <= m_bottom - 1; i++)
  1617. {
  1618. if (isfirst)
  1619. isfirst = false;
  1620. else if (xpos_offset != 0)
  1621. xpos_offset = 0;
  1622. if (m_bottom - i < loadHeight) goto end;
  1623. for (int j = m_Right - 1 - xpos_offset; j >= m_Left - 1; j--)
  1624. {
  1625. if (j - m_Left < loadWidth) break;
  1626. if (comparaRightToLeftFromTopEx(i, j))
  1627. {
  1628. isFind = true;
  1629. xpos = j - loadWidth + 1;
  1630. ypos = i;
  1631. goto end;
  1632. }
  1633. }
  1634. }
  1635. }
  1636. end:
  1637. return isFind;
  1638. }
  1639. ////////处理背景色为透明的图片////////////////
  1640. //bool MyFindPictureClass::comparaRightToLeftFromTop(int h,int w,int startX,int startY)
  1641. bool MyFindPictureClass::comparaRightToLeftFromTop(int h, int w, int startX, int startY)
  1642. {
  1643. int fristX = 0;
  1644. int fristY = 0;
  1645. bool isFrist = true;
  1646. COLORREF pColor = pLoadBmpBuffer[0][0]; //// 目标图片的背景色
  1647. BYTE loadR = 0, loadG = 0, loadB = 0;
  1648. BYTE wndR = 0, wndG = 0, wndB = 0;
  1649. int absResult = 0;
  1650. //int lineAdd =-1;
  1651. COLORREF pLoadColor = 0, pWndColor = 0;
  1652. for (int i = startY; i < loadHeight; i++)
  1653. {
  1654. //lineAdd =-1;
  1655. for (int j = loadWidth - 1; j >= 0; j--)
  1656. {
  1657. //lineAdd++;
  1658. if (pLoadBmpBuffer[i][j] == pColor)
  1659. {
  1660. //////过滤掉不处理的背景透明色
  1661. continue;
  1662. }
  1663. pLoadColor = pLoadBmpBuffer[i][j];
  1664. loadR = GetRValue(pLoadColor);
  1665. loadG = GetGValue(pLoadColor);
  1666. loadB = GetBValue(pLoadColor);
  1667. pWndColor = pWndBmpBuffer[h + i][w - (loadWidth - j - 1)];
  1668. wndR = GetRValue(pWndColor);
  1669. wndG = GetGValue(pWndColor);
  1670. wndB = GetBValue(pWndColor);
  1671. absResult = (loadR - wndR) + abs(loadG - wndG) + abs(loadB - wndB); ///用于相似度比较
  1672. //if(pLoadBmpBuffer[i][j] == pWndBmpBuffer[h+i][w+(/ *fristX* /0-lineAdd)])
  1673. //if(abs(loadR-wndR)<=m_colorOffsR && abs(loadG-wndG)<=m_colorOffsG && abs(loadB-wndB)<=m_colorOffsB )
  1674. if (abs(loadR - wndR) <= (m_colorOffsR + m_simColor) && abs(loadG - wndG) <= (m_colorOffsG + m_simColor) && abs(loadB - wndB) <= (m_colorOffsB + m_simColor))
  1675. {
  1676. continue;
  1677. }
  1678. else
  1679. {
  1680. return false;
  1681. }
  1682. //}
  1683. }
  1684. }
  1685. return true;
  1686. }
  1687. ////////处理背景色为不透明的图片////////////////
  1688. //bool MyFindPictureClass::comparaRightToLeftFromTopEx(int h,int w)
  1689. bool MyFindPictureClass::comparaRightToLeftFromTopEx(int h, int w)
  1690. {
  1691. BYTE loadR = 0, loadG = 0, loadB = 0;
  1692. BYTE wndR = 0, wndG = 0, wndB = 0;
  1693. int absResult = 0;
  1694. //int lineAdd =-1;
  1695. COLORREF pLoadColor = 0, pWndColor = 0;
  1696. for (int i = 0; i < loadHeight; i++)
  1697. {
  1698. //lineAdd = -1;
  1699. for (int j = loadWidth - 1; j >= 0; j--)
  1700. {
  1701. //lineAdd++;
  1702. pLoadColor = pLoadBmpBuffer[i][j];
  1703. loadR = GetRValue(pLoadColor);
  1704. loadG = GetGValue(pLoadColor);
  1705. loadB = GetBValue(pLoadColor);
  1706. pWndColor = pWndBmpBuffer[h + i][w - (loadWidth - j - 1)];
  1707. wndR = GetRValue(pWndColor);
  1708. wndG = GetGValue(pWndColor);
  1709. wndB = GetBValue(pWndColor);
  1710. absResult = (loadR - wndR) + abs(loadG - wndG) + abs(loadB - wndB); ///用于相似度比较
  1711. //if(pLoadBmpBuffer[i][j] == pWndBmpBuffer[h+i][w+j])
  1712. //if(abs(loadR-wndR)<=m_colorOffsR && abs(loadG-wndG)<=m_colorOffsG && abs(loadB-wndB)<=m_colorOffsB )
  1713. if (abs(loadR - wndR) <= (m_colorOffsR + m_simColor) && abs(loadG - wndG) <= (m_colorOffsG + m_simColor) && abs(loadB - wndB) <= (m_colorOffsB + m_simColor))
  1714. {
  1715. continue;
  1716. }
  1717. else
  1718. {
  1719. return false;
  1720. }
  1721. }
  1722. }
  1723. return true;
  1724. }
  1725. ///////////////////////////////////////////////////3:从右到左,从下倒上////////////////////
  1726. //bool MyFindPictureClass::rightToLeftFromBottom(int &xpos,int &ypos)
  1727. bool MyFindPictureClass::rightToLeftFromBottom(long& xpos, long& ypos)
  1728. {
  1729. bool isFind = false;
  1730. bool isfirst = true;
  1731. COLORREF pColor = pLoadBmpBuffer[0][0]; //// 目标图片的背景色
  1732. if (pLoadBmpBuffer[0][0] == pLoadBmpBuffer[0][loadWidth - 1] &&
  1733. pLoadBmpBuffer[0][0] == pLoadBmpBuffer[loadHeight - 1][loadWidth - 1] &&
  1734. pLoadBmpBuffer[0][0] == pLoadBmpBuffer[loadHeight - 1][0])
  1735. {
  1736. int startx = 0, starty = 0;
  1737. for (int m = loadHeight - 1; m >= 0; m--)
  1738. {
  1739. for (int n = loadWidth - 1; n >= 0; n--)
  1740. {
  1741. if (pLoadBmpBuffer[m][n] == pColor)
  1742. {
  1743. //////过滤掉不处理的背景透明色
  1744. continue;
  1745. }
  1746. else
  1747. {
  1748. //2012.2.28 修改相似的图无法识别的问题.
  1749. startx = n;
  1750. starty = m;
  1751. goto toSerch;
  1752. }
  1753. }
  1754. if ((m + 1) == loadWidth)//如果全图是一个颜色,说明不是透明图
  1755. goto Serch;
  1756. }
  1757. toSerch:
  1758. for (int i = m_bottom - ypos_offset; i >= m_Top - 1; i--)
  1759. {
  1760. if (isfirst)
  1761. isfirst = false;
  1762. else if (xpos_offset != 0)
  1763. xpos_offset = 0;
  1764. if (i - m_Top < loadHeight)
  1765. {
  1766. goto end;
  1767. }
  1768. for (int j = m_Right - xpos_offset; j >= m_Left - 1; j--)
  1769. {
  1770. if (j - m_Left < loadWidth)
  1771. {
  1772. break;
  1773. }
  1774. if (comparaRightToLeftFromBottom(i, j, startx, starty))
  1775. {
  1776. isFind = true;
  1777. xpos = j - startx - 1;
  1778. ypos = i - starty;
  1779. goto end;
  1780. }
  1781. }
  1782. }
  1783. }
  1784. else
  1785. {
  1786. Serch:
  1787. for (int i = m_bottom - ypos_offset; i >= m_Top - 1; i--)
  1788. {
  1789. if (isfirst)
  1790. isfirst = false;
  1791. else if (xpos_offset != 0)
  1792. xpos_offset = 0;
  1793. if (i - m_Top < loadHeight)
  1794. {
  1795. goto end;
  1796. }
  1797. for (int j = m_Right - xpos_offset; j >= m_Left - 1; j--)
  1798. {
  1799. if (j - m_Left < loadWidth)
  1800. {
  1801. break;
  1802. }
  1803. if (comparaRightToLeftFromBottomEx(i, j))
  1804. {
  1805. isFind = true;
  1806. xpos = j;
  1807. ypos = i;
  1808. goto end;
  1809. }
  1810. }
  1811. }
  1812. }
  1813. end:
  1814. return isFind;
  1815. }
  1816. ////////处理背景色为透明的图片////////////////
  1817. //bool MyFindPictureClass::comparaRightToLeftFromBottom(int h,int w,int startX,int startY)
  1818. bool MyFindPictureClass::comparaRightToLeftFromBottom(int h, int w, int startX, int startY)
  1819. {
  1820. int fristX = 0;
  1821. int fristY = 0;
  1822. bool isFrist = true;
  1823. COLORREF pColor = pLoadBmpBuffer[0][0]; //// 目标图片的背景色
  1824. BYTE loadR = 0, loadG = 0, loadB = 0;
  1825. BYTE wndR = 0, wndG = 0, wndB = 0;
  1826. int absResult = 0;
  1827. //int lineAdd=-1;
  1828. //int colAdd =-1;
  1829. COLORREF pLoadColor = 0, pWndColor = 0;
  1830. for (int i = startY; i >= 0; i--)
  1831. {
  1832. //colAdd ++;
  1833. //lineAdd=-1;
  1834. for (int j = loadWidth - 1; j >= 0; j--)
  1835. {
  1836. //lineAdd++;
  1837. if (pLoadBmpBuffer[i][j] == pColor)
  1838. {
  1839. //////过滤掉不处理的背景透明色
  1840. continue;
  1841. }
  1842. else
  1843. {
  1844. pLoadColor = pLoadBmpBuffer[i][j];
  1845. loadR = GetRValue(pLoadColor);
  1846. loadG = GetGValue(pLoadColor);
  1847. loadB = GetBValue(pLoadColor);
  1848. pWndColor = pWndBmpBuffer[h - (loadHeight - i - 1)][w - (loadWidth - j - 1)];
  1849. wndR = GetRValue(pWndColor);
  1850. wndG = GetGValue(pWndColor);
  1851. wndB = GetBValue(pWndColor);
  1852. if (abs(loadR - wndR) <= (m_colorOffsR + m_simColor) && abs(loadG - wndG) <= (m_colorOffsG + m_simColor) && abs(loadB - wndB) <= (m_colorOffsB + m_simColor))
  1853. {
  1854. continue;
  1855. }
  1856. else
  1857. {
  1858. return false;
  1859. }
  1860. }
  1861. }
  1862. }
  1863. return true;
  1864. }
  1865. ////////处理背景色为不透明的图片////////////////
  1866. //bool MyFindPictureClass::comparaRightToLeftFromBottomEx(int h,int w)
  1867. bool MyFindPictureClass::comparaRightToLeftFromBottomEx(int h, int w)
  1868. {
  1869. BYTE loadR = 0, loadG = 0, loadB = 0;
  1870. BYTE wndR = 0, wndG = 0, wndB = 0;
  1871. int absResult = 0;
  1872. //int lineAdd=-1;
  1873. //int colAdd =-1;
  1874. COLORREF pLoadColor = 0, pWndColor = 0;
  1875. //for(int i=loadHeight-1;i>=0;i--)
  1876. for (int i = 0; i < loadHeight; i++)
  1877. {
  1878. //colAdd ++;
  1879. //lineAdd=-1;
  1880. //for(int j=loadWidth-1;j>=0;j--)
  1881. for (int j = 0; j < loadWidth; j++)
  1882. {
  1883. //lineAdd++;
  1884. pLoadColor = pLoadBmpBuffer[i][j];
  1885. loadR = GetRValue(pLoadColor);
  1886. loadG = GetGValue(pLoadColor);
  1887. loadB = GetBValue(pLoadColor);
  1888. //COLORREF colorWnd=pWndBmpBuffer[h-colAdd][w-lineAdd];
  1889. pWndColor = pWndBmpBuffer[h + i][w + j];
  1890. wndR = GetRValue(pWndColor);
  1891. wndG = GetGValue(pWndColor);
  1892. wndB = GetBValue(pWndColor);
  1893. //absResult=(loadR-wndR)+abs(loadG-wndG)+abs(loadB-wndB); ///用于相似度比较
  1894. //if(abs(loadR-wndR)<=m_colorOffsR && abs(loadG-wndG)<=m_colorOffsG && abs(loadB-wndB)<=m_colorOffsB )
  1895. if (abs(loadR - wndR) <= (m_colorOffsR + m_simColor) && abs(loadG - wndG) <= (m_colorOffsG + m_simColor) && abs(loadB - wndB) <= (m_colorOffsB + m_simColor))
  1896. {
  1897. continue;
  1898. }
  1899. else
  1900. {
  1901. return false;
  1902. }
  1903. }
  1904. }
  1905. return true;
  1906. }
  1907. /////// 颜色内存处理。。 提高速度 //////////////////////
  1908. //bool MyFindPictureClass::processCreenWindow(int color,int left,int top,int right,int bottom,double simi,int dir,int &x,int &y,wchar_t* colorStr,bool isMulitColor)
  1909. bool MyFindPictureClass::processCreenWindow(int color, int left, int top, int right, int bottom, double simi, int dir, int& x, int& y, wchar_t* colorStr, bool isMulitColor)
  1910. {
  1911. int Width = GetSystemMetrics(SM_CXSCREEN);
  1912. int Height = GetSystemMetrics(SM_CYSCREEN);
  1913. ////////////////////////////四点的边界值判断////////////////
  1914. if (right<left || top>bottom)
  1915. return false;
  1916. if (left < 0)
  1917. left = 0;
  1918. if (top < 0)
  1919. top = 0;
  1920. if (left >= Width)
  1921. left = Width - 1;
  1922. if (top >= Height)
  1923. top = Height - 1;
  1924. if (right >= Width)
  1925. right = Width - 1;
  1926. if (bottom >= Height)
  1927. bottom = Height - 1;
  1928. ////////////////////////////////////////////////////////////
  1929. // 24位图的BITMAPINFO
  1930. BITMAPINFO* pBITMAPINFO = (BITMAPINFO*)malloc(sizeof(BITMAPINFOHEADER));
  1931. memset(pBITMAPINFO, 0, sizeof(BITMAPINFOHEADER));
  1932. BITMAPINFOHEADER* pInfo_Header = (BITMAPINFOHEADER*)pBITMAPINFO;
  1933. pInfo_Header->biSize = sizeof(BITMAPINFOHEADER);
  1934. pInfo_Header->biWidth = right - left; //宽度
  1935. pInfo_Header->biHeight = (bottom - top); //长度
  1936. pInfo_Header->biPlanes = 1; //目标设备的位平面数
  1937. pInfo_Header->biBitCount = 24; //对于每个像素的字节数 24:16M真彩色图,一般没有调色板,图像数据中每3个字节表示一个像素,每个字节表示一个RGB分量
  1938. pInfo_Header->biCompression = BI_RGB; //现实位图时必须的颜色数 0:表示所有颜色
  1939. HDC dc;
  1940. dc = ::CreateDC(L"DISPLAY", NULL, NULL, NULL);
  1941. HBITMAP hBitmap = ::CreateCompatibleBitmap(dc, Width, Height);
  1942. HDC tdc;
  1943. tdc = ::CreateCompatibleDC(dc);
  1944. HBITMAP hOldBitmap = (HBITMAP)::SelectObject(tdc, hBitmap);
  1945. BitBlt(tdc, 0, 0, pInfo_Header->biWidth, pInfo_Header->biHeight, dc, left, top, CAPTUREBLT | SRCCOPY);
  1946. hBitmap = (HBITMAP)::SelectObject(tdc, hOldBitmap);
  1947. // 获得数据buf
  1948. DWORD bufSize = (pInfo_Header->biWidth * 3 + 3) / 4 * 4 * pInfo_Header->biHeight;
  1949. BYTE* pBuffer = new BYTE[bufSize];
  1950. int aHeight = pInfo_Header->biHeight;
  1951. if (::GetDIBits(tdc, hBitmap, 0, aHeight, pBuffer, pBITMAPINFO, DIB_RGB_COLORS) == 0)
  1952. {
  1953. ::MessageBox(NULL, L"获取Color位图数据失败", L"TC", 0);
  1954. return false;
  1955. }
  1956. int width = pBITMAPINFO->bmiHeader.biWidth;
  1957. int height = pBITMAPINFO->bmiHeader.biHeight;
  1958. COLORREF** pColorBuffer = new COLORREF * [height]; ///// 二维数组 用来存储图像的颜色值
  1959. for (int i = 0; i < height; i++)
  1960. {
  1961. pColorBuffer[i] = new COLORREF[width];
  1962. }
  1963. int help_simi = 0;
  1964. if (simi == 0.5)
  1965. help_simi = 49;
  1966. else if (simi == 0.6)
  1967. help_simi = 38;
  1968. else if (simi == 0.7)
  1969. help_simi = 29;
  1970. else if (simi == 0.8)
  1971. help_simi = 18;
  1972. else if (simi == 0.9)
  1973. help_simi = 9;
  1974. COLORREF helpcolor = 0;
  1975. /*BYTE r1=0,g1=0,b1=0;*/
  1976. int pitch = width % 4;
  1977. for (int i = 0; i < height; i++) //获取范围内所有颜色值,并保存
  1978. {
  1979. int realPitch = i * pitch;
  1980. for (int j = 0; j < width; j++)
  1981. {
  1982. UCHAR b = pBuffer[(i * width + j) * 3 + realPitch];
  1983. UCHAR g = pBuffer[(i * width + j) * 3 + 1 + realPitch];
  1984. UCHAR r = pBuffer[(i * width + j) * 3 + 2 + realPitch];
  1985. helpcolor = RGB(r, g, b);
  1986. pColorBuffer[height - i - 1][j] = helpcolor;
  1987. }
  1988. }
  1989. //转换传入的颜色值参数
  1990. BYTE rValue = 0, gValue = 0, bValue = 0;
  1991. rValue = GetRValue((DWORD)color);
  1992. gValue = GetGValue((DWORD)color);
  1993. bValue = GetBValue((DWORD)color);
  1994. BYTE rHelpValue = 0, gHelpValue = 0, bHelpValue = 0;
  1995. //////////////////////////////四点的边界值判断////////////////
  1996. //if(left<0)
  1997. // left=0;
  1998. //if(left>=Width)
  1999. // left=Width-1;
  2000. //if(top<0)
  2001. // top=0;
  2002. //if(top>=Height)
  2003. // top=Height-1;
  2004. //if(right>=Width)
  2005. // right=Width-1;
  2006. //if(bottom>=Height)
  2007. // bottom=Height-1;
  2008. //////////////////////////////////////////////////////////////
  2009. /////////////////////////////////////////////////////////
  2010. if (isMulitColor) // // ////// 多个颜色值的找色方法
  2011. {
  2012. //XMLString::trim(colorStr);
  2013. /////////////////////0 从左到右 从上到下 方式查找///////////////
  2014. int absResult = 0;
  2015. for (int i = 0; i < bottom - top; i++)
  2016. {
  2017. for (int j = 0; j < right - left; j++)
  2018. {
  2019. rHelpValue = GetRValue(pColorBuffer[i][j]);
  2020. gHelpValue = GetGValue(pColorBuffer[i][j]);
  2021. bHelpValue = GetBValue(pColorBuffer[i][j]);
  2022. absResult = abs(rHelpValue - rValue) + abs(gHelpValue - gValue) + abs(bHelpValue - bValue);
  2023. ///////////////////判断第一个颜色值///////////////////////
  2024. if (absResult <= help_simi)
  2025. {
  2026. bool IsTrue = false;
  2027. int c_col = 0, c_xpoint = -1, c_ypoint = -1, c_num = 0, colorValue = 0, helpColor = 0;
  2028. wchar_t pointAndcolor[20] = { 0 };
  2029. //XMLString::trim(colorStr);
  2030. int lenght = wcslen(colorStr);
  2031. //int lenght = XMLString::stringLen(colorStr);
  2032. while (c_col < lenght && lenght != 0)
  2033. {
  2034. //////第一个x偏移坐标
  2035. while (colorStr[c_col] != '|')
  2036. {
  2037. pointAndcolor[c_num] = colorStr[c_col];
  2038. c_num++;
  2039. c_col++;
  2040. }
  2041. c_xpoint = ::wcstol(pointAndcolor, NULL, 10);
  2042. ::memset(pointAndcolor, 0, sizeof(wchar_t) * 20);
  2043. c_num = 0;
  2044. c_col++;
  2045. //////第二个Y偏移坐标
  2046. while (colorStr[c_col] != '|')
  2047. {
  2048. pointAndcolor[c_num] = colorStr[c_col];
  2049. c_num++;
  2050. c_col++;
  2051. }
  2052. c_ypoint = ::wcstol(pointAndcolor, NULL, 10);
  2053. ::memset(pointAndcolor, 0, sizeof(wchar_t) * 20);
  2054. c_num = 0;
  2055. c_col++;
  2056. //////第三个颜色值
  2057. while (colorStr[c_col] != ',' && colorStr[c_col])
  2058. {
  2059. pointAndcolor[c_num] = colorStr[c_col];
  2060. c_num++;
  2061. c_col++;
  2062. }
  2063. colorValue = ::wcstol(pointAndcolor, NULL, 16);
  2064. ::memset(pointAndcolor, 0, sizeof(wchar_t) * 20);
  2065. c_num = 0;
  2066. c_col++;
  2067. ////////坐标点已经超出屏幕的范围 /////////
  2068. if (i + c_ypoint > Height || j + c_xpoint > Width)
  2069. break;
  2070. helpColor = pColorBuffer[i + c_ypoint][j + c_xpoint];
  2071. absResult = abs(GetRValue(helpColor) - GetRValue(colorValue)) + abs(GetGValue(helpColor) - GetGValue(colorValue)) + abs(GetBValue(helpColor) - GetBValue(colorValue));
  2072. if (absResult <= help_simi)
  2073. {
  2074. IsTrue = true;
  2075. }
  2076. else
  2077. {
  2078. IsTrue = false;
  2079. break;
  2080. }
  2081. }
  2082. if (IsTrue)
  2083. {
  2084. x = j + left;
  2085. y = i + top;
  2086. goto end;
  2087. }
  2088. }
  2089. }
  2090. }
  2091. }
  2092. else
  2093. {
  2094. // // ////// 单个颜色值的找色方法
  2095. if (dir == 0)
  2096. {
  2097. /////////////////////0 从左到右 从上到下 方式查找///////////////
  2098. int absResult = 0;
  2099. for (int i = 0; i < bottom - top; i++)
  2100. {
  2101. for (int j = 0; j < right - left; j++)
  2102. {
  2103. rHelpValue = GetRValue(pColorBuffer[i][j]);
  2104. gHelpValue = GetGValue(pColorBuffer[i][j]);
  2105. bHelpValue = GetBValue(pColorBuffer[i][j]);
  2106. absResult = abs(rHelpValue - rValue) + abs(gHelpValue - gValue) + abs(bHelpValue - bValue);
  2107. if (absResult <= help_simi)
  2108. {
  2109. x = j + left;
  2110. y = i + top;
  2111. goto end;
  2112. }
  2113. }
  2114. }
  2115. }
  2116. else if (dir == 1)
  2117. {
  2118. /////////////////////1 从中心到四周 螺旋方式查找///////////////
  2119. int px = (right - left) / 2;
  2120. int py = (bottom - top) / 2;
  2121. int VisitCount = 0;
  2122. int count = 1;
  2123. int ii = 0;
  2124. int absResult = 0;
  2125. RUNTYPE runType = TurnUP;
  2126. while (VisitCount < (right - left) * (bottom - top))
  2127. {
  2128. for (ii = 0; ii < count; ii++)
  2129. {
  2130. if (px >= 0 && px < right - left && py >= 0 && py < bottom - top)
  2131. {
  2132. VisitCount++; ///执行次数累加
  2133. rHelpValue = GetRValue(pColorBuffer[py][px]);
  2134. gHelpValue = GetGValue(pColorBuffer[py][px]);
  2135. bHelpValue = GetBValue(pColorBuffer[py][px]);
  2136. absResult = abs(rHelpValue - rValue) + abs(gHelpValue - gValue) + abs(bHelpValue - bValue);
  2137. if (absResult <= help_simi)
  2138. { // 找到就退出循环
  2139. x = px + left;
  2140. y = py + top;
  2141. goto end;
  2142. }
  2143. }
  2144. //// 控制x,y变化
  2145. switch (runType)
  2146. {
  2147. case TurnUP:
  2148. py--;
  2149. break;
  2150. case TurnDOWN:
  2151. py++;
  2152. break;
  2153. case TurnLEFT:
  2154. px--;
  2155. break;
  2156. case TurnRIGHT:
  2157. px++;
  2158. break;
  2159. }
  2160. } //endfor
  2161. /////控制 螺旋方式流程的走向
  2162. switch (runType)
  2163. {
  2164. case TurnUP:
  2165. runType = TurnRIGHT;
  2166. break;
  2167. case TurnDOWN:
  2168. runType = TurnLEFT;
  2169. break;
  2170. case TurnLEFT:
  2171. runType = TurnUP;
  2172. count++;
  2173. break;
  2174. case TurnRIGHT:
  2175. runType = TurnDOWN;
  2176. count++;
  2177. break;
  2178. }
  2179. } ////endwhile
  2180. }///end else if
  2181. else if (dir == 2)
  2182. {
  2183. /////////////////////0 从右到左 从下到上 方式查找///////////////
  2184. for (int i = bottom - top - 1; i >= 0; i--)
  2185. {
  2186. for (int j = right - left - 1; j >= 0; j--)
  2187. {
  2188. rHelpValue = GetRValue(pColorBuffer[i][j]);
  2189. gHelpValue = GetGValue(pColorBuffer[i][j]);
  2190. bHelpValue = GetBValue(pColorBuffer[i][j]);
  2191. int absResult = abs(rHelpValue - rValue) + abs(gHelpValue - gValue) + abs(bHelpValue - bValue);
  2192. if (absResult <= help_simi)
  2193. {
  2194. x = j + left;
  2195. y = i + top;
  2196. goto end;
  2197. }
  2198. }
  2199. }
  2200. }
  2201. }
  2202. end:
  2203. free(pBITMAPINFO);
  2204. ::DeleteObject(hBitmap);
  2205. ::DeleteDC(dc);
  2206. ::DeleteDC(tdc);
  2207. for (int i = 0; i < height; i++)
  2208. {
  2209. delete[] pColorBuffer[i];
  2210. pColorBuffer[i] = NULL;
  2211. }
  2212. delete[] pColorBuffer;
  2213. pColorBuffer = NULL;
  2214. delete[] pBuffer;
  2215. return true;
  2216. }
  2217. COLORREF MyFindPictureClass::RgbToBgb(__in COLORREF rgb)
  2218. {
  2219. BYTE r;
  2220. BYTE g;
  2221. BYTE b;
  2222. r = GetRValue(rgb);
  2223. g = GetGValue(rgb);
  2224. b = GetBValue(rgb);
  2225. return r << 16 | g << 8 | b;
  2226. }
  2227. bool MyFindPictureClass::FindMultiColor(wchar_t* MultiColor, int i, int j, int Bottom, int right, int help_simi)//多点找色
  2228. {
  2229. bool IsTrue = false;
  2230. int c_col = 0, c_xpoint = -1, c_ypoint = -1, c_num = 0, colorValue = 0, helpColor = 0;
  2231. wchar_t pointAndcolor[20] = { 0 };
  2232. int lenght = wcslen(MultiColor);
  2233. while (c_col < lenght && lenght != 0)
  2234. {
  2235. //////第一个x偏移坐标
  2236. while (MultiColor[c_col] != '|')
  2237. {
  2238. pointAndcolor[c_num] = MultiColor[c_col];
  2239. c_num++;
  2240. c_col++;
  2241. }
  2242. c_xpoint = ::wcstol(pointAndcolor, NULL, 10);
  2243. ::memset(pointAndcolor, 0, sizeof(wchar_t) * 20);
  2244. c_num = 0;
  2245. c_col++;
  2246. //////第二个Y偏移坐标
  2247. while (MultiColor[c_col] != '|')
  2248. {
  2249. pointAndcolor[c_num] = MultiColor[c_col];
  2250. c_num++;
  2251. c_col++;
  2252. }
  2253. c_ypoint = ::wcstol(pointAndcolor, NULL, 10);
  2254. ::memset(pointAndcolor, 0, sizeof(wchar_t) * 20);
  2255. c_num = 0;
  2256. c_col++;
  2257. int coloroffsetValue = 0;//记录偏色
  2258. wchar_t coloroffsetstr[20] = { 0 };//记录偏色字符串
  2259. bool findoffset = false;
  2260. bool isGetAnti = false;//是否取反颜色
  2261. //////第三个颜色值
  2262. while (MultiColor[c_col] != ',' && MultiColor[c_col])
  2263. {
  2264. if (c_num == 0 && MultiColor[c_col] == '-')//第一个是否出现‘-’符号决定是否取反
  2265. {
  2266. isGetAnti = true;
  2267. }
  2268. if (MultiColor[c_col] == '-')
  2269. {
  2270. c_num = 0;
  2271. c_col++;
  2272. findoffset = true;
  2273. }
  2274. if (!findoffset)//没有找到偏色前记录颜色值
  2275. pointAndcolor[c_num] = MultiColor[c_col];
  2276. else
  2277. coloroffsetstr[c_num] = MultiColor[c_col];
  2278. c_num++;
  2279. c_col++;
  2280. }
  2281. colorValue = ::wcstol(pointAndcolor, NULL, 16);
  2282. coloroffsetValue = ::wcstol(coloroffsetstr, NULL, 16);
  2283. ::memset(pointAndcolor, 0, sizeof(wchar_t) * 20);
  2284. c_num = 0;
  2285. c_col++;
  2286. ////////坐标点已经超出屏幕的范围 /////////
  2287. //if(i+c_ypoint>=Height||j+c_xpoint>=Width)
  2288. if (i + c_ypoint >= Bottom || j + c_xpoint >= right)
  2289. break;
  2290. BYTE rHelpValue = GetRValue(pWndBmpBuffer[i + c_ypoint][j + c_xpoint]);
  2291. BYTE gHelpValue = GetGValue(pWndBmpBuffer[i + c_ypoint][j + c_xpoint]);
  2292. BYTE bHelpValue = GetBValue(pWndBmpBuffer[i + c_ypoint][j + c_xpoint]);
  2293. int BGRcolorValue = RgbToBgb(colorValue);
  2294. BYTE rValue = GetRValue(BGRcolorValue);
  2295. BYTE gValue = GetGValue(BGRcolorValue);
  2296. BYTE bValue = GetBValue(BGRcolorValue);
  2297. int BGRcoloroffsetValue = RgbToBgb(coloroffsetValue);
  2298. BYTE colorOffR = GetRValue(BGRcoloroffsetValue);
  2299. BYTE colorOffG = GetGValue(BGRcoloroffsetValue);
  2300. BYTE colorOffB = GetBValue(BGRcoloroffsetValue);
  2301. //helpColor = RgbToBgb(pWndBmpBuffer[i+c_ypoint][j+c_xpoint]);
  2302. //absResult=abs(GetRValue(helpColor)-GetRValue(colorValue))+abs(GetGValue(helpColor)-GetGValue(colorValue))+abs(GetBValue(helpColor)-GetBValue(colorValue));
  2303. //if(absResult<=help_simi)
  2304. if (abs(rHelpValue - rValue) <= (colorOffR + help_simi) && abs(gHelpValue - gValue) <= (colorOffG + help_simi) && abs(bHelpValue - bValue) <= (colorOffB + help_simi))
  2305. {
  2306. if (!isGetAnti)//判断是否取反颜色值,默认不是
  2307. IsTrue = true;
  2308. else
  2309. {
  2310. IsTrue = false;
  2311. break;
  2312. }
  2313. }
  2314. else
  2315. {
  2316. if (!isGetAnti)//判断是否取反颜色值,默认不是
  2317. {
  2318. IsTrue = false;
  2319. break;
  2320. }
  2321. else
  2322. IsTrue = true;
  2323. }
  2324. }
  2325. return IsTrue;
  2326. }
  2327. /////// 后台颜色内存处理。。 提高速度//////////////////////
  2328. int MyFindPictureClass::processColor(PVOID pbuffer, HWND src_hWnd, int& color, int left, int top, int right, int bottom, double simi, int dir, long& x, long& y, DWORD(*ColorDataInfo)[2000], wchar_t* colorstr, wchar_t* retstring, wchar_t* MultiColor)
  2329. {
  2330. int nret = -1;
  2331. RECT rc;
  2332. rc.bottom = bottom;
  2333. rc.left = left;
  2334. rc.right = right;
  2335. rc.top = top;
  2336. ////////////////////////////四点的边界值判断////////////////
  2337. {
  2338. RECT rc;
  2339. if (ColorDataInfo != NULL)
  2340. ::GetClientRect(m_hwnd, &rc);
  2341. else//gdi
  2342. ::GetClientRect(src_hWnd, &rc);
  2343. if (left < 0)
  2344. left = 0;
  2345. if (left >= rc.right)
  2346. left = rc.right - 1;
  2347. if (top < 0)
  2348. top = 0;
  2349. if (top >= rc.bottom)
  2350. top = rc.bottom - 1;
  2351. if (right < left | right >= rc.right)
  2352. right = rc.right - 1;
  2353. if (bottom < top | bottom >= rc.bottom)
  2354. bottom = rc.bottom - 1;
  2355. }
  2356. ////////////////////////////////////////////////////////////
  2357. if (m_parenthwnd != 0 && m_parenthwnd != ::GetDesktopWindow())//normol找图
  2358. {
  2359. RECT rec;
  2360. RECT clientrec;
  2361. ::GetClientRect(m_parenthwnd, &clientrec);
  2362. int clienthight = clientrec.bottom - clientrec.top;
  2363. int clientwide = clientrec.right - clientrec.left;
  2364. ::GetWindowRect(m_parenthwnd, &rec);
  2365. POINT point;
  2366. point.x = 0;
  2367. point.y = 0;
  2368. ::ClientToScreen(m_parenthwnd, &point);
  2369. m_Left = point.x;
  2370. m_Top = point.y;
  2371. //TS_GETCOLOR,取色
  2372. if (color == 0)
  2373. {
  2374. m_Left = 0;
  2375. m_Top = 0;
  2376. m_Right = rec.right;
  2377. m_bottom = rec.bottom;
  2378. }
  2379. else//找色
  2380. {
  2381. m_Left = point.x;
  2382. m_Top = point.y;
  2383. m_Right = rec.right;
  2384. m_bottom = rec.bottom;
  2385. if (left < 0)
  2386. left = 0;
  2387. if (left >= clientwide)
  2388. left = clientwide - 1;
  2389. if (top < 0)
  2390. top = 0;
  2391. if (top >= clienthight)
  2392. top = clienthight - 1;
  2393. if (right >= clientwide)
  2394. right = clientwide - 1;
  2395. if (bottom >= clienthight)
  2396. bottom = clienthight - 1;
  2397. if (m_Left < 0)
  2398. {
  2399. if ((left + m_Left) <= 0)//越界
  2400. left = 0;
  2401. else
  2402. left = m_Left + left;
  2403. }
  2404. else
  2405. left = m_Left + left;
  2406. if (m_Top >= 0)
  2407. top = m_Top + top;
  2408. if (m_Right >= ::GetSystemMetrics(SM_CXSCREEN))
  2409. {
  2410. if ((right + m_Left) > ::GetSystemMetrics(SM_CXSCREEN))
  2411. right = ::GetSystemMetrics(SM_CXSCREEN) - 1;
  2412. else
  2413. right = right + m_Left;
  2414. }
  2415. else
  2416. right = right + m_Left;
  2417. if (m_bottom >= ::GetSystemMetrics(SM_CYSCREEN))
  2418. {
  2419. if ((bottom + m_Top) >= ::GetSystemMetrics(SM_CYSCREEN))
  2420. bottom = ::GetSystemMetrics(SM_CYSCREEN) - 1;
  2421. else
  2422. bottom = bottom + m_Top;
  2423. }
  2424. else
  2425. bottom = bottom + m_Top;
  2426. m_Left = left;
  2427. m_Top = top;
  2428. m_Right = right;
  2429. m_bottom = bottom;
  2430. }
  2431. }
  2432. else
  2433. {
  2434. m_Left = left;
  2435. m_Top = top;
  2436. m_Right = right;
  2437. m_bottom = bottom;
  2438. }
  2439. rc.bottom = m_bottom;
  2440. rc.left = m_Left;
  2441. rc.right = m_Right;
  2442. rc.top = m_Top;
  2443. //TSRuntime::add_log( "m_Left:%d,m_Top:%d,m_Right:%d,m_bottom:%d",m_Left,m_Top,m_Right,m_bottom);
  2444. if (ColorDataInfo != NULL)
  2445. {
  2446. wndWidth = m_Right - m_Left;
  2447. wndHeight = m_bottom - m_Top;
  2448. //DX模式找字
  2449. if (!getDXBitmap(pbuffer, ColorDataInfo))
  2450. {
  2451. return nret;
  2452. }
  2453. }
  2454. else
  2455. {
  2456. ////GDI模式找字
  2457. if (!getGDIBitmap(src_hWnd, rc))
  2458. {
  2459. return nret;
  2460. }
  2461. }
  2462. //TS_GETCOLOR,取色
  2463. if (color == 0)
  2464. {
  2465. if (m_parenthwnd != 0 && m_parenthwnd != ::GetDesktopWindow())//normal
  2466. {
  2467. POINT pt;
  2468. pt.x = left;
  2469. pt.y = top;
  2470. ::ClientToScreen(m_parenthwnd, &pt);
  2471. color = pWndBmpBuffer[pt.y][pt.x];
  2472. }
  2473. else
  2474. color = pWndBmpBuffer[top][left];
  2475. //TSRuntime::add_log( "top:%d,left:%d",top,left);
  2476. goto end;
  2477. }
  2478. //处理多个色偏
  2479. int col = 0;
  2480. wchar_t colorBuffer[MAX_PATH][16];//存储多个字符串
  2481. for (int i = 0; i < MAX_PATH; i++)
  2482. {
  2483. memset(colorBuffer[i], 0, 16);
  2484. }
  2485. short nstr = 0;//要查找色偏的个数 ,最多支持(MAX_PATH)个色偏
  2486. while (*colorstr)
  2487. {
  2488. if (*colorstr != L'|')
  2489. {
  2490. if (col > 13)
  2491. goto end;
  2492. colorBuffer[nstr][col++] = *colorstr;
  2493. colorstr++;
  2494. continue;
  2495. }
  2496. else
  2497. {
  2498. if (col < 1)
  2499. {
  2500. colorstr++;
  2501. continue;
  2502. }
  2503. nstr++;
  2504. col = 0;
  2505. colorstr++;
  2506. }
  2507. }
  2508. if (col >= 1)
  2509. nstr++;
  2510. ncolors = nstr;//记录下色偏个数
  2511. //int t=::GetTickCount();
  2512. for (int i = 0; i < nstr; i++)
  2513. {
  2514. WCHAR colorR[4] = { 0 };//字的颜色
  2515. WCHAR colorG[4] = { 0 };//字的颜色
  2516. WCHAR colorB[4] = { 0 };//字的颜色
  2517. WCHAR coloroffR[4] = { 0 };//偏色
  2518. WCHAR coloroffG[4] = { 0 };//偏色
  2519. WCHAR coloroffB[4] = { 0 };//偏色
  2520. int count = 0;
  2521. int colorL = 0;
  2522. //StrTrimW(colorBuffer[i],L" ");//移除前后空格符
  2523. while (colorBuffer[i][colorL] != L'-')
  2524. {
  2525. if (count > 6) //判断是否是非法颜色值
  2526. break;
  2527. if (count < 2)
  2528. colorR[count] = colorBuffer[i][colorL++];
  2529. else if (count > 1 && count < 4)
  2530. colorG[count - 2] = colorBuffer[i][colorL++];
  2531. else if (count > 3)
  2532. colorB[count - 4] = colorBuffer[i][colorL++];
  2533. count++;
  2534. }
  2535. //将传入的RGB转换为BGR
  2536. WCHAR colorBGR[16] = { 0 };//字的颜色
  2537. swprintf(colorBGR, L"%s%s%s", colorB, colorG, colorR);
  2538. count = 0;
  2539. colorL++;
  2540. while (colorBuffer[colorL] != L'\0')
  2541. {
  2542. if (count >= 6) //判断是否是非法色偏值
  2543. break;
  2544. if (count < 2)
  2545. coloroffR[count] = colorBuffer[i][colorL++];
  2546. else if (count > 1 && count < 4)
  2547. coloroffG[count - 2] = colorBuffer[i][colorL++];
  2548. else if (count > 3)
  2549. coloroffB[count - 4] = colorBuffer[i][colorL++];
  2550. count++;
  2551. }
  2552. //将传入的RGB转换为BGR
  2553. WCHAR coloroffBGR[16] = { 0 };//偏色
  2554. swprintf(coloroffBGR, L"%s%s%s", coloroffB, coloroffG, coloroffR);
  2555. strColors[i] = wcstol(colorBGR, NULL, 16);//整形颜色值
  2556. strColoroff[i] = wcstol(coloroffBGR, NULL, 16);//整形偏色值
  2557. m_colorOffR[i] = GetRValue(strColoroff[i]);
  2558. m_colorOffG[i] = GetGValue(strColoroff[i]);
  2559. m_colorOffB[i] = GetBValue(strColoroff[i]);
  2560. }
  2561. int help_simi = 0;
  2562. if (simi == 0.5)
  2563. help_simi = 49;
  2564. else if (simi == 0.6)
  2565. help_simi = 38;
  2566. else if (simi == 0.7)
  2567. help_simi = 29;
  2568. else if (simi == 0.8)
  2569. help_simi = 18;
  2570. else if (simi == 0.9)
  2571. help_simi = 9;
  2572. for (int n = 0; n < ncolors; n++)
  2573. {
  2574. color = strColors[n];
  2575. BYTE rValue = 0, gValue = 0, bValue = 0;
  2576. rValue = GetRValue(color);
  2577. gValue = GetGValue(color);
  2578. bValue = GetBValue(color);
  2579. BYTE rHelpValue = 0, gHelpValue = 0, bHelpValue = 0;
  2580. if (MultiColor)//多点找色
  2581. {
  2582. /////////////////////0 从左到右 从上到下 方式查找///////////////
  2583. int absResult = 0;
  2584. if (dir == 0)
  2585. {
  2586. //TSRuntime::add_log("ncolors:%d",ncolors);
  2587. for (int i = top; i <= bottom; i++)
  2588. {
  2589. for (int j = left; j <= right; j++)
  2590. {
  2591. rHelpValue = GetRValue(pWndBmpBuffer[i][j]);
  2592. gHelpValue = GetGValue(pWndBmpBuffer[i][j]);
  2593. bHelpValue = GetBValue(pWndBmpBuffer[i][j]);
  2594. //int absResult=abs(rHelpValue-rValue-m_colorOffR[n])+abs(gHelpValue-gValue-m_colorOffG[n])+abs(bHelpValue-bValue-m_colorOffB[n]);
  2595. if (abs(rHelpValue - rValue) <= (m_colorOffR[n] + help_simi) && abs(gHelpValue - gValue) <= (m_colorOffG[n] + help_simi) && abs(bHelpValue - bValue) <= (m_colorOffB[n] + help_simi))
  2596. {
  2597. bool IsTrue = false;
  2598. IsTrue = FindMultiColor(MultiColor, i, j, bottom, right, help_simi);
  2599. if (IsTrue)
  2600. {
  2601. nret = 1;
  2602. x = j;
  2603. y = i;
  2604. if (m_parenthwnd != 0 && m_parenthwnd != ::GetDesktopWindow())
  2605. {
  2606. POINT pt;
  2607. pt.x = j;
  2608. pt.y = i;
  2609. ::ScreenToClient(m_parenthwnd, &pt);
  2610. x = pt.x;
  2611. y = pt.y;
  2612. }
  2613. if (retstring == NULL)
  2614. goto end;
  2615. else
  2616. {
  2617. int len = wcslen(retstring);
  2618. if (len > 1 && len < (MAX_PATH * 4 - 16))
  2619. swprintf(retstring, L"%s|%d,%d,%d", retstring, n, x, y);
  2620. else if (len == 0)
  2621. swprintf(retstring, L"%d,%d,%d", n, x, y);
  2622. }
  2623. }
  2624. else
  2625. nret = 0;
  2626. }
  2627. }
  2628. }
  2629. }
  2630. else if (dir == 1)
  2631. {
  2632. /////////////////////1 从左到右,从下到上 方式查找///////////////
  2633. for (int i = bottom; i >= top; i--)
  2634. {
  2635. for (int j = left; j <= right; j++)
  2636. {
  2637. rHelpValue = GetRValue(pWndBmpBuffer[i][j]);
  2638. gHelpValue = GetGValue(pWndBmpBuffer[i][j]);
  2639. bHelpValue = GetBValue(pWndBmpBuffer[i][j]);
  2640. if (abs(rHelpValue - rValue) <= (m_colorOffR[n] + help_simi) && abs(gHelpValue - gValue) <= (m_colorOffG[n] + help_simi) && abs(bHelpValue - bValue) <= (m_colorOffB[n] + help_simi))
  2641. {
  2642. bool IsTrue = false;
  2643. IsTrue = FindMultiColor(MultiColor, i, j, bottom, right, help_simi);
  2644. if (IsTrue)
  2645. {
  2646. nret = 1;
  2647. x = j;
  2648. y = i;
  2649. if (m_parenthwnd != 0 && m_parenthwnd != ::GetDesktopWindow())
  2650. {
  2651. POINT pt;
  2652. pt.x = j;
  2653. pt.y = i;
  2654. ::ScreenToClient(m_parenthwnd, &pt);
  2655. x = pt.x;
  2656. y = pt.y;
  2657. }
  2658. if (retstring == NULL)
  2659. goto end;
  2660. else
  2661. {
  2662. int len = wcslen(retstring);
  2663. if (len > 1 && len < (MAX_PATH * 4 - 16))
  2664. swprintf(retstring, L"%s|%d,%d,%d", retstring, n, x, y);
  2665. else if (len == 0)
  2666. swprintf(retstring, L"%d,%d,%d", n, x, y);
  2667. }
  2668. }
  2669. else
  2670. nret = 0;
  2671. }
  2672. }
  2673. }
  2674. }
  2675. else if (dir == 2)
  2676. {
  2677. /////////////////////2: 从右到左,从上到下 方式查找///////////////
  2678. for (int i = top; i <= bottom; i++)
  2679. {
  2680. for (int j = right; j >= left; j--)
  2681. {
  2682. rHelpValue = GetRValue(pWndBmpBuffer[i][j]);
  2683. gHelpValue = GetGValue(pWndBmpBuffer[i][j]);
  2684. bHelpValue = GetBValue(pWndBmpBuffer[i][j]);
  2685. //int absResult=abs(rHelpValue-rValue-m_colorOffR[n])+abs(gHelpValue-gValue-m_colorOffG[n])+abs(bHelpValue-bValue-m_colorOffB[n]);
  2686. //if(absResult<=help_simi)
  2687. if (abs(rHelpValue - rValue) <= (m_colorOffR[n] + help_simi) && abs(gHelpValue - gValue) <= (m_colorOffG[n] + help_simi) && abs(bHelpValue - bValue) <= (m_colorOffB[n] + help_simi))
  2688. {
  2689. bool IsTrue = false;
  2690. IsTrue = FindMultiColor(MultiColor, i, j, bottom, right, help_simi);
  2691. if (IsTrue)
  2692. {
  2693. nret = 1;
  2694. x = j;
  2695. y = i;
  2696. if (m_parenthwnd != 0 && m_parenthwnd != ::GetDesktopWindow())
  2697. {
  2698. POINT pt;
  2699. pt.x = j;
  2700. pt.y = i;
  2701. ::ScreenToClient(m_parenthwnd, &pt);
  2702. x = pt.x;
  2703. y = pt.y;
  2704. }
  2705. if (retstring == NULL)
  2706. goto end;
  2707. else
  2708. {
  2709. int len = wcslen(retstring);
  2710. if (len > 1 && len < (MAX_PATH * 4 - 16))
  2711. swprintf(retstring, L"%s|%d,%d,%d", retstring, n, x, y);
  2712. else if (len == 0)
  2713. swprintf(retstring, L"%d,%d,%d", n, x, y);
  2714. }
  2715. }
  2716. else
  2717. nret = 0;
  2718. }
  2719. }
  2720. }
  2721. }
  2722. else if (dir == 3)
  2723. {
  2724. /////////////////////3: 从右到左,从下到上 方式查找///////////////
  2725. for (int i = bottom; i >= top; i--)
  2726. {
  2727. for (int j = right; j >= left; j--)
  2728. {
  2729. rHelpValue = GetRValue(pWndBmpBuffer[i][j]);
  2730. gHelpValue = GetGValue(pWndBmpBuffer[i][j]);
  2731. bHelpValue = GetBValue(pWndBmpBuffer[i][j]);
  2732. //int absResult=abs(rHelpValue-rValue-m_colorOffR[n])+abs(gHelpValue-gValue-m_colorOffG[n])+abs(bHelpValue-bValue-m_colorOffB[n]);
  2733. //if(absResult<=help_simi)
  2734. if (abs(rHelpValue - rValue) <= (m_colorOffR[n] + help_simi) && abs(gHelpValue - gValue) <= (m_colorOffG[n] + help_simi) && abs(bHelpValue - bValue) <= (m_colorOffB[n] + help_simi))
  2735. {
  2736. bool IsTrue = false;
  2737. IsTrue = FindMultiColor(MultiColor, i, j, bottom, right, help_simi);
  2738. if (IsTrue)
  2739. {
  2740. nret = 1;
  2741. x = j;
  2742. y = i;
  2743. if (m_parenthwnd != 0 && m_parenthwnd != ::GetDesktopWindow())
  2744. {
  2745. POINT pt;
  2746. pt.x = j;
  2747. pt.y = i;
  2748. ::ScreenToClient(m_parenthwnd, &pt);
  2749. x = pt.x;
  2750. y = pt.y;
  2751. }
  2752. if (retstring == NULL)
  2753. goto end;
  2754. else
  2755. {
  2756. int len = wcslen(retstring);
  2757. if (len > 1 && len < (MAX_PATH * 4 - 16))
  2758. swprintf(retstring, L"%s|%d,%d,%d", retstring, n, x, y);
  2759. else if (len == 0)
  2760. swprintf(retstring, L"%d,%d,%d", n, x, y);
  2761. }
  2762. }
  2763. else
  2764. nret = 0;
  2765. }
  2766. }
  2767. }
  2768. }
  2769. }
  2770. else//正常找色
  2771. {
  2772. if (dir == 0)
  2773. {
  2774. //TSRuntime::add_log("ncolors:%d",ncolors);
  2775. /////////////////////0 从左到右 从上到下 方式查找///////////////
  2776. for (int i = top; i <= bottom; i++)
  2777. {
  2778. for (int j = left; j <= right; j++)
  2779. {
  2780. rHelpValue = GetRValue(pWndBmpBuffer[i][j]);
  2781. gHelpValue = GetGValue(pWndBmpBuffer[i][j]);
  2782. bHelpValue = GetBValue(pWndBmpBuffer[i][j]);
  2783. //int absResult=abs(rHelpValue-rValue-m_colorOffR[n])+abs(gHelpValue-gValue-m_colorOffG[n])+abs(bHelpValue-bValue-m_colorOffB[n]);
  2784. if (abs(rHelpValue - rValue) <= (m_colorOffR[n] + help_simi) && abs(gHelpValue - gValue) <= (m_colorOffG[n] + help_simi) && abs(bHelpValue - bValue) <= (m_colorOffB[n] + help_simi))
  2785. {
  2786. nret = n;
  2787. x = j;
  2788. y = i;
  2789. if (m_parenthwnd != 0 && m_parenthwnd != ::GetDesktopWindow())
  2790. {
  2791. POINT pt;
  2792. pt.x = j;
  2793. pt.y = i;
  2794. ::ScreenToClient(m_parenthwnd, &pt);
  2795. x = pt.x;
  2796. y = pt.y;
  2797. }
  2798. if (retstring == NULL)
  2799. goto end;
  2800. else
  2801. {
  2802. int len = wcslen(retstring);
  2803. if (len > 1 && len < (MAX_PATH * 4 - 16))
  2804. swprintf(retstring, L"%s|%d,%d,%d", retstring, n, x, y);
  2805. else if (len == 0)
  2806. swprintf(retstring, L"%d,%d,%d", n, x, y);
  2807. }
  2808. }
  2809. }
  2810. }
  2811. }
  2812. else if (dir == 1)
  2813. {
  2814. /////////////////////1 从左到右,从下到上 方式查找///////////////
  2815. for (int i = bottom; i >= top; i--)
  2816. {
  2817. for (int j = left; j <= right; j++)
  2818. {
  2819. rHelpValue = GetRValue(pWndBmpBuffer[i][j]);
  2820. gHelpValue = GetGValue(pWndBmpBuffer[i][j]);
  2821. bHelpValue = GetBValue(pWndBmpBuffer[i][j]);
  2822. //int absResult=abs(rHelpValue-rValue-m_colorOffR[n])+abs(gHelpValue-gValue-m_colorOffG[n])+abs(bHelpValue-bValue-m_colorOffB[n]);
  2823. //if(absResult<=help_simi)
  2824. if (abs(rHelpValue - rValue) <= (m_colorOffR[n] + help_simi) && abs(gHelpValue - gValue) <= (m_colorOffG[n] + help_simi) && abs(bHelpValue - bValue) <= (m_colorOffB[n] + help_simi))
  2825. {
  2826. nret = n;
  2827. x = j;
  2828. y = i;
  2829. if (m_parenthwnd != 0 && m_parenthwnd != ::GetDesktopWindow())
  2830. {
  2831. POINT pt;
  2832. pt.x = j;
  2833. pt.y = i;
  2834. ::ScreenToClient(m_parenthwnd, &pt);
  2835. x = pt.x;
  2836. y = pt.y;
  2837. }
  2838. if (retstring == NULL)
  2839. goto end;
  2840. else
  2841. {
  2842. int len = wcslen(retstring);
  2843. if (len > 1 && len < (MAX_PATH * 4 - 16))
  2844. swprintf(retstring, L"%s|%d,%d,%d", retstring, n, x, y);
  2845. else if (len == 0)
  2846. swprintf(retstring, L"%d,%d,%d", n, x, y);
  2847. }
  2848. }
  2849. }
  2850. }
  2851. }
  2852. else if (dir == 2)
  2853. {
  2854. /////////////////////2: 从右到左,从上到下 方式查找///////////////
  2855. for (int i = top; i <= bottom; i++)
  2856. {
  2857. for (int j = right; j >= left; j--)
  2858. {
  2859. rHelpValue = GetRValue(pWndBmpBuffer[i][j]);
  2860. gHelpValue = GetGValue(pWndBmpBuffer[i][j]);
  2861. bHelpValue = GetBValue(pWndBmpBuffer[i][j]);
  2862. //int absResult=abs(rHelpValue-rValue-m_colorOffR[n])+abs(gHelpValue-gValue-m_colorOffG[n])+abs(bHelpValue-bValue-m_colorOffB[n]);
  2863. //if(absResult<=help_simi)
  2864. if (abs(rHelpValue - rValue) <= (m_colorOffR[n] + help_simi) && abs(gHelpValue - gValue) <= (m_colorOffG[n] + help_simi) && abs(bHelpValue - bValue) <= (m_colorOffB[n] + help_simi))
  2865. {
  2866. nret = n;
  2867. x = j;
  2868. y = i;
  2869. if (m_parenthwnd != 0 && m_parenthwnd != ::GetDesktopWindow())
  2870. {
  2871. POINT pt;
  2872. pt.x = j;
  2873. pt.y = i;
  2874. ::ScreenToClient(m_parenthwnd, &pt);
  2875. x = pt.x;
  2876. y = pt.y;
  2877. }
  2878. if (retstring == NULL)
  2879. goto end;
  2880. else
  2881. {
  2882. int len = wcslen(retstring);
  2883. if (len > 1 && len < (MAX_PATH * 4 - 16))
  2884. swprintf(retstring, L"%s|%d,%d,%d", retstring, n, x, y);
  2885. else if (len == 0)
  2886. swprintf(retstring, L"%d,%d,%d", n, x, y);
  2887. }
  2888. }
  2889. }
  2890. }
  2891. }
  2892. else if (dir == 3)
  2893. {
  2894. /////////////////////3: 从右到左,从下到上 方式查找///////////////
  2895. for (int i = bottom; i >= top; i--)
  2896. {
  2897. for (int j = right; j >= left; j--)
  2898. {
  2899. rHelpValue = GetRValue(pWndBmpBuffer[i][j]);
  2900. gHelpValue = GetGValue(pWndBmpBuffer[i][j]);
  2901. bHelpValue = GetBValue(pWndBmpBuffer[i][j]);
  2902. //int absResult=abs(rHelpValue-rValue-m_colorOffR[n])+abs(gHelpValue-gValue-m_colorOffG[n])+abs(bHelpValue-bValue-m_colorOffB[n]);
  2903. //if(absResult<=help_simi)
  2904. if (abs(rHelpValue - rValue) <= (m_colorOffR[n] + help_simi) && abs(gHelpValue - gValue) <= (m_colorOffG[n] + help_simi) && abs(bHelpValue - bValue) <= (m_colorOffB[n] + help_simi))
  2905. {
  2906. nret = n;
  2907. x = j;
  2908. y = i;
  2909. if (m_parenthwnd != 0 && m_parenthwnd != ::GetDesktopWindow())
  2910. {
  2911. POINT pt;
  2912. pt.x = j;
  2913. pt.y = i;
  2914. ::ScreenToClient(m_parenthwnd, &pt);
  2915. x = pt.x;
  2916. y = pt.y;
  2917. }
  2918. if (retstring == NULL)
  2919. goto end;
  2920. else
  2921. {
  2922. int len = wcslen(retstring);
  2923. if (len > 1 && len < (MAX_PATH * 4 - 16))
  2924. swprintf(retstring, L"%s|%d,%d,%d", retstring, n, x, y);
  2925. else if (len == 0)
  2926. swprintf(retstring, L"%d,%d,%d", n, x, y);
  2927. }
  2928. }
  2929. }
  2930. }
  2931. }
  2932. else if (dir == 4)
  2933. {
  2934. /////////////////////4 从中心到四周 螺旋方式查找///////////////
  2935. int px = (right - left) / 2 + left;
  2936. int py = (bottom - top) / 2 + top;
  2937. int VisitCount = 0;
  2938. int count = 1;
  2939. int ii = 0;
  2940. int absResult = 0;
  2941. RUNTYPE runType = TurnUP;
  2942. while (VisitCount < (right - left) * (bottom - top))
  2943. {
  2944. for (ii = 0; ii < count; ii++)
  2945. {
  2946. if (px >= left && px < right && py >= top && py < bottom)
  2947. {
  2948. VisitCount++; ///执行次数累加
  2949. rHelpValue = GetRValue(pWndBmpBuffer[py][px]);
  2950. gHelpValue = GetGValue(pWndBmpBuffer[py][px]);
  2951. bHelpValue = GetBValue(pWndBmpBuffer[py][px]);
  2952. //absResult=abs(rHelpValue-rValue-m_colorOffR[n])+abs(gHelpValue-gValue-m_colorOffG[n])+abs(bHelpValue-bValue-m_colorOffB[n]);
  2953. //absResult =abs(rHelpValue-rValue)+abs(gHelpValue-gValue)+abs(bHelpValue-bValue);
  2954. //if(absResult<=help_simi)
  2955. if (abs(rHelpValue - rValue) <= (m_colorOffR[n] + help_simi) && abs(gHelpValue - gValue) <= (m_colorOffG[n] + help_simi) && abs(bHelpValue - bValue) <= (m_colorOffB[n] + help_simi))
  2956. { // 找到就退出循环
  2957. nret = n;
  2958. x = px;
  2959. y = py;
  2960. if (m_parenthwnd != 0 && m_parenthwnd != ::GetDesktopWindow())
  2961. {
  2962. POINT pt;
  2963. pt.x = x;
  2964. pt.y = y;
  2965. ::ScreenToClient(m_parenthwnd, &pt);
  2966. x = pt.x;
  2967. y = pt.y;
  2968. }
  2969. if (retstring == NULL)
  2970. goto end;
  2971. else
  2972. {
  2973. int len = wcslen(retstring);
  2974. if (len > 1 && len < (MAX_PATH * 4 - 16))
  2975. swprintf(retstring, L"%s|%d,%d,%d", retstring, n, x, y);
  2976. else if (len == 0)
  2977. swprintf(retstring, L"%d,%d,%d", n, x, y);
  2978. }
  2979. }
  2980. }
  2981. //// 控制x,y变化
  2982. switch (runType)
  2983. {
  2984. case TurnUP:
  2985. py--;
  2986. break;
  2987. case TurnDOWN:
  2988. py++;
  2989. break;
  2990. case TurnLEFT:
  2991. px--;
  2992. break;
  2993. case TurnRIGHT:
  2994. px++;
  2995. break;
  2996. }
  2997. } //endfor
  2998. /////控制 螺旋方式流程的走向
  2999. switch (runType)
  3000. {
  3001. case TurnUP:
  3002. runType = TurnRIGHT;
  3003. break;
  3004. case TurnDOWN:
  3005. runType = TurnLEFT;
  3006. break;
  3007. case TurnLEFT:
  3008. runType = TurnUP;
  3009. count++;
  3010. break;
  3011. case TurnRIGHT:
  3012. runType = TurnDOWN;
  3013. count++;
  3014. break;
  3015. }
  3016. } ////endwhile
  3017. }
  3018. else if (dir == 5)
  3019. {
  3020. /////////////////////从上到下,从左到右 方式查找///////////////
  3021. for (int j = left; j <= right; j++)
  3022. {
  3023. for (int i = top; i <= bottom; i++)
  3024. {
  3025. rHelpValue = GetRValue(pWndBmpBuffer[i][j]);
  3026. gHelpValue = GetGValue(pWndBmpBuffer[i][j]);
  3027. bHelpValue = GetBValue(pWndBmpBuffer[i][j]);
  3028. //int absResult=abs(rHelpValue-rValue-m_colorOffR[n])+abs(gHelpValue-gValue-m_colorOffG[n])+abs(bHelpValue-bValue-m_colorOffB[n]);
  3029. //if(absResult<=help_simi)
  3030. if (abs(rHelpValue - rValue) <= (m_colorOffR[n] + help_simi) && abs(gHelpValue - gValue) <= (m_colorOffG[n] + help_simi) && abs(bHelpValue - bValue) <= (m_colorOffB[n] + help_simi))
  3031. {
  3032. nret = n;
  3033. x = j;
  3034. y = i;
  3035. if (m_parenthwnd != 0 && m_parenthwnd != ::GetDesktopWindow())
  3036. {
  3037. POINT pt;
  3038. pt.x = j;
  3039. pt.y = i;
  3040. ::ScreenToClient(m_parenthwnd, &pt);
  3041. x = pt.x;
  3042. y = pt.y;
  3043. }
  3044. if (retstring == NULL)
  3045. goto end;
  3046. else
  3047. {
  3048. int len = wcslen(retstring);
  3049. if (len > 1 && len < (MAX_PATH * 4 - 16))
  3050. swprintf(retstring, L"%s|%d,%d,%d", retstring, n, x, y);
  3051. else if (len == 0)
  3052. swprintf(retstring, L"%d,%d,%d", n, x, y);
  3053. }
  3054. }
  3055. }
  3056. }
  3057. }
  3058. else if (dir == 6)
  3059. {
  3060. /////////////////////6: 从上到下,从右到左 方式查找///////////////
  3061. for (int j = right; j >= left; j--)
  3062. {
  3063. for (int i = top; i <= bottom; i++)
  3064. {
  3065. rHelpValue = GetRValue(pWndBmpBuffer[i][j]);
  3066. gHelpValue = GetGValue(pWndBmpBuffer[i][j]);
  3067. bHelpValue = GetBValue(pWndBmpBuffer[i][j]);
  3068. //int absResult=abs(rHelpValue-rValue-m_colorOffR[n])+abs(gHelpValue-gValue-m_colorOffG[n])+abs(bHelpValue-bValue-m_colorOffB[n]);
  3069. //if(absResult<=help_simi)
  3070. if (abs(rHelpValue - rValue) <= (m_colorOffR[n] + help_simi) && abs(gHelpValue - gValue) <= (m_colorOffG[n] + help_simi) && abs(bHelpValue - bValue) <= (m_colorOffB[n] + help_simi))
  3071. {
  3072. nret = n;
  3073. x = j;
  3074. y = i;
  3075. if (m_parenthwnd != 0 && m_parenthwnd != ::GetDesktopWindow())
  3076. {
  3077. POINT pt;
  3078. pt.x = j;
  3079. pt.y = i;
  3080. ::ScreenToClient(m_parenthwnd, &pt);
  3081. x = pt.x;
  3082. y = pt.y;
  3083. }
  3084. if (retstring == NULL)
  3085. goto end;
  3086. else
  3087. {
  3088. int len = wcslen(retstring);
  3089. if (len > 1 && len < (MAX_PATH * 4 - 16))
  3090. swprintf(retstring, L"%s|%d,%d,%d", retstring, n, x, y);
  3091. else if (len == 0)
  3092. swprintf(retstring, L"%d,%d,%d", n, x, y);
  3093. }
  3094. }
  3095. }
  3096. }
  3097. }
  3098. else if (dir == 7)
  3099. {
  3100. /////////////////////7: 从下到上,从左到右 方式查找///////////////
  3101. for (int j = left; j <= right; j++)
  3102. {
  3103. for (int i = bottom; i >= top; i--)
  3104. {
  3105. rHelpValue = GetRValue(pWndBmpBuffer[i][j]);
  3106. gHelpValue = GetGValue(pWndBmpBuffer[i][j]);
  3107. bHelpValue = GetBValue(pWndBmpBuffer[i][j]);
  3108. //int absResult=abs(rHelpValue-rValue-m_colorOffR[n])+abs(gHelpValue-gValue-m_colorOffG[n])+abs(bHelpValue-bValue-m_colorOffB[n]);
  3109. //if(absResult<=help_simi)
  3110. if (abs(rHelpValue - rValue) <= (m_colorOffR[n] + help_simi) && abs(gHelpValue - gValue) <= (m_colorOffG[n] + help_simi) && abs(bHelpValue - bValue) <= (m_colorOffB[n] + help_simi))
  3111. {
  3112. nret = n;
  3113. x = j;
  3114. y = i;
  3115. if (m_parenthwnd != 0 && m_parenthwnd != ::GetDesktopWindow())
  3116. {
  3117. POINT pt;
  3118. pt.x = j;
  3119. pt.y = i;
  3120. ::ScreenToClient(m_parenthwnd, &pt);
  3121. x = pt.x;
  3122. y = pt.y;
  3123. }
  3124. if (retstring == NULL)
  3125. goto end;
  3126. else
  3127. {
  3128. int len = wcslen(retstring);
  3129. if (len > 1 && len < (MAX_PATH * 4 - 16))
  3130. swprintf(retstring, L"%s|%d,%d,%d", retstring, n, x, y);
  3131. else if (len == 0)
  3132. swprintf(retstring, L"%d,%d,%d", n, x, y);
  3133. }
  3134. }
  3135. }
  3136. }
  3137. }
  3138. else if (dir == 8)
  3139. {
  3140. /////////////////////8: 从下到上,从右到左 方式查找///////////////
  3141. for (int j = right; j >= left; j--)
  3142. {
  3143. for (int i = bottom; i >= top; i--)
  3144. {
  3145. rHelpValue = GetRValue(pWndBmpBuffer[i][j]);
  3146. gHelpValue = GetGValue(pWndBmpBuffer[i][j]);
  3147. bHelpValue = GetBValue(pWndBmpBuffer[i][j]);
  3148. //int absResult=abs(rHelpValue-rValue-m_colorOffR[n])+abs(gHelpValue-gValue-m_colorOffG[n])+abs(bHelpValue-bValue-m_colorOffB[n]);
  3149. //if(absResult<=help_simi)
  3150. if (abs(rHelpValue - rValue) <= (m_colorOffR[n] + help_simi) && abs(gHelpValue - gValue) <= (m_colorOffG[n] + help_simi) && abs(bHelpValue - bValue) <= (m_colorOffB[n] + help_simi))
  3151. {
  3152. nret = n;
  3153. x = j;
  3154. y = i;
  3155. if (m_parenthwnd != 0 && m_parenthwnd != ::GetDesktopWindow())
  3156. {
  3157. POINT pt;
  3158. pt.x = j;
  3159. pt.y = i;
  3160. ::ScreenToClient(m_parenthwnd, &pt);
  3161. x = pt.x;
  3162. y = pt.y;
  3163. }
  3164. if (retstring == NULL)
  3165. goto end;
  3166. else
  3167. {
  3168. int len = wcslen(retstring);
  3169. if (len > 1 && len < (MAX_PATH * 4 - 16))
  3170. swprintf(retstring, L"%s|%d,%d,%d", retstring, n, x, y);
  3171. else if (len == 0)
  3172. swprintf(retstring, L"%d,%d,%d", n, x, y);
  3173. }
  3174. }
  3175. }
  3176. }
  3177. }
  3178. }
  3179. }
  3180. end:
  3181. return nret;
  3182. }
  3183. DWORD MyFindPictureClass::GetBkMouseShape(HCURSOR bkmouseshape)//默认bkmouseshape为空时为前台模式, bkmouseshape有值的时候是后台模式
  3184. {
  3185. HDC dDC = CreateDC(_T("DISPLAY"), NULL, NULL, NULL);
  3186. HDC hDC = CreateCompatibleDC(dDC);
  3187. HBITMAP hBitmap = CreateCompatibleBitmap(dDC, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON));
  3188. HBITMAP hOldBitmap = (HBITMAP)SelectObject(hDC, hBitmap);
  3189. CURSORINFO pci;
  3190. pci.cbSize = sizeof(CURSORINFO);
  3191. pci.flags = 0;
  3192. pci.hCursor = 0;
  3193. if (bkmouseshape == NULL) //前台模式
  3194. {
  3195. if (!GetCursorInfo(&pci))
  3196. {
  3197. return 0;
  3198. }
  3199. DrawIcon(hDC, 0, 0, pci.hCursor);
  3200. }
  3201. else//后台模式
  3202. {
  3203. DrawIcon(hDC, 0, 0, bkmouseshape);
  3204. }
  3205. hBitmap = (HBITMAP)SelectObject(hDC, hOldBitmap);
  3206. DWORD dwPaletteSize = 0, dwBmBitsSize;
  3207. BITMAP Bitmap;
  3208. BITMAPINFOHEADER bi;
  3209. //位图信息头结构
  3210. LPBITMAPINFOHEADER lpbi;
  3211. HANDLE hDib;
  3212. HPALETTE hOldPal = NULL;
  3213. //设置位图信息头结构
  3214. GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&Bitmap);
  3215. WORD wBitCount = Bitmap.bmBitsPixel;
  3216. dwBmBitsSize = ((Bitmap.bmWidth * wBitCount + 31) & ~31) / 8 * Bitmap.bmHeight;
  3217. BYTE* bmpDate = new BYTE[dwBmBitsSize];
  3218. bi.biSize = sizeof(BITMAPINFOHEADER);
  3219. bi.biWidth = Bitmap.bmWidth;
  3220. bi.biHeight = Bitmap.bmHeight;
  3221. bi.biPlanes = 1;
  3222. bi.biBitCount = wBitCount;
  3223. bi.biCompression = BI_RGB;
  3224. bi.biSizeImage = dwBmBitsSize;
  3225. bi.biXPelsPerMeter = 0;
  3226. bi.biYPelsPerMeter = 0;
  3227. bi.biClrUsed = 0;
  3228. bi.biClrImportant = 0;
  3229. hDib = GlobalAlloc(GHND, dwBmBitsSize + dwPaletteSize + sizeof(BITMAPINFOHEADER));
  3230. lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);
  3231. *lpbi = bi;
  3232. GetDIBits(hDC, hBitmap, 0, (UINT)Bitmap.bmHeight, bmpDate, (BITMAPINFO*)lpbi, DIB_RGB_COLORS);
  3233. DWORD num = 0;
  3234. int height = Bitmap.bmHeight;
  3235. int width = Bitmap.bmWidth;
  3236. int n = 0;//位图深度
  3237. ////////////////////////////////////////////////////////////////////////////////////////
  3238. //HDC hDC1 = ::GetWindowDC(::GetDesktopWindow());
  3239. //COLORREF helpcolor=0;
  3240. //int pitch=width%2;
  3241. ////int pitch=Pitch/4;
  3242. //for(int i=0;i<height;i++)
  3243. // {
  3244. // int realPitch=i*pitch;
  3245. // for(int j=0;j<width;j++)
  3246. // {
  3247. // //UCHAR b=tpData24[(i*wndWidth+j)*3+realPitch];
  3248. // //UCHAR g=tpData24[(i*wndWidth+j)*3+1+realPitch];
  3249. // //UCHAR r=tpData24[(i*wndWidth+j)*3+2+realPitch];
  3250. // UCHAR b=bmpDate[(i*width+j)*2+realPitch];
  3251. // UCHAR g=bmpDate[(i*width+j)*2+1+realPitch];
  3252. // UCHAR r=bmpDate[(i*width+j)*2+2+realPitch];
  3253. // helpcolor=RGB(r,g,b);
  3254. // ::SetPixel(hDC1,width-i-1,j,helpcolor);
  3255. // }
  3256. // }
  3257. //////////////////////////////////////////////////////////////////////////////////////
  3258. BYTE b0 = bmpDate[0];
  3259. BYTE g0 = bmpDate[1];
  3260. BYTE r0 = bmpDate[2];
  3261. if (Bitmap.bmBitsPixel == 32)
  3262. n = 4;
  3263. else if (Bitmap.bmBitsPixel == 24)
  3264. n = 3;
  3265. else if (Bitmap.bmBitsPixel == 16)
  3266. n = 2;
  3267. else if (Bitmap.bmBitsPixel == 8)
  3268. n = 1;
  3269. //TSRuntime::add_log( "Bitmap.bmBitsPixel:%d,bmpDate:%x",Bitmap.bmBitsPixel,bmpDate);
  3270. if (n != 0)
  3271. {
  3272. int pitch = width % n;
  3273. for (int i = 0; i < height; i++)
  3274. {
  3275. int realPitch = i * pitch;
  3276. for (int j = 0; j < width; j++)
  3277. {
  3278. UCHAR b = bmpDate[(i * width + j) * n + realPitch];
  3279. UCHAR g = bmpDate[(i * width + j) * n + 1 + realPitch];
  3280. UCHAR r = bmpDate[(i * width + j) * n + 2 + realPitch];
  3281. if (b == b0 && g == g0 && r == r0)
  3282. {
  3283. continue;
  3284. }
  3285. else
  3286. {
  3287. num++;
  3288. }
  3289. }
  3290. }
  3291. }
  3292. ////////////////////////////////////////////////////////////////////////
  3293. DWORD helpnum = num * RGB(255, 255, 0);
  3294. delete[] bmpDate;
  3295. ::GlobalFree(hDib);
  3296. ::DeleteDC(hDC);
  3297. ::DeleteDC(dDC);
  3298. ::DeleteObject(hBitmap);
  3299. return helpnum;
  3300. }
  3301. //************************************//
  3302. //函数名:hextobyte
  3303. //说明:将十六进制字符串转换成二进制字符串
  3304. // 参数:hexStr 十六进制字符串
  3305. // 参数:byteStr 二进制字符串
  3306. //
  3307. //************************************//
  3308. void MyFindPictureClass::hextobyte(char* hexStr, char* byteStr)
  3309. {
  3310. int i = 0;
  3311. while (hexStr[i] != '\0')
  3312. {
  3313. switch (hexStr[i])
  3314. {
  3315. case '0':
  3316. sprintf(byteStr, "%s%s", byteStr, "0000");
  3317. break;
  3318. case '1':
  3319. sprintf(byteStr, "%s%s", byteStr, "0001");
  3320. break;
  3321. case '2':
  3322. sprintf(byteStr, "%s%s", byteStr, "0010");
  3323. break;
  3324. case '3':
  3325. sprintf(byteStr, "%s%s", byteStr, "0011");
  3326. break;
  3327. case '4':
  3328. sprintf(byteStr, "%s%s", byteStr, "0100");
  3329. break;
  3330. case '5':
  3331. sprintf(byteStr, "%s%s", byteStr, "0101");
  3332. break;
  3333. case '6':
  3334. sprintf(byteStr, "%s%s", byteStr, "0110");
  3335. break;
  3336. case '7':
  3337. sprintf(byteStr, "%s%s", byteStr, "0111");
  3338. break;
  3339. case '8':
  3340. sprintf(byteStr, "%s%s", byteStr, "1000");
  3341. break;
  3342. case '9':
  3343. sprintf(byteStr, "%s%s", byteStr, "1001");
  3344. break;
  3345. case 'A':
  3346. sprintf(byteStr, "%s%s", byteStr, "1010");
  3347. break;
  3348. case 'B':
  3349. sprintf(byteStr, "%s%s", byteStr, "1011");
  3350. break;
  3351. case 'C':
  3352. sprintf(byteStr, "%s%s", byteStr, "1100");
  3353. break;
  3354. case 'D':
  3355. sprintf(byteStr, "%s%s", byteStr, "1101");
  3356. break;
  3357. case 'E':
  3358. sprintf(byteStr, "%s%s", byteStr, "1110");
  3359. break;
  3360. case 'F':
  3361. sprintf(byteStr, "%s%s", byteStr, "1111");
  3362. break;
  3363. }
  3364. i++;
  3365. }
  3366. }
  3367. //************************************//
  3368. //函数名:byteTopic
  3369. //说明:把字库信息还原成有色图像
  3370. // 参数:hexStr 十六进制字符串
  3371. // 参数:color 颜色
  3372. //
  3373. //************************************//
  3374. //void MyFindPictureClass::byteTopic(short strvalue[MAX_PATH],int strwide,COLORREF pLoadstrBuffer[11][MAX_PATH-10]) //MAX_PATH
  3375. void MyFindPictureClass::byteTopic(short strvalue[MAX_PATH], int strwide) //MAX_PATH
  3376. {
  3377. //char bytestr[1024*2]={0};
  3378. //memset(TSRuntime::bytestr,0,strwide*11+1);
  3379. memset(TSRuntime::bytestr, 0, strwide * 11 + 11);
  3380. for (int n = 0; n < strwide; n++)
  3381. {
  3382. char strbyte[12] = { 0 };
  3383. itoa(strvalue[n], strbyte, 2);
  3384. int len = strlen(strbyte);
  3385. if (len != 11)
  3386. {
  3387. for (int i = 0; i < (11 - len); i++)//在字符串前面补齐0
  3388. {
  3389. char Tstrbyte[12] = { 0 };
  3390. sprintf(Tstrbyte, "%s%s", "0", strbyte);
  3391. sprintf(strbyte, "%s", Tstrbyte);
  3392. }
  3393. }
  3394. sprintf(TSRuntime::bytestr, "%s%s", TSRuntime::bytestr, strbyte); //整形还原二进制字符串
  3395. }
  3396. int n = 4 - (11 * strwide) % 4;
  3397. if (n == 1)
  3398. sprintf(TSRuntime::bytestr, "%s%s", TSRuntime::bytestr, "0");
  3399. else if (n == 2)
  3400. sprintf(TSRuntime::bytestr, "%s%s", TSRuntime::bytestr, "00");
  3401. else if (n == 3)
  3402. sprintf(TSRuntime::bytestr, "%s%s", TSRuntime::bytestr, "000");
  3403. int hhh = 0;
  3404. int tempH = 11;
  3405. //正常视觉图
  3406. for (int w = 0; w < strwide; w++)
  3407. {
  3408. for (int h = 0; h < tempH; h++)
  3409. {
  3410. switch (TSRuntime::bytestr[hhh++])
  3411. {
  3412. case '0'://底色
  3413. TSRuntime::pLoadstrBuffer[h][w] = RGB(0, 0, 0);
  3414. break;
  3415. case '1'://前景色
  3416. TSRuntime::pLoadstrBuffer[h][w] = RGB(255, 255, 255);
  3417. break;
  3418. }
  3419. }
  3420. }
  3421. }
  3422. // 字库信息转二进制
  3423. void MyFindPictureClass::dicthextobyte(char* hexStr, char* byteStr, int& hexlen, short& nstrinfo)
  3424. {
  3425. int i = 0;
  3426. while (hexStr[i] != '\0')
  3427. {
  3428. //取字库点阵信息的2/3进行匹
  3429. switch (hexStr[i])
  3430. {
  3431. case '0':
  3432. {
  3433. sprintf(byteStr, "%s%s", byteStr, "0000");
  3434. }
  3435. break;
  3436. case '1':
  3437. {
  3438. sprintf(byteStr, "%s%s", byteStr, "0001");
  3439. nstrinfo++;
  3440. }
  3441. break;
  3442. case '2':
  3443. {
  3444. sprintf(byteStr, "%s%s", byteStr, "0010");
  3445. nstrinfo++;
  3446. }
  3447. break;
  3448. case '3':
  3449. {
  3450. sprintf(byteStr, "%s%s", byteStr, "0011");
  3451. nstrinfo = nstrinfo + 2;
  3452. }
  3453. break;
  3454. case '4':
  3455. {
  3456. sprintf(byteStr, "%s%s", byteStr, "0100");
  3457. nstrinfo++;
  3458. }
  3459. break;
  3460. case '5':
  3461. {
  3462. sprintf(byteStr, "%s%s", byteStr, "0101");
  3463. nstrinfo = nstrinfo + 2;
  3464. }
  3465. break;
  3466. case '6':
  3467. {
  3468. sprintf(byteStr, "%s%s", byteStr, "0110");
  3469. nstrinfo = nstrinfo + 2;
  3470. }
  3471. break;
  3472. case '7':
  3473. {
  3474. sprintf(byteStr, "%s%s", byteStr, "0111");
  3475. nstrinfo = nstrinfo + 3;
  3476. }
  3477. break;
  3478. case '8':
  3479. {
  3480. sprintf(byteStr, "%s%s", byteStr, "1000");
  3481. nstrinfo++;
  3482. }
  3483. break;
  3484. case '9':
  3485. {
  3486. sprintf(byteStr, "%s%s", byteStr, "1001");
  3487. nstrinfo = nstrinfo + 2;
  3488. }
  3489. break;
  3490. case 'A':
  3491. {
  3492. sprintf(byteStr, "%s%s", byteStr, "1010");
  3493. nstrinfo = nstrinfo + 2;
  3494. }
  3495. break;
  3496. case 'B':
  3497. {
  3498. sprintf(byteStr, "%s%s", byteStr, "1011");
  3499. nstrinfo = nstrinfo + 3;
  3500. }
  3501. break;
  3502. case 'C':
  3503. {
  3504. sprintf(byteStr, "%s%s", byteStr, "1100");
  3505. nstrinfo = nstrinfo + 2;
  3506. }
  3507. break;
  3508. case 'D':
  3509. {
  3510. sprintf(byteStr, "%s%s", byteStr, "1101");
  3511. nstrinfo = nstrinfo + 3;
  3512. }
  3513. break;
  3514. case 'E':
  3515. {
  3516. sprintf(byteStr, "%s%s", byteStr, "1110");
  3517. nstrinfo = nstrinfo + 3;
  3518. }
  3519. break;
  3520. case 'F':
  3521. {
  3522. sprintf(byteStr, "%s%s", byteStr, "1111");
  3523. nstrinfo = nstrinfo + 4;
  3524. }
  3525. break;
  3526. }
  3527. i++;
  3528. }
  3529. hexlen = (i + 1) * 4;//得到二进制的长度
  3530. }
  3531. bool MyFindPictureClass::comparaStr(short pLoadstrBufferxy[MAX_PATH], int strwide, int strhight, int& RetX, int& RetY, int startX, int startY, float simi, int nstrcount)
  3532. {
  3533. return true;
  3534. }
  3535. bool MyFindPictureClass::GetStringInfo()//获取位置点阵信息
  3536. {
  3537. return true;
  3538. }
  3539. bool MyFindPictureClass::MyUseDict(int Useindex, pMyDictInfo* MyDictCount, int& NowUsingDictIndex) //使用字库
  3540. {
  3541. if (Useindex >= 0 && Useindex < 20)
  3542. {
  3543. //if(TSRuntime::MyDictCount[Useindex]!=NULL)
  3544. if (MyDictCount[Useindex] != NULL)
  3545. {
  3546. //TSRuntime::NowUsingDictIndex=Useindex;
  3547. NowUsingDictIndex = Useindex;
  3548. return true;
  3549. }
  3550. }
  3551. return false;
  3552. }
  3553. bool MyFindPictureClass::MyDeleteDict(int deleteindex, pMyDictInfo* MyDictCount, int* nMyDictCountsize, int& NowUsingDictIndex) //删除字库
  3554. {
  3555. //如果不是注入模式,直接使用静态类
  3556. if (deleteindex >= 0 && deleteindex < 20)
  3557. {
  3558. //if(TSRuntime::MyDictCount[deleteindex]!=NULL)
  3559. // {
  3560. // TSRuntime::nMyDictCountsize[deleteindex]=0;
  3561. // TSRuntime::nMaxStrHight[deleteindex]=0;
  3562. // delete [] TSRuntime::MyDictCount[deleteindex];
  3563. // TSRuntime::MyDictCount[deleteindex]=NULL;
  3564. // return true;
  3565. // }
  3566. if (MyDictCount[deleteindex] != NULL)
  3567. {
  3568. nMyDictCountsize[deleteindex] = 0;
  3569. TSRuntime::nMaxStrHight[deleteindex] = 0;
  3570. delete[] MyDictCount[deleteindex];
  3571. MyDictCount[deleteindex] = NULL;
  3572. return true;
  3573. }
  3574. }
  3575. return false;
  3576. }
  3577. int MyFindPictureClass::FindStrFast(PVOID pbuffer, HWND hwnd, int left, int top, int right, int bottom, wchar_t* strings, wchar_t* color, double simi, long& x, long& y, pMyDictInfo* MyDictCount, int* nMyDictCountsize, int NowUsingDictIndex, DWORD(*ColorDataInfo)[2000], wchar_t* retstring, int type)
  3578. {
  3579. wchar_t retstr[MAX_PATH * 100] = { 0 };
  3580. x = -1;
  3581. y = -1;
  3582. int index = 0;
  3583. int retIndex = -1;
  3584. bool isFind = false;
  3585. //得到当前字库的下标
  3586. usingindex = NowUsingDictIndex;
  3587. //得到当前字库个数
  3588. dictindex = nMyDictCountsize[usingindex];
  3589. if (dictindex <= 0) //字库个数==0就退出
  3590. return isFind;
  3591. //处理多个色偏
  3592. int col = 0;
  3593. wchar_t colorBuffer[MAX_PATH][16];//存储多个字符串
  3594. for (int i = 0; i < MAX_PATH; i++)
  3595. {
  3596. memset(colorBuffer[i], 0, 16);
  3597. }
  3598. short nstr = 0;//要查找色偏的个数 ,最多支持(MAX_PATH)个色偏
  3599. while (*color)
  3600. {
  3601. if (*color != L'|')
  3602. {
  3603. if (col > 13)
  3604. return retIndex;
  3605. colorBuffer[nstr][col++] = *color;
  3606. color++;
  3607. continue;
  3608. }
  3609. else
  3610. {
  3611. if (col < 1)
  3612. {
  3613. color++;
  3614. continue;
  3615. }
  3616. nstr++;
  3617. col = 0;
  3618. color++;
  3619. }
  3620. }
  3621. if (col >= 1)
  3622. nstr++;
  3623. ncolors = nstr;//记录下色偏个数
  3624. //int t=::GetTickCount();
  3625. for (int i = 0; i < nstr; i++)
  3626. {
  3627. WCHAR colorR[4] = { 0 };//字的颜色
  3628. WCHAR colorG[4] = { 0 };//字的颜色
  3629. WCHAR colorB[4] = { 0 };//字的颜色
  3630. WCHAR coloroffR[4] = { 0 };//偏色
  3631. WCHAR coloroffG[4] = { 0 };//偏色
  3632. WCHAR coloroffB[4] = { 0 };//偏色
  3633. int count = 0;
  3634. int colorL = 0;
  3635. //StrTrimW(colorBuffer[i],L" ");//移除前后空格符
  3636. while (colorBuffer[i][colorL] != L'-')
  3637. {
  3638. if (count > 6) //判断是否是非法颜色值
  3639. return retIndex;
  3640. if (count < 2)
  3641. colorR[count] = colorBuffer[i][colorL++];
  3642. else if (count > 1 && count < 4)
  3643. colorG[count - 2] = colorBuffer[i][colorL++];
  3644. else if (count > 3)
  3645. colorB[count - 4] = colorBuffer[i][colorL++];
  3646. count++;
  3647. }
  3648. //将传入的RGB转换为BGR
  3649. WCHAR colorBGR[16] = { 0 };//字的颜色
  3650. swprintf(colorBGR, L"%s%s%s", colorB, colorG, colorR);
  3651. count = 0;
  3652. colorL++;
  3653. while (colorBuffer[colorL] != L'\0')
  3654. {
  3655. if (count >= 6) //判断是否是非法色偏值
  3656. break;
  3657. if (count < 2)
  3658. coloroffR[count] = colorBuffer[i][colorL++];
  3659. else if (count > 1 && count < 4)
  3660. coloroffG[count - 2] = colorBuffer[i][colorL++];
  3661. else if (count > 3)
  3662. coloroffB[count - 4] = colorBuffer[i][colorL++];
  3663. count++;
  3664. }
  3665. //将传入的RGB转换为BGR
  3666. WCHAR coloroffBGR[16] = { 0 };//偏色
  3667. swprintf(coloroffBGR, L"%s%s%s", coloroffB, coloroffG, coloroffR);
  3668. strColors[i] = wcstol(colorBGR, NULL, 16);//整形颜色值
  3669. strColoroff[i] = wcstol(coloroffBGR, NULL, 16);//整形偏色值
  3670. m_colorOffR[i] = GetRValue(strColoroff[i]);
  3671. m_colorOffG[i] = GetGValue(strColoroff[i]);
  3672. m_colorOffB[i] = GetBValue(strColoroff[i]);
  3673. }
  3674. //处理查找多个字
  3675. col = 0;
  3676. short stringslen = 0;
  3677. wchar_t StrBuffer[MAX_PATH][16];//存储多个字符串
  3678. for (int i = 0; i < MAX_PATH; i++)
  3679. {
  3680. memset(StrBuffer[i], 0, 16);
  3681. }
  3682. nstr = 0;//要查找字的个数 ,最多支持(MAX_PATH)个字
  3683. while (*strings)
  3684. {
  3685. if (*strings != L'|')
  3686. {
  3687. StrBuffer[nstr][col++] = *strings;
  3688. strings++;
  3689. continue;
  3690. stringslen++;
  3691. }
  3692. else
  3693. {
  3694. if (col < 1)
  3695. {
  3696. strings++;
  3697. continue;
  3698. }
  3699. nstr++;
  3700. col = 0;
  3701. strings++;
  3702. stringslen++;
  3703. }
  3704. }
  3705. if (col >= 1)
  3706. nstr++;
  3707. //TSRuntime::add_log("FindStrFast:重新拼接要查找的字符串-start");
  3708. //处理查找多个字
  3709. //memset(strings,0,wcslen(strings)*sizeof(wchar_t));
  3710. //memset(strings,0,stringslen);
  3711. wchar_t findstrs[MAX_PATH * 100] = { 0 };
  3712. for (int i = 0; i < nstr; i++)
  3713. {
  3714. swprintf(findstrs, L"%s%s", findstrs, StrBuffer[i]);//重新拼接要查找的字符串
  3715. }
  3716. short slen = wcslen(strings);//得到要查找字符串的长度
  3717. int nstringlen = 0;
  3718. int nstringindex[MAX_PATH * 4] = { 0 };//记录查找字符在字库中的位置下标
  3719. char finstrings[MAX_PATH * 100] = { 0 };
  3720. USES_CONVERSION;
  3721. strcpy(finstrings, W2A(findstrs));
  3722. //TSRuntime::add_log("finstrings:%s,strings:%s",finstrings,W2A(strings));
  3723. //TSRuntime::add_log("FindStrFast:重新拼接要查找的字符串-ing,dictindex:%d,strings:%s",dictindex,W2A(strings));
  3724. /////////////////////////////////////获取传进的要查找的字在字库中的位置(下标)
  3725. for (int n = 0; n < dictindex; n++)
  3726. {
  3727. bool bfind = false;
  3728. //if(TSRuntime::MyDictCount[usingindex][n].mydistchar)
  3729. {
  3730. char* strtofind = strstr(finstrings, MyDictCount[usingindex][n].mydistchar);
  3731. if (strtofind != NULL)
  3732. {
  3733. if (nstringindex[nstringlen] == 0)
  3734. {
  3735. nstringindex[nstringlen] = n;
  3736. nstringlen++;
  3737. }
  3738. //TSRuntime::add_log("strtofind:%s",strtofind);
  3739. //TSRuntime::add_log("nstringlen:%d,strtofind:%s",nstringlen,TSRuntime::MyDictCount[usingindex][n].mydistchar);
  3740. }
  3741. //TSRuntime::add_log("strtofind:%s",TSRuntime::MyDictCount[usingindex][n].mydistchar);
  3742. }
  3743. }
  3744. if (nstringlen == 0) //如果字库中没找到用户要找的字,返回失败
  3745. {
  3746. return -1;
  3747. }
  3748. //TSRuntime::add_log("nstringlen:%d",nstringlen);
  3749. /////////////////////////////////////获取传进的要查找的字在字库中的位置(下标)
  3750. if (m_parenthwnd != 0 && m_parenthwnd != ::GetDesktopWindow())//normol找图
  3751. {
  3752. RECT rec;
  3753. RECT clientrec;
  3754. ::GetClientRect(m_parenthwnd, &clientrec);
  3755. int clienthight = clientrec.bottom - clientrec.top;
  3756. int clientwide = clientrec.right - clientrec.left;
  3757. ::GetWindowRect(m_parenthwnd, &rec);
  3758. POINT point;
  3759. point.x = 0;
  3760. point.y = 0;
  3761. ::ClientToScreen(m_parenthwnd, &point);
  3762. m_Left = point.x;
  3763. m_Top = point.y;
  3764. m_Right = rec.right;
  3765. m_bottom = rec.bottom;
  3766. if (left < 0)
  3767. left = 0;
  3768. if (left >= clientwide)
  3769. left = clientwide - 1;
  3770. if (top < 0)
  3771. top = 0;
  3772. if (top >= clienthight)
  3773. top = clienthight - 1;
  3774. if (right >= clientwide)
  3775. right = clientwide - 1;
  3776. if (bottom >= clienthight)
  3777. bottom = clienthight - 1;
  3778. if (m_Left < 0)
  3779. {
  3780. if ((left + m_Left) <= 0)//越界
  3781. left = 0;
  3782. else
  3783. left = m_Left + left;
  3784. }
  3785. else
  3786. left = m_Left + left;
  3787. if (m_Top >= 0)
  3788. top = m_Top + top;
  3789. if (m_Right >= ::GetSystemMetrics(SM_CXSCREEN))
  3790. {
  3791. if ((right + m_Left) > ::GetSystemMetrics(SM_CXSCREEN))
  3792. right = ::GetSystemMetrics(SM_CXSCREEN) - 1;
  3793. else
  3794. right = right + m_Left;
  3795. }
  3796. else
  3797. right = right + m_Left;
  3798. if (m_bottom >= ::GetSystemMetrics(SM_CYSCREEN))
  3799. {
  3800. if ((bottom + m_Top) >= ::GetSystemMetrics(SM_CYSCREEN))
  3801. bottom = ::GetSystemMetrics(SM_CYSCREEN) - 1;
  3802. else
  3803. bottom = bottom + m_Top;
  3804. }
  3805. else
  3806. bottom = bottom + m_Top;
  3807. m_Left = left;
  3808. m_Top = top;
  3809. m_Right = right;
  3810. m_bottom = bottom;
  3811. }
  3812. else
  3813. {
  3814. RECT clientrec;
  3815. if (ColorDataInfo != NULL)
  3816. ::GetClientRect(m_hwnd, &clientrec);
  3817. else//gdi
  3818. ::GetClientRect(hwnd, &clientrec);
  3819. int clienthight = clientrec.bottom - clientrec.top;
  3820. int clientwide = clientrec.right - clientrec.left;
  3821. m_Left = left;
  3822. m_Top = top;
  3823. m_Right = right;
  3824. m_bottom = bottom;
  3825. if (left < 0)
  3826. m_Left = left = 0;
  3827. if (left >= clientwide)
  3828. m_Left = clientwide - 1;
  3829. if (top < 0)
  3830. m_Top = top = 0;
  3831. if (top >= clienthight)
  3832. m_Top = clienthight - 1;
  3833. if (right >= clientwide)
  3834. m_Right = clientwide - 1;
  3835. if (bottom >= clienthight)
  3836. m_bottom = clienthight - 1;
  3837. }
  3838. RECT rc;
  3839. rc.bottom = m_bottom;
  3840. rc.left = m_Left;
  3841. rc.right = m_Right;
  3842. rc.top = m_Top;
  3843. if (ColorDataInfo != NULL)
  3844. {
  3845. wndWidth = m_Right - m_Left;
  3846. wndHeight = m_bottom - m_Top;
  3847. //DX模式找字
  3848. if (!getDXBitmap(pbuffer, ColorDataInfo, 1))
  3849. {
  3850. return retIndex;
  3851. }
  3852. }
  3853. else
  3854. {
  3855. ////GDI模式找字
  3856. if (!getGDIBitmap(hwnd, rc, 1))
  3857. {
  3858. return retIndex;
  3859. }
  3860. }
  3861. /////////////////////////////控制四点的边界值以防止越界//////////////////////////
  3862. GetStringInfo();
  3863. DWORD Findstrings[MAX_PATH * 10] = { 0 };
  3864. bool UnkownStr[MAX_PATH * 10] = { 0 };//未知字体
  3865. /////////////////// 处理多张图 /////////////////////
  3866. int indexstr = 0;
  3867. int findstrX = -1;
  3868. int findstrY = -1;
  3869. int X = 0;
  3870. int Y = 0;
  3871. BYTE bfindstr = 0;
  3872. for (int i = 0; i < nstringlen; i++)
  3873. {
  3874. bool strfind = false;
  3875. if (simi == 1.0)//精确查找
  3876. {
  3877. //使用指定字库
  3878. int nstrvaluelen = pWndBmpStr[MyDictCount[usingindex][nstringindex[i]].nstrvalue[0]].size();
  3879. for (int n = 0; n < nstrvaluelen; n++)
  3880. {
  3881. findstrX = pWndBmpStr[MyDictCount[usingindex][nstringindex[i]].nstrvalue[0]].at(n).x;
  3882. findstrY = pWndBmpStr[MyDictCount[usingindex][nstringindex[i]].nstrvalue[0]].at(n).y;
  3883. if (findstrX == -1 || findstrY == -1)
  3884. continue;
  3885. if (comparaStr(MyDictCount[usingindex][nstringindex[i]].nstrvalue, MyDictCount[usingindex][nstringindex[i]].strwide, MyDictCount[usingindex][nstringindex[i]].strhight, X, Y, findstrX, findstrY, simi, MyDictCount[usingindex][nstringindex[i]].nstrcount))
  3886. {
  3887. strfind = true;
  3888. int swide = MyDictCount[usingindex][nstringindex[i]].strwide;
  3889. int shight = MyDictCount[usingindex][nstringindex[i]].strhight;
  3890. for (int k = 0; k <= swide; k++)//判断是否是非正规字体
  3891. {
  3892. if ((pWndBmpStrxy[Y - 1][X - 1] != 0 && pWndBmpStrxy[Y - 1][X - 1] >= 16) && shight >= 11)//左上角
  3893. {
  3894. UnkownStr[indexstr] = true;//非正规字体标识
  3895. break;
  3896. }
  3897. if (pWndBmpStrxy[Y - 1][X + k] != 0 && pWndBmpStrxy[Y - 1][X + k] >= 1024)
  3898. {
  3899. UnkownStr[indexstr] = true;//非正规字体标识
  3900. break;
  3901. }
  3902. }
  3903. //bool UnderlineString=true;//下滑线字体
  3904. //bool bfind=false;
  3905. //for(int k=0;k<=swide;k++)
  3906. //{
  3907. // if((pWndBmpStrxy[Y][X+k]%2==0)||pWndBmpStrxy[Y][X+k]==0||swide<2||shight>11)//判断字体最后一行是否是下划线
  3908. // UnderlineString=false;
  3909. // //if(shight<=2&&pWndBmpStrxy[Y-4][X+k]!=0&&swide>=10) //区分三和一字的重合问题
  3910. // // bfind=true;
  3911. // if(swide>=14&&shight==1&&pWndBmpStrxy[Y-2][X+k]!=0)//判断区别处理字体13号字体的点阵例如:"一"和"查"
  3912. // bfind=true;
  3913. // if(pWndBmpStrxy[Y-1][X+k]!=0&&pWndBmpStrxy[Y-1][X+k]>=1024)
  3914. // bfind=true;
  3915. // else if((pWndBmpStrxy[Y-1][X+swide]!=0&&pWndBmpStrxy[Y-1][X+swide]>=16)&&shight>=11) //右上角
  3916. // bfind=true;
  3917. // else if((pWndBmpStrxy[Y-1][X-1]!=0&&pWndBmpStrxy[Y-1][X-1]>=16)&&shight>=11)//左上角
  3918. // bfind=true;
  3919. // if((pWndBmpStrxy[Y][X-1]!=0||pWndBmpStrxy[Y][X+swide]!=0)&&shight<5)
  3920. // bfind=true;
  3921. // if(swide==1&&(pWndBmpStrxy[Y-1][X-1]!=0||pWndBmpStrxy[Y-1][X+swide]!=0)) //处理特殊符号
  3922. // bfind=true;
  3923. // if(bfind)
  3924. // break;
  3925. //}
  3926. //if(bfind)
  3927. //{
  3928. // if(UnderlineString==false)
  3929. // continue;
  3930. //}
  3931. if (m_parenthwnd != 0 && m_parenthwnd != ::GetDesktopWindow())//normol模式
  3932. {
  3933. POINT pt;
  3934. pt.x = X + m_Left;
  3935. pt.y = Y + m_Top;
  3936. ::ScreenToClient(m_parenthwnd, &pt);
  3937. addrxy[indexstr][0] = pt.x;
  3938. addrxy[indexstr][1] = pt.y;
  3939. }
  3940. else
  3941. {
  3942. addrxy[indexstr][0] = X + m_Left;
  3943. addrxy[indexstr][1] = Y + m_Top;
  3944. }
  3945. addrxy[indexstr][2] = swide;
  3946. addrxy[indexstr][3] = shight;
  3947. USES_CONVERSION;
  3948. Findstrings[indexstr] = (DWORD)MyDictCount[usingindex][nstringindex[i]].mydistchar;
  3949. //pWndBmpStr[MyDictCount[usingindex][nstringindex[i]].nstrvalue[0]].at(n).x=-1;
  3950. //pWndBmpStr[MyDictCount[usingindex][nstringindex[i]].nstrvalue[0]].at(n).y=-1;
  3951. indexstr++;
  3952. }
  3953. if (indexstr >= MAX_PATH * 10 - 1)
  3954. break;
  3955. }
  3956. if (indexstr >= MAX_PATH * 10 - 1)
  3957. break;
  3958. }
  3959. else if (simi > 0.1 && simi < 1.0)//处理相似度
  3960. {
  3961. //使用指定字库
  3962. //int nstrwide=TSRuntime::MyDictCount[usingindex][nstringindex[i]].strwide;
  3963. int nstrwide = MyDictCount[usingindex][nstringindex[i]].strwide;
  3964. //COLORREF pLoadstrBuffer[11][MAX_PATH-10];
  3965. for (int a = 0; a < 11; a++)
  3966. {
  3967. memset(TSRuntime::pLoadstrBuffer[a], 0, nstrwide + 1);
  3968. }
  3969. //将点阵信息还原图片矩阵信息
  3970. //byteTopic(TSRuntime::MyDictCount[usingindex][nstringindex[i]].nstrvalue,nstrwide, pLoadstrBuffer);
  3971. //byteTopic(TSRuntime::MyDictCount[usingindex][nstringindex[i]].nstrvalue,nstrwide);
  3972. byteTopic(MyDictCount[usingindex][nstringindex[i]].nstrvalue, nstrwide);
  3973. for (int j = 1; j < 2048; j++)
  3974. {
  3975. int nstrvaluelen = pWndBmpStr[j].size();
  3976. for (int n = 0; n < nstrvaluelen; n++)
  3977. {
  3978. findstrX = pWndBmpStr[j].at(n).x;
  3979. findstrY = pWndBmpStr[j].at(n).y;
  3980. if (findstrX == -1 || findstrY == -1)
  3981. {
  3982. continue;
  3983. }
  3984. //if(comparaStrTosim(nstrwide,TSRuntime::MyDictCount[usingindex][nstringindex[i]].strhight,X,Y,findstrX,findstrY,simi,TSRuntime::MyDictCount[usingindex][nstringindex[i]].nstrcount))
  3985. if (comparaStrTosim(nstrwide, MyDictCount[usingindex][nstringindex[i]].strhight, X, Y, findstrX, findstrY, simi, MyDictCount[usingindex][nstringindex[i]].nstrcount))
  3986. {
  3987. //int swide=MyDictCount[usingindex][nstringindex[i]].strwide;
  3988. //int shight=MyDictCount[usingindex][nstringindex[i]].strhight;
  3989. //for(int k=0;k<=swide;k++)//判断是否是非正规字体
  3990. //{
  3991. // if((pWndBmpStrxy[Y-1][X-1]!=0&&pWndBmpStrxy[Y-1][X-1]>=16)&&shight>=11)//左上角
  3992. // {
  3993. // UnkownStr[indexstr]=true;//非正规字体标识
  3994. // break;
  3995. // }
  3996. // if(pWndBmpStrxy[Y-1][X+k]!=0&&pWndBmpStrxy[Y-1][X+k]>=1024)
  3997. // {
  3998. // UnkownStr[indexstr]=true;//非正规字体标识
  3999. // break;
  4000. // }
  4001. //}
  4002. USES_CONVERSION;
  4003. //wchar_t *finds=wcsstr(retstr,A2W(TSRuntime::MyDictCount[usingindex][nstringindex[i]].mydistchar));
  4004. wchar_t* finds = wcsstr(retstr, A2W(MyDictCount[usingindex][nstringindex[i]].mydistchar));
  4005. if (finds != NULL)
  4006. {
  4007. bool bfind = false;
  4008. for (int k = 0; k < indexstr; k++)
  4009. {
  4010. //if(abs(addrxy[k][0]-X)<=TSRuntime::MyDictCount[usingindex][nstringindex[i]].strwide&&abs(addrxy[k][1]-Y)<=TSRuntime::MyDictCount[usingindex][nstringindex[i]].strhight)
  4011. if (abs(addrxy[k][0] - X) <= MyDictCount[usingindex][nstringindex[i]].strwide && abs(addrxy[k][1] - Y) <= MyDictCount[usingindex][nstringindex[i]].strhight)
  4012. {
  4013. bfind = true;
  4014. break;
  4015. }
  4016. }
  4017. if (bfind)
  4018. continue;
  4019. }
  4020. strfind = true;
  4021. if (m_parenthwnd != 0 && m_parenthwnd != ::GetDesktopWindow())//normol模式
  4022. {
  4023. POINT pt;
  4024. pt.x = X + m_Left;
  4025. pt.y = Y + m_Top;
  4026. ::ScreenToClient(m_parenthwnd, &pt);
  4027. addrxy[indexstr][0] = pt.x;
  4028. addrxy[indexstr][1] = pt.y;
  4029. }
  4030. else
  4031. {
  4032. addrxy[indexstr][0] = X + m_Left;
  4033. addrxy[indexstr][1] = Y + m_Top;
  4034. }
  4035. /////////取背景图 点阵坐标
  4036. //wsprintf(retstr,L"%s%s",retstr,A2W(TSRuntime::MyDictCount[usingindex][nstringindex[i]].mydistchar));
  4037. //Findstrings[indexstr]=(DWORD)TSRuntime::MyDictCount[usingindex][nstringindex[i]].mydistchar;
  4038. //for(int p=0;p<TSRuntime::MyDictCount[usingindex][nstringindex[i]].strwide;p++)
  4039. Findstrings[indexstr] = (DWORD)MyDictCount[usingindex][nstringindex[i]].mydistchar;
  4040. //for(int p=0;p<MyDictCount[usingindex][nstringindex[i]].strwide;p++)
  4041. //{
  4042. // pWndBmpStrxy[Y][X+p]=0;
  4043. //}
  4044. //pWndBmpStr[j].at(n).x=-1;
  4045. //pWndBmpStr[j].at(n).y=-1;
  4046. indexstr++;
  4047. }
  4048. if (indexstr >= MAX_PATH * 10 - 1)
  4049. break;
  4050. }
  4051. if (indexstr >= MAX_PATH * 10 - 1)
  4052. break;
  4053. }
  4054. if (indexstr >= MAX_PATH * 10 - 1)
  4055. break;
  4056. }
  4057. if (!strfind && nstr == 1)
  4058. {
  4059. retIndex = -1;
  4060. x = -1;
  4061. y = -1;
  4062. }
  4063. }
  4064. ////TSRuntime::add_log( "TS_OCR-排序-start");
  4065. int addrxyBak[MAX_PATH * 10][4] = { 0 };
  4066. wchar_t tempfindstr[256] = { 0 };
  4067. wchar_t tempfindstr1[256] = { 0 };
  4068. for (int i = 0; i < indexstr - 1; i++)
  4069. {
  4070. retstr[i];
  4071. retstr[i + 1];
  4072. if (addrxy[i + 1][1] < addrxy[i][1])
  4073. {
  4074. for (int j = i + 1; j > 0; j--)
  4075. {
  4076. if (addrxy[j][1] < addrxy[j - 1][1])
  4077. {
  4078. //这里做交换操作
  4079. int tem[10 * 2] = { 0 };
  4080. ::memcpy(tem, addrxy[j - 1], 4 * 4);
  4081. ::memcpy(addrxy[j - 1], addrxy[j], 4 * 4);
  4082. ::memcpy(addrxy[j], tem, 4 * 4);
  4083. DWORD csTemp = Findstrings[j - 1];
  4084. Findstrings[j - 1] = Findstrings[j];
  4085. Findstrings[j] = csTemp;
  4086. bool bret = UnkownStr[j - 1];
  4087. UnkownStr[j - 1] = UnkownStr[j];
  4088. UnkownStr[j] = bret;
  4089. addrxyBak[j - 1][0] = addrxy[j - 1][0];
  4090. addrxyBak[j - 1][1] = addrxy[j - 1][1];
  4091. addrxyBak[j - 1][2] = addrxy[j - 1][2];
  4092. addrxyBak[j - 1][3] = addrxy[j - 1][3];
  4093. }
  4094. else
  4095. {
  4096. addrxyBak[j][0] = addrxy[j][0];
  4097. addrxyBak[j][1] = addrxy[j][1];
  4098. addrxyBak[j][2] = addrxy[j][2];
  4099. addrxyBak[j][3] = addrxy[j][3];
  4100. }
  4101. }
  4102. }
  4103. addrxyBak[i][0] = addrxy[i][0];
  4104. addrxyBak[i][1] = addrxy[i][1];
  4105. addrxyBak[i][2] = addrxy[i][2];
  4106. addrxyBak[i][3] = addrxy[i][3];
  4107. }
  4108. for (int i = 0; i < indexstr - 1; i++)
  4109. {
  4110. if (abs(addrxy[i + 1][1] - addrxy[i][1]) <= 11)
  4111. {
  4112. //这里做交换操作
  4113. for (int m = i + 1; m > 0; m--)
  4114. {
  4115. if (abs(addrxy[m][1] - addrxy[m - 1][1]) <= 11)
  4116. {
  4117. if (addrxy[m][0] < addrxy[m - 1][0])
  4118. {
  4119. int tem[10 * 2] = { 0 };
  4120. ::memcpy(tem, addrxy[m - 1], 4 * 4);
  4121. ::memcpy(addrxy[m - 1], addrxy[m], 4 * 4);
  4122. ::memcpy(addrxy[m], tem, 4 * 4);
  4123. DWORD csTemp = Findstrings[m - 1];
  4124. Findstrings[m - 1] = Findstrings[m];
  4125. Findstrings[m] = csTemp;
  4126. bool bret = UnkownStr[m - 1];
  4127. UnkownStr[m - 1] = UnkownStr[m];
  4128. UnkownStr[m] = bret;
  4129. addrxyBak[m - 1][0] = addrxy[m - 1][0];
  4130. addrxyBak[m - 1][1] = addrxy[m - 1][1];
  4131. addrxyBak[m - 1][2] = addrxy[m - 1][2];
  4132. addrxyBak[m - 1][3] = addrxy[m - 1][3];
  4133. addrxyBak[m][0] = addrxy[m][0];
  4134. addrxyBak[m][1] = addrxy[m][1];
  4135. addrxyBak[m][2] = addrxy[m][2];
  4136. addrxyBak[m][3] = addrxy[m][3];
  4137. }
  4138. else
  4139. {
  4140. addrxyBak[m][0] = addrxy[m][0];
  4141. addrxyBak[m][1] = addrxy[m][1];
  4142. addrxyBak[m][2] = addrxy[m][2];
  4143. addrxyBak[m][3] = addrxy[m][3];
  4144. }
  4145. }
  4146. else
  4147. {
  4148. addrxyBak[m][0] = addrxy[m][0];
  4149. addrxyBak[m][1] = addrxy[m][1];
  4150. addrxyBak[m][2] = addrxy[m][2];
  4151. addrxyBak[m][3] = addrxy[m][3];
  4152. break;
  4153. }
  4154. }
  4155. }
  4156. }
  4157. if (indexstr >= 1)
  4158. {
  4159. addrxyBak[indexstr - 1][0] = addrxy[indexstr - 1][0];
  4160. addrxyBak[indexstr - 1][1] = addrxy[indexstr - 1][1];
  4161. addrxyBak[indexstr - 1][2] = addrxy[indexstr - 1][2];
  4162. addrxyBak[indexstr - 1][3] = addrxy[indexstr - 1][3];
  4163. }
  4164. DWORD Strings[MAX_PATH * 10] = { 0 };//记录字符串指针地址
  4165. DWORD nindex = 0;//记录组合字个数
  4166. DWORD nindexs[MAX_PATH * 10] = { 0 };//记录组合字的下标
  4167. for (int i = 0; i < indexstr; i++)
  4168. {
  4169. int len = strlen((char*)Findstrings[i]);
  4170. len = len / sizeof(wchar_t);
  4171. if (len > 1) //是组合字
  4172. {
  4173. Strings[nindex] = Findstrings[i];
  4174. nindexs[nindex] = i;
  4175. nindex++;
  4176. }
  4177. }
  4178. if (nindex > 0)
  4179. {
  4180. for (int i = 0; i < nindex; i++)
  4181. {
  4182. for (int j = 0; j < indexstr; j++)
  4183. {
  4184. if (nindexs[i] != j)
  4185. {
  4186. char* t1 = (char*)Strings[i];
  4187. char* t2 = (char*)Findstrings[j];
  4188. char* findchar = strstr((char*)Strings[i], (char*)Findstrings[j]);
  4189. if (findchar != NULL)
  4190. {
  4191. if (addrxyBak[nindexs[i]][0] <= addrxyBak[j][0] &&
  4192. (addrxyBak[nindexs[i]][0] + addrxyBak[nindexs[i]][2]) >= (addrxyBak[j][0] + addrxyBak[j][2]) &&
  4193. addrxyBak[nindexs[i]][1] == addrxyBak[j][1])
  4194. {
  4195. for (int n = 0; n < indexstr - j; n++)
  4196. {
  4197. addrxyBak[j + n][0] = addrxyBak[j + n + 1][0];
  4198. addrxyBak[j + n][1] = addrxyBak[j + n + 1][1];
  4199. addrxyBak[j + n][2] = addrxyBak[j + n + 1][2];
  4200. Findstrings[j + n] = Findstrings[j + n + 1];
  4201. }
  4202. indexstr--;
  4203. nindexs[i + 1]--;
  4204. }
  4205. }
  4206. }
  4207. }
  4208. }
  4209. }
  4210. //过滤字--2013-5-11//过滤字--2013-5-11//过滤字--2013-5-11//过滤字--2013-5-11//过滤字--2013-5-11
  4211. int nstrs = 0;
  4212. for (int i = 0; i < indexstr; i++)
  4213. {
  4214. int j = 1;
  4215. short hight = addrxyBak[i][3];
  4216. short wide = addrxyBak[i][2];
  4217. short Y = addrxyBak[i][1];
  4218. short X = addrxyBak[i][0];
  4219. bool iscontinuefind = false;
  4220. continuefind:
  4221. //char *t=(char *)Findstrings[i];
  4222. //char *tt=(char *)Findstrings[i+j];
  4223. if (addrxyBak[i + j][0] < (X + wide) && ((addrxyBak[i + j][1] - Y) < hight))
  4224. {
  4225. bool bfind = false;
  4226. for (int n = 0; n < indexstr - i; n++)
  4227. {
  4228. if (addrxyBak[i + j + n][2] > wide && bfind == false && UnkownStr[i + j + n] == false)
  4229. {
  4230. //char *t1=(char *)Findstrings[i+n];
  4231. //char *t2=(char *)Findstrings[i+j+n];
  4232. addrxyBak[i + n][0] = addrxyBak[i + j + n][0];
  4233. addrxyBak[i + n][1] = addrxyBak[i + j + n][1];
  4234. addrxyBak[i + n][2] = addrxyBak[i + j + n][2];
  4235. addrxyBak[i + n][3] = addrxyBak[i + j + n][3];
  4236. UnkownStr[i + n] = UnkownStr[i + j + n];
  4237. Findstrings[i + n] = Findstrings[i + j + n];
  4238. }
  4239. else
  4240. {
  4241. bfind = true;
  4242. //char *t1=(char *)Findstrings[i+j+n];
  4243. //char *t2=(char *)Findstrings[i+j+1+n];
  4244. addrxyBak[i + j + n][0] = addrxyBak[i + j + 1 + n][0];
  4245. addrxyBak[i + j + n][1] = addrxyBak[i + j + 1 + n][1];
  4246. addrxyBak[i + j + n][2] = addrxyBak[i + j + 1 + n][2];
  4247. addrxyBak[i + j + n][3] = addrxyBak[i + j + 1 + n][3];
  4248. UnkownStr[i + j + n] = UnkownStr[i + j + 1 + n];
  4249. Findstrings[i + j + n] = Findstrings[i + j + 1 + n];
  4250. }
  4251. }
  4252. nstrs++;
  4253. int dwnstr = indexstr - nstrs;
  4254. if (dwnstr == 0)
  4255. {
  4256. indexstr = i + 1;
  4257. break;
  4258. }
  4259. //char tempTetstr[MAX_PATH*100]={0};
  4260. //for(int k=0;k<dwnstr;k++)
  4261. //{
  4262. // ::strcat(tempTetstr,(char *)Findstrings[k]);
  4263. //}
  4264. iscontinuefind = true;
  4265. goto continuefind;
  4266. }
  4267. else if (!iscontinuefind)
  4268. nstrs++;
  4269. }
  4270. //过滤字--2013-5-11//过滤字--2013-5-11//过滤字--2013-5-11//过滤字--2013-5-11//过滤字--2013-5-11//过滤字--2013-5-11
  4271. //TSRuntime::add_log( "TS_OCR-排序-end");
  4272. char tempTet[MAX_PATH * 100] = { 0 };
  4273. for (int i = 0; i < indexstr; i++)
  4274. {
  4275. ::strcat(tempTet, (char*)Findstrings[i]);
  4276. }
  4277. //USES_CONVERSION;
  4278. ::wcscpy(retstr, A2W(tempTet));
  4279. //MessageBox(NULL,retstr,L"TS",NULL);
  4280. wchar_t* retstrnext = retstr;
  4281. int findJ = 0;
  4282. if (wcslen(retstr) > 0)
  4283. {
  4284. ////存储最先找到的字符串
  4285. int strfindlast = 0;
  4286. int strfind = 0;
  4287. short ret_x = -1;
  4288. short ret_y = -1;
  4289. short n_index = -1;
  4290. for (int j = 0; j < nstr; j++)
  4291. {
  4292. short len = wcslen(StrBuffer[j]);
  4293. short retx = -1;
  4294. short rety = -1;
  4295. short nindex = -1;
  4296. wchar_t* straddr = NULL;
  4297. int rlen = 0;
  4298. for (int i = 0; i < len; i++)
  4299. {
  4300. if (findJ != j)//重置状态
  4301. {
  4302. findJ = j;
  4303. retstrnext = retstr;
  4304. }
  4305. //在找到的字符中查找,是否存在
  4306. wchar_t findchar[2] = { 0 };
  4307. //wcsncpy(findchar,&StrBuffer[j][i],sizeof(wchar_t));
  4308. wcsncpy(findchar, &StrBuffer[j][i], 1);
  4309. if (i == 0)
  4310. straddr = wcsstr(retstrnext, findchar);
  4311. else if (straddr != NULL)
  4312. straddr = wcsstr(straddr, findchar);
  4313. if (straddr != NULL)
  4314. {
  4315. if (i == 0)
  4316. {
  4317. wchar_t* stringaddr = wcsstr(retstrnext, StrBuffer[j]);
  4318. if (stringaddr != NULL)
  4319. straddr = stringaddr;
  4320. strfind = (DWORD)straddr;
  4321. nindex = j;
  4322. retx = addrxyBak[((DWORD)strfind - (DWORD)retstr) / 2][0];
  4323. rety = addrxyBak[((DWORD)strfind - (DWORD)retstr) / 2][1];
  4324. if (retstring && type == 2)
  4325. {
  4326. swprintf(retstring, L"%s,%d,%d", StrBuffer[j], retx, rety);
  4327. }
  4328. if (retstring && type != 2)
  4329. {
  4330. rlen = wcslen(retstring);
  4331. bool issamex = false;
  4332. bool issamey = false;
  4333. if (retx != ret_x)
  4334. {
  4335. ret_x = retx;
  4336. issamex = true;
  4337. }
  4338. if (ret_y != rety)
  4339. {
  4340. ret_y = rety;
  4341. issamey = true;
  4342. }
  4343. if (n_index != nindex)
  4344. n_index = nindex;
  4345. else
  4346. {
  4347. if (!issamex && !issamey)
  4348. {
  4349. retstrnext++;
  4350. if (wcsstr(retstrnext, StrBuffer[j]) && rlen < (MAX_PATH * 100 - 16) && i == len - 1 && len != 1)//继续往后找,如果还找到存在,重置变量j,继续返回坐标
  4351. j--;
  4352. continue;
  4353. }
  4354. }
  4355. if (rlen > 1 && rlen < (MAX_PATH * 100 - 16) && type == 1)//FindStrFastExS
  4356. swprintf(retstring, L"%s|%s,%d,%d", retstring, StrBuffer[j], retx, rety);
  4357. else if (rlen == 0 && type == 1)//FindStrFastExS
  4358. swprintf(retstring, L"%s,%d,%d", StrBuffer[j], retx, rety);
  4359. else if (rlen > 1 && rlen < (MAX_PATH * 100 - 16))////FindStrFastEx
  4360. swprintf(retstring, L"%s|%d,%d,%d", retstring, nindex, retx, rety);
  4361. else if (rlen == 0)////FindStrFastEx
  4362. swprintf(retstring, L"%d,%d,%d", nindex, retx, rety);
  4363. retstrnext++;
  4364. }
  4365. }
  4366. }
  4367. else
  4368. {
  4369. nindex = -1;
  4370. retx = -1;
  4371. rety = -1;
  4372. break;
  4373. }
  4374. if (wcsstr(retstrnext, StrBuffer[j]) && rlen < (MAX_PATH * 100 - 16) && i == len - 1 && type != 2 && retstring)//继续往后找,如果还找到存在,重置变量j,继续返回坐标,&&len!=1
  4375. {
  4376. if (len == 1)
  4377. {
  4378. i--;
  4379. continue;
  4380. }
  4381. j--;
  4382. }
  4383. }
  4384. if (nindex != -1 && (strfindlast == 0 || (strfind < strfindlast && strfind != 0)))
  4385. {
  4386. strfindlast = strfind;
  4387. retIndex = nindex;
  4388. x = retx;
  4389. y = rety;
  4390. }
  4391. }
  4392. }
  4393. return retIndex;
  4394. }
  4395. int MyFindPictureClass::FindStr(PVOID pbuffer, HWND hwnd, int left, int top, int right, int bottom, wchar_t* strings, wchar_t* color, double simi, long& x, long& y, pMyDictInfo* MyDictCount, int* nMyDictCountsize, int NowUsingDictIndex, DWORD(*ColorDataInfo)[2000], wchar_t* retstring, int type)
  4396. {
  4397. wchar_t retstr[MAX_PATH * 100] = { 0 };
  4398. int index = 0;
  4399. int retIndex = -1;
  4400. bool isFind = false;
  4401. //得到当前字库的下标
  4402. usingindex = NowUsingDictIndex;
  4403. //得到当前字库个数
  4404. dictindex = nMyDictCountsize[usingindex];
  4405. if (dictindex <= 0) //字库个数==0就退出
  4406. return isFind;
  4407. //处理多个色偏
  4408. int col = 0;
  4409. wchar_t colorBuffer[MAX_PATH][16];//存储多个字符串
  4410. for (int i = 0; i < MAX_PATH; i++)
  4411. {
  4412. memset(colorBuffer[i], 0, 16);
  4413. }
  4414. short nstr = 0;//要查找色偏的个数 ,最多支持(MAX_PATH)个色偏
  4415. while (*color)
  4416. {
  4417. if (*color != L'|')
  4418. {
  4419. if (col > 13)
  4420. return retIndex;
  4421. colorBuffer[nstr][col++] = *color;
  4422. color++;
  4423. continue;
  4424. }
  4425. else
  4426. {
  4427. if (col < 1)
  4428. {
  4429. color++;
  4430. continue;
  4431. }
  4432. nstr++;
  4433. col = 0;
  4434. color++;
  4435. }
  4436. }
  4437. if (col >= 1)
  4438. nstr++;
  4439. ncolors = nstr;//记录下色偏个数
  4440. //int t=::GetTickCount();
  4441. for (int i = 0; i < nstr; i++)
  4442. {
  4443. WCHAR colorR[4] = { 0 };//字的颜色
  4444. WCHAR colorG[4] = { 0 };//字的颜色
  4445. WCHAR colorB[4] = { 0 };//字的颜色
  4446. WCHAR coloroffR[4] = { 0 };//偏色
  4447. WCHAR coloroffG[4] = { 0 };//偏色
  4448. WCHAR coloroffB[4] = { 0 };//偏色
  4449. int count = 0;
  4450. int colorL = 0;
  4451. //StrTrimW(colorBuffer[i],L" ");//移除前后空格符
  4452. while (colorBuffer[i][colorL] != L'-')
  4453. {
  4454. if (count > 6) //判断是否是非法颜色值
  4455. return retIndex;
  4456. if (count < 2)
  4457. colorR[count] = colorBuffer[i][colorL++];
  4458. else if (count > 1 && count < 4)
  4459. colorG[count - 2] = colorBuffer[i][colorL++];
  4460. else if (count > 3)
  4461. colorB[count - 4] = colorBuffer[i][colorL++];
  4462. count++;
  4463. }
  4464. //将传入的RGB转换为BGR
  4465. WCHAR colorBGR[16] = { 0 };//字的颜色
  4466. swprintf(colorBGR, L"%s%s%s", colorB, colorG, colorR);
  4467. count = 0;
  4468. colorL++;
  4469. while (colorBuffer[colorL] != L'\0')
  4470. {
  4471. if (count >= 6) //判断是否是非法色偏值
  4472. break;
  4473. if (count < 2)
  4474. coloroffR[count] = colorBuffer[i][colorL++];
  4475. else if (count > 1 && count < 4)
  4476. coloroffG[count - 2] = colorBuffer[i][colorL++];
  4477. else if (count > 3)
  4478. coloroffB[count - 4] = colorBuffer[i][colorL++];
  4479. count++;
  4480. }
  4481. //将传入的RGB转换为BGR
  4482. WCHAR coloroffBGR[16] = { 0 };//偏色
  4483. swprintf(coloroffBGR, L"%s%s%s", coloroffB, coloroffG, coloroffR);
  4484. strColors[i] = wcstol(colorBGR, NULL, 16);//整形颜色值
  4485. strColoroff[i] = wcstol(coloroffBGR, NULL, 16);//整形偏色值
  4486. m_colorOffR[i] = GetRValue(strColoroff[i]);
  4487. m_colorOffG[i] = GetGValue(strColoroff[i]);
  4488. m_colorOffB[i] = GetBValue(strColoroff[i]);
  4489. }
  4490. if (m_parenthwnd != 0 && m_parenthwnd != ::GetDesktopWindow())//normol找图
  4491. {
  4492. RECT rec;
  4493. RECT clientrec;
  4494. ::GetClientRect(m_parenthwnd, &clientrec);
  4495. int clienthight = clientrec.bottom - clientrec.top;
  4496. int clientwide = clientrec.right - clientrec.left;
  4497. ::GetWindowRect(m_parenthwnd, &rec);
  4498. POINT point;
  4499. point.x = 0;
  4500. point.y = 0;
  4501. ::ClientToScreen(m_parenthwnd, &point);
  4502. m_Left = point.x;
  4503. m_Top = point.y;
  4504. m_Right = rec.right;
  4505. m_bottom = rec.bottom;
  4506. if (left < 0)
  4507. left = 0;
  4508. if (left >= clientwide)
  4509. left = clientwide - 1;
  4510. if (top < 0)
  4511. top = 0;
  4512. if (top >= clienthight)
  4513. top = clienthight - 1;
  4514. if (right >= clientwide)
  4515. right = clientwide - 1;
  4516. if (bottom >= clienthight)
  4517. bottom = clienthight - 1;
  4518. if (m_Left < 0)
  4519. {
  4520. if ((left + m_Left) <= 0)//越界
  4521. left = 0;
  4522. else
  4523. left = m_Left + left;
  4524. }
  4525. else
  4526. left = m_Left + left;
  4527. if (m_Top >= 0)
  4528. top = m_Top + top;
  4529. if (m_Right >= ::GetSystemMetrics(SM_CXSCREEN))
  4530. {
  4531. if ((right + m_Left) > ::GetSystemMetrics(SM_CXSCREEN))
  4532. right = ::GetSystemMetrics(SM_CXSCREEN) - 1;
  4533. else
  4534. right = right + m_Left;
  4535. }
  4536. else
  4537. right = right + m_Left;
  4538. if (m_bottom >= ::GetSystemMetrics(SM_CYSCREEN))
  4539. {
  4540. if ((bottom + m_Top) >= ::GetSystemMetrics(SM_CYSCREEN))
  4541. bottom = ::GetSystemMetrics(SM_CYSCREEN) - 1;
  4542. else
  4543. bottom = bottom + m_Top;
  4544. }
  4545. else
  4546. bottom = bottom + m_Top;
  4547. m_Left = left;
  4548. m_Top = top;
  4549. m_Right = right;
  4550. m_bottom = bottom;
  4551. }
  4552. else
  4553. {
  4554. RECT clientrec;
  4555. if (ColorDataInfo != NULL)
  4556. ::GetClientRect(m_hwnd, &clientrec);
  4557. else//gdi
  4558. ::GetClientRect(hwnd, &clientrec);
  4559. int clienthight = clientrec.bottom - clientrec.top;
  4560. int clientwide = clientrec.right - clientrec.left;
  4561. m_Left = left;
  4562. m_Top = top;
  4563. m_Right = right;
  4564. m_bottom = bottom;
  4565. if (left < 0)
  4566. m_Left = left = 0;
  4567. if (left >= clientwide)
  4568. m_Left = clientwide - 1;
  4569. if (top < 0)
  4570. m_Top = top = 0;
  4571. if (top >= clienthight)
  4572. m_Top = clienthight - 1;
  4573. if (right >= clientwide)
  4574. m_Right = clientwide - 1;
  4575. if (bottom >= clienthight)
  4576. m_bottom = clienthight - 1;
  4577. }
  4578. RECT rc;
  4579. rc.bottom = m_bottom;
  4580. rc.left = m_Left;
  4581. rc.right = m_Right;
  4582. rc.top = m_Top;
  4583. if (ColorDataInfo != NULL)
  4584. {
  4585. wndWidth = m_Right - m_Left;
  4586. wndHeight = m_bottom - m_Top;
  4587. //DX模式找字
  4588. if (!getDXBitmap(pbuffer, ColorDataInfo, 1))
  4589. {
  4590. return retIndex;
  4591. }
  4592. }
  4593. else
  4594. {
  4595. ////GDI模式找字
  4596. if (!getGDIBitmap(hwnd, rc, 1))
  4597. {
  4598. return retIndex;
  4599. }
  4600. }
  4601. GetStringInfo();
  4602. //处理查找多个字
  4603. col = 0;
  4604. wchar_t StrBuffer[MAX_PATH][MAX_PATH];//存储多个字符串
  4605. for (int i = 0; i < MAX_PATH; i++)
  4606. {
  4607. memset(StrBuffer[i], 0, MAX_PATH);
  4608. }
  4609. nstr = 0;//要查找字的个数 ,最多支持(MAX_PATH)个字
  4610. while (*strings)
  4611. {
  4612. if (*strings != L'|')
  4613. {
  4614. StrBuffer[nstr][col++] = *strings;
  4615. strings++;
  4616. continue;
  4617. }
  4618. else
  4619. {
  4620. if (col < 1)
  4621. {
  4622. strings++;
  4623. continue;
  4624. }
  4625. nstr++;
  4626. col = 0;
  4627. strings++;
  4628. }
  4629. }
  4630. if (col >= 1)
  4631. nstr++;
  4632. //处理查找多个字
  4633. DWORD Findstrings[MAX_PATH * 10] = { 0 };
  4634. bool UnkownStr[MAX_PATH * 10] = { 0 };//未知字体
  4635. /////////////////// 处理多张图 /////////////////////
  4636. int indexstr = 0;
  4637. int findstrX = -1;
  4638. int findstrY = -1;
  4639. int X = 0;
  4640. int Y = 0;
  4641. x = -1;
  4642. y = -1;
  4643. for (int i = 0; i < dictindex; i++)
  4644. {
  4645. if (simi == 1.0)
  4646. {
  4647. //使用指定字库
  4648. //int nstrvaluelen=pWndBmpStr[TSRuntime::MyDictCount[usingindex][i].nstrvalue[0]].size();
  4649. int nstrvaluelen = pWndBmpStr[MyDictCount[usingindex][i].nstrvalue[0]].size();
  4650. for (int n = 0; n < nstrvaluelen; n++)
  4651. {
  4652. findstrX = pWndBmpStr[MyDictCount[usingindex][i].nstrvalue[0]].at(n).x;
  4653. findstrY = pWndBmpStr[MyDictCount[usingindex][i].nstrvalue[0]].at(n).y;
  4654. if (findstrX == -1 || findstrY == -1)
  4655. continue;
  4656. if (comparaStr(MyDictCount[usingindex][i].nstrvalue, MyDictCount[usingindex][i].strwide, MyDictCount[usingindex][i].strhight, X, Y, findstrX, findstrY, simi, MyDictCount[usingindex][i].nstrcount))
  4657. {
  4658. int swide = MyDictCount[usingindex][i].strwide;
  4659. int shight = MyDictCount[usingindex][i].strhight;
  4660. for (int k = 0; k <= swide; k++)//判断是否是非正规字体
  4661. {
  4662. if ((pWndBmpStrxy[Y - 1][X - 1] != 0 && pWndBmpStrxy[Y - 1][X - 1] >= 16) && shight >= 11)//左上角
  4663. {
  4664. UnkownStr[indexstr] = true;//非正规字体标识
  4665. break;
  4666. }
  4667. if (pWndBmpStrxy[Y - 1][X + k] != 0 && pWndBmpStrxy[Y - 1][X + k] >= 1024)
  4668. {
  4669. UnkownStr[indexstr] = true;//非正规字体标识
  4670. break;
  4671. }
  4672. }
  4673. //bool UnderlineString=true;//下滑线字体
  4674. //bool bfind=false;
  4675. //for(int k=0;k<=swide;k++)
  4676. //{
  4677. // if((pWndBmpStrxy[Y][X+k]%2==0)||pWndBmpStrxy[Y][X+k]==0||swide<2||shight>11)//判断字体最后一行是否是下划线
  4678. // UnderlineString=false;
  4679. // //if(shight<=2&&pWndBmpStrxy[Y-4][X+k]!=0&&swide>=10) //区分三和一字的重合问题
  4680. // // bfind=true;
  4681. // if(swide>=14&&shight==1&&pWndBmpStrxy[Y-2][X+k]!=0)//判断区别处理字体13号字体的点阵例如:"一"和"查"
  4682. // bfind=true;
  4683. // if(pWndBmpStrxy[Y-1][X+k]!=0&&pWndBmpStrxy[Y-1][X+k]>=1024)
  4684. // bfind=true;
  4685. // else if((pWndBmpStrxy[Y-1][X+swide]!=0&&pWndBmpStrxy[Y-1][X+swide]>=16)&&shight>=11) //右上角
  4686. // bfind=true;
  4687. // else if((pWndBmpStrxy[Y-1][X-1]!=0&&pWndBmpStrxy[Y-1][X-1]>=16)&&shight>=11)//左上角
  4688. // bfind=true;
  4689. // if((pWndBmpStrxy[Y][X-1]!=0||pWndBmpStrxy[Y][X+swide]!=0)&&shight<5)
  4690. // bfind=true;
  4691. // if(swide==1&&(pWndBmpStrxy[Y-1][X-1]!=0||pWndBmpStrxy[Y-1][X+swide]!=0)) //处理特殊符号
  4692. // bfind=true;
  4693. // if(bfind)
  4694. // break;
  4695. //}
  4696. //if(bfind)
  4697. //{
  4698. // if(UnderlineString==false)
  4699. // continue;
  4700. //}
  4701. if (m_parenthwnd != 0 && m_parenthwnd != ::GetDesktopWindow())//normol模式
  4702. {
  4703. POINT pt;
  4704. pt.x = X + m_Left;
  4705. pt.y = Y + m_Top;
  4706. ::ScreenToClient(m_parenthwnd, &pt);
  4707. addrxy[indexstr][0] = pt.x;
  4708. addrxy[indexstr][1] = pt.y;
  4709. }
  4710. else
  4711. {
  4712. addrxy[indexstr][0] = X + m_Left;
  4713. addrxy[indexstr][1] = Y + m_Top;
  4714. }
  4715. addrxy[indexstr][2] = swide;
  4716. addrxy[indexstr][3] = shight;
  4717. USES_CONVERSION;
  4718. ///////取背景图 点阵坐标
  4719. Findstrings[indexstr] = (DWORD)(MyDictCount[usingindex][i].mydistchar);
  4720. ////重置背景图坐标,防止找重复图
  4721. //pWndBmpStr[MyDictCount[usingindex][i].nstrvalue[0]].at(n).x=-1;
  4722. //pWndBmpStr[MyDictCount[usingindex][i].nstrvalue[0]].at(n).y=-1;
  4723. indexstr++;
  4724. }
  4725. if (indexstr >= MAX_PATH * 10 - 1)
  4726. break;
  4727. }
  4728. if (indexstr >= MAX_PATH * 10 - 1)
  4729. break;
  4730. }
  4731. else if (simi > 0.1 && simi < 1.0)
  4732. {
  4733. //int nstrwide=TSRuntime::MyDictCount[usingindex][i].strwide;
  4734. int nstrwide = MyDictCount[usingindex][i].strwide;
  4735. //COLORREF pLoadstrBuffer[11][MAX_PATH-10];
  4736. for (int a = 0; a < 11; a++)
  4737. {
  4738. memset(TSRuntime::pLoadstrBuffer[a], 0, nstrwide + 1);
  4739. }
  4740. //将点阵信息还原图片矩阵信息
  4741. //byteTopic(TSRuntime::MyDictCount[usingindex][i].nstrvalue,nstrwide);
  4742. byteTopic(MyDictCount[usingindex][i].nstrvalue, nstrwide);
  4743. for (int j = 1; j < 2048; j++)
  4744. {
  4745. //使用指定字库
  4746. int nstrvaluelen = pWndBmpStr[j].size();
  4747. for (int n = 0; n < nstrvaluelen; n++)
  4748. {
  4749. findstrX = pWndBmpStr[j].at(n).x;
  4750. findstrY = pWndBmpStr[j].at(n).y;
  4751. if (findstrX == -1 || findstrY == -1)
  4752. continue;
  4753. //if(comparaStrTosim(nstrwide,TSRuntime::MyDictCount[usingindex][i].strhight,X,Y,findstrX,findstrY,simi,TSRuntime::MyDictCount[usingindex][i].nstrcount))
  4754. if (comparaStrTosim(nstrwide, MyDictCount[usingindex][i].strhight, X, Y, findstrX, findstrY, simi, MyDictCount[usingindex][i].nstrcount))
  4755. {
  4756. int swide = MyDictCount[usingindex][i].strwide;
  4757. int shight = MyDictCount[usingindex][i].strhight;
  4758. for (int k = 0; k <= swide; k++)//判断是否是非正规字体
  4759. {
  4760. if ((pWndBmpStrxy[Y - 1][X - 1] != 0 && pWndBmpStrxy[Y - 1][X - 1] >= 16) && shight >= 11)//左上角
  4761. {
  4762. UnkownStr[indexstr] = true;//非正规字体标识
  4763. break;
  4764. }
  4765. if (pWndBmpStrxy[Y - 1][X + k] != 0 && pWndBmpStrxy[Y - 1][X + k] >= 1024)
  4766. {
  4767. UnkownStr[indexstr] = true;//非正规字体标识
  4768. break;
  4769. }
  4770. }
  4771. //USES_CONVERSION;
  4772. ////wchar_t *finds=wcsstr(retstr,A2W(TSRuntime::MyDictCount[usingindex][i].mydistchar));
  4773. //wchar_t *finds=wcsstr(retstr,A2W(MyDictCount[usingindex][i].mydistchar));
  4774. //if(finds!=NULL)
  4775. //{
  4776. // bool bfind=false;
  4777. // for(int k=0;k<indexstr;k++)
  4778. // {
  4779. // //if(abs(addrxy[k][0]-X)<=TSRuntime::MyDictCount[usingindex][i].strwide&&abs(addrxy[k][1]-Y)<=TSRuntime::MyDictCount[usingindex][i].strhight)
  4780. // if(abs(addrxy[k][0]-X)<=MyDictCount[usingindex][i].strwide&&abs(addrxy[k][1]-Y)<=MyDictCount[usingindex][i].strhight)
  4781. // {
  4782. // bfind=true;
  4783. // break;
  4784. // }
  4785. // }
  4786. // if(bfind)
  4787. // continue;
  4788. //}
  4789. if (m_parenthwnd != 0 && m_parenthwnd != ::GetDesktopWindow())//normol模式
  4790. {
  4791. POINT pt;
  4792. pt.x = X + m_Left;
  4793. pt.y = Y + m_Top;
  4794. ::ScreenToClient(m_parenthwnd, &pt);
  4795. addrxy[indexstr][0] = pt.x;
  4796. addrxy[indexstr][1] = pt.y;
  4797. }
  4798. else
  4799. {
  4800. addrxy[indexstr][0] = X + m_Left;
  4801. addrxy[indexstr][1] = Y + m_Top;
  4802. }
  4803. addrxy[indexstr][2] = swide;
  4804. addrxy[indexstr][3] = shight;
  4805. ///////取背景图 点阵坐标
  4806. Findstrings[indexstr] = (DWORD)(MyDictCount[usingindex][i].mydistchar);
  4807. ////重置背景图坐标,防止找重复图
  4808. //pWndBmpStr[j].at(n).x=-1;
  4809. //pWndBmpStr[j].at(n).y=-1;
  4810. indexstr++;
  4811. }
  4812. if (indexstr >= MAX_PATH * 10 - 1)
  4813. break;
  4814. }
  4815. if (indexstr >= MAX_PATH * 10 - 1)
  4816. break;
  4817. }
  4818. }
  4819. }
  4820. ////TSRuntime::add_log( "TS_OCR-排序-start");
  4821. int addrxyBak[MAX_PATH * 10][4] = { 0 };
  4822. wchar_t tempfindstr[256] = { 0 };
  4823. wchar_t tempfindstr1[256] = { 0 };
  4824. for (int i = 0; i < indexstr - 1; i++)
  4825. {
  4826. retstr[i];
  4827. retstr[i + 1];
  4828. if (addrxy[i + 1][1] < addrxy[i][1])
  4829. {
  4830. for (int j = i + 1; j > 0; j--)
  4831. {
  4832. if (addrxy[j][1] < addrxy[j - 1][1])
  4833. {
  4834. //这里做交换操作
  4835. int tem[10 * 2] = { 0 };
  4836. ::memcpy(tem, addrxy[j - 1], 4 * 4);
  4837. ::memcpy(addrxy[j - 1], addrxy[j], 4 * 4);
  4838. ::memcpy(addrxy[j], tem, 4 * 4);
  4839. DWORD csTemp = Findstrings[j - 1];
  4840. Findstrings[j - 1] = Findstrings[j];
  4841. Findstrings[j] = csTemp;
  4842. bool bret = UnkownStr[j - 1];
  4843. UnkownStr[j - 1] = UnkownStr[j];
  4844. UnkownStr[j] = bret;
  4845. addrxyBak[j - 1][0] = addrxy[j - 1][0];
  4846. addrxyBak[j - 1][1] = addrxy[j - 1][1];
  4847. addrxyBak[j - 1][2] = addrxy[j - 1][2];
  4848. addrxyBak[j - 1][3] = addrxy[j - 1][3];
  4849. }
  4850. else
  4851. {
  4852. addrxyBak[j][0] = addrxy[j][0];
  4853. addrxyBak[j][1] = addrxy[j][1];
  4854. addrxyBak[j][2] = addrxy[j][2];
  4855. addrxyBak[j][3] = addrxy[j][3];
  4856. }
  4857. }
  4858. }
  4859. addrxyBak[i][0] = addrxy[i][0];
  4860. addrxyBak[i][1] = addrxy[i][1];
  4861. addrxyBak[i][2] = addrxy[i][2];
  4862. addrxyBak[i][3] = addrxy[i][3];
  4863. }
  4864. for (int i = 0; i < indexstr - 1; i++)
  4865. {
  4866. if (abs(addrxy[i + 1][1] - addrxy[i][1]) <= 11)
  4867. {
  4868. //这里做交换操作
  4869. for (int m = i + 1; m > 0; m--)
  4870. {
  4871. if (abs(addrxy[m][1] - addrxy[m - 1][1]) <= 11)
  4872. {
  4873. if (addrxy[m][0] < addrxy[m - 1][0])
  4874. {
  4875. int tem[10 * 2] = { 0 };
  4876. ::memcpy(tem, addrxy[m - 1], 4 * 4);
  4877. ::memcpy(addrxy[m - 1], addrxy[m], 4 * 4);
  4878. ::memcpy(addrxy[m], tem, 4 * 4);
  4879. DWORD csTemp = Findstrings[m - 1];
  4880. Findstrings[m - 1] = Findstrings[m];
  4881. Findstrings[m] = csTemp;
  4882. bool bret = UnkownStr[m - 1];
  4883. UnkownStr[m - 1] = UnkownStr[m];
  4884. UnkownStr[m] = bret;
  4885. addrxyBak[m - 1][0] = addrxy[m - 1][0];
  4886. addrxyBak[m - 1][1] = addrxy[m - 1][1];
  4887. addrxyBak[m - 1][2] = addrxy[m - 1][2];
  4888. addrxyBak[m - 1][3] = addrxy[m - 1][3];
  4889. addrxyBak[m][0] = addrxy[m][0];
  4890. addrxyBak[m][1] = addrxy[m][1];
  4891. addrxyBak[m][2] = addrxy[m][2];
  4892. addrxyBak[m][3] = addrxy[m][3];
  4893. }
  4894. else
  4895. {
  4896. addrxyBak[m][0] = addrxy[m][0];
  4897. addrxyBak[m][1] = addrxy[m][1];
  4898. addrxyBak[m][2] = addrxy[m][2];
  4899. addrxyBak[m][3] = addrxy[m][3];
  4900. }
  4901. }
  4902. else
  4903. {
  4904. addrxyBak[m][0] = addrxy[m][0];
  4905. addrxyBak[m][1] = addrxy[m][1];
  4906. addrxyBak[m][2] = addrxy[m][2];
  4907. addrxyBak[m][3] = addrxy[m][3];
  4908. break;
  4909. }
  4910. }
  4911. }
  4912. }
  4913. if (indexstr >= 1)
  4914. {
  4915. addrxyBak[indexstr - 1][0] = addrxy[indexstr - 1][0];
  4916. addrxyBak[indexstr - 1][1] = addrxy[indexstr - 1][1];
  4917. addrxyBak[indexstr - 1][2] = addrxy[indexstr - 1][2];
  4918. addrxyBak[indexstr - 1][3] = addrxy[indexstr - 1][3];
  4919. }
  4920. DWORD Strings[MAX_PATH * 10] = { 0 };//记录字符串指针地址
  4921. DWORD nindex = 0;//记录组合字个数
  4922. DWORD nindexs[MAX_PATH * 10] = { 0 };//记录组合字的下标
  4923. for (int i = 0; i < indexstr; i++)
  4924. {
  4925. int len = strlen((char*)Findstrings[i]);
  4926. len = len / sizeof(wchar_t);
  4927. if (len > 1) //是组合字
  4928. {
  4929. Strings[nindex] = Findstrings[i];
  4930. nindexs[nindex] = i;
  4931. nindex++;
  4932. }
  4933. }
  4934. if (nindex > 0)
  4935. {
  4936. for (int i = 0; i < nindex; i++)
  4937. {
  4938. for (int j = 0; j < indexstr; j++)
  4939. {
  4940. if (nindexs[i] != j)
  4941. {
  4942. char* t1 = (char*)Strings[i];
  4943. char* t2 = (char*)Findstrings[j];
  4944. char* findchar = strstr((char*)Strings[i], (char*)Findstrings[j]);
  4945. if (findchar != NULL)
  4946. {
  4947. if (addrxyBak[nindexs[i]][0] <= addrxyBak[j][0] &&
  4948. (addrxyBak[nindexs[i]][0] + addrxyBak[nindexs[i]][2]) >= (addrxyBak[j][0] + addrxyBak[j][2]) &&
  4949. addrxyBak[nindexs[i]][1] == addrxyBak[j][1])
  4950. {
  4951. for (int n = 0; n < indexstr - j; n++)
  4952. {
  4953. addrxyBak[j + n][0] = addrxyBak[j + n + 1][0];
  4954. addrxyBak[j + n][1] = addrxyBak[j + n + 1][1];
  4955. addrxyBak[j + n][2] = addrxyBak[j + n + 1][2];
  4956. Findstrings[j + n] = Findstrings[j + n + 1];
  4957. }
  4958. indexstr--;
  4959. nindexs[i + 1]--;
  4960. }
  4961. }
  4962. }
  4963. }
  4964. }
  4965. }
  4966. //过滤字--2013-5-11//过滤字--2013-5-11//过滤字--2013-5-11//过滤字--2013-5-11//过滤字--2013-5-11
  4967. int nstrs = 0;
  4968. for (int i = 0; i < indexstr; i++)
  4969. {
  4970. int j = 1;
  4971. short hight = addrxyBak[i][3];
  4972. short wide = addrxyBak[i][2];
  4973. short Y = addrxyBak[i][1];
  4974. short X = addrxyBak[i][0];
  4975. bool iscontinuefind = false;
  4976. continuefind:
  4977. //char *t=(char *)Findstrings[i];
  4978. //char *tt=(char *)Findstrings[i+j];
  4979. if (addrxyBak[i + j][0] < (X + wide) && ((addrxyBak[i + j][1] - Y) < hight))
  4980. {
  4981. bool bfind = false;
  4982. for (int n = 0; n < indexstr - i; n++)
  4983. {
  4984. if (addrxyBak[i + j + n][2] > wide && bfind == false && UnkownStr[i + j + n] == false)
  4985. {
  4986. //char *t1=(char *)Findstrings[i+n];
  4987. //char *t2=(char *)Findstrings[i+j+n];
  4988. addrxyBak[i + n][0] = addrxyBak[i + j + n][0];
  4989. addrxyBak[i + n][1] = addrxyBak[i + j + n][1];
  4990. addrxyBak[i + n][2] = addrxyBak[i + j + n][2];
  4991. addrxyBak[i + n][3] = addrxyBak[i + j + n][3];
  4992. UnkownStr[i + n] = UnkownStr[i + j + n];
  4993. Findstrings[i + n] = Findstrings[i + j + n];
  4994. }
  4995. else
  4996. {
  4997. bfind = true;
  4998. //char *t1=(char *)Findstrings[i+j+n];
  4999. //char *t2=(char *)Findstrings[i+j+1+n];
  5000. addrxyBak[i + j + n][0] = addrxyBak[i + j + 1 + n][0];
  5001. addrxyBak[i + j + n][1] = addrxyBak[i + j + 1 + n][1];
  5002. addrxyBak[i + j + n][2] = addrxyBak[i + j + 1 + n][2];
  5003. addrxyBak[i + j + n][3] = addrxyBak[i + j + 1 + n][3];
  5004. UnkownStr[i + j + n] = UnkownStr[i + j + 1 + n];
  5005. Findstrings[i + j + n] = Findstrings[i + j + 1 + n];
  5006. }
  5007. }
  5008. nstrs++;
  5009. int dwnstr = indexstr - nstrs;
  5010. if (dwnstr == 0)
  5011. {
  5012. indexstr = i + 1;
  5013. break;
  5014. }
  5015. //char tempTetstr[MAX_PATH*100]={0};
  5016. //for(int k=0;k<dwnstr;k++)
  5017. //{
  5018. // ::strcat(tempTetstr,(char *)Findstrings[k]);
  5019. //}
  5020. iscontinuefind = true;
  5021. goto continuefind;
  5022. }
  5023. else if (!iscontinuefind)
  5024. nstrs++;
  5025. }
  5026. //过滤字--2013-5-11//过滤字--2013-5-11//过滤字--2013-5-11//过滤字--2013-5-11//过滤字--2013-5-11//过滤字--2013-5-11
  5027. //TSRuntime::add_log( "TS_OCR-排序-end");
  5028. USES_CONVERSION;
  5029. char tempTet[MAX_PATH * 100] = { 0 };
  5030. for (int i = 0; i < indexstr; i++)
  5031. {
  5032. ::strcat(tempTet, (char*)Findstrings[i]);
  5033. }
  5034. ::wcscpy(retstr, A2W(tempTet));
  5035. //MessageBox(NULL,retstr,L"TS",NULL);
  5036. int strfind = 0;//存储最先找到的字符串
  5037. wchar_t* straddr = NULL;
  5038. for (int j = 0; j < nstr; j++)
  5039. {
  5040. //在找到的字符中查找,是否存在
  5041. if (straddr == NULL)
  5042. straddr = wcsstr(retstr, StrBuffer[j]);
  5043. else
  5044. straddr = wcsstr(straddr, StrBuffer[j]);
  5045. if (straddr != NULL)
  5046. {
  5047. if (strfind == 0 || strfind > ((DWORD)straddr))
  5048. {
  5049. strfind = (DWORD)straddr;
  5050. retIndex = j;
  5051. x = addrxyBak[((DWORD)straddr - (DWORD)retstr) / 2][0];
  5052. y = addrxyBak[((DWORD)straddr - (DWORD)retstr) / 2][1];
  5053. if (type == 2 && retstring)//FindStrS
  5054. {
  5055. swprintf(retstring, L"%s,%d,%d", StrBuffer[j], x, y);
  5056. }
  5057. }
  5058. if (retstring && type != 2)
  5059. {
  5060. retIndex = j;
  5061. x = addrxyBak[((DWORD)straddr - (DWORD)retstr) / 2][0];
  5062. y = addrxyBak[((DWORD)straddr - (DWORD)retstr) / 2][1];
  5063. int len = wcslen(retstring);
  5064. if (len > 1 && len < (MAX_PATH * 100 - 16) && type == 1)//FindStrExS
  5065. swprintf(retstring, L"%s|%s,%d,%d", retstring, StrBuffer[j], x, y);
  5066. else if (len == 0 && type == 1)//FindStrExS
  5067. swprintf(retstring, L"%s,%d,%d", StrBuffer[j], x, y);
  5068. else if (len > 1 && len < (MAX_PATH * 100 - 16)) //FindStrEx
  5069. swprintf(retstring, L"%s|%d,%d,%d", retstring, retIndex, x, y);
  5070. else if (len == 0)//FindStrEx
  5071. swprintf(retstring, L"%d,%d,%d", retIndex, x, y);
  5072. straddr++;
  5073. if (wcsstr(straddr, StrBuffer[j]) && len < (MAX_PATH * 100 - 16))//继续往后找,如果还找到存在,重置变量j,继续返回坐标
  5074. j--;
  5075. else
  5076. straddr = NULL;
  5077. }
  5078. }
  5079. }
  5080. if (strfind == 0)
  5081. {
  5082. retIndex = -1;
  5083. x = -1;
  5084. y = -1;
  5085. }
  5086. return retIndex;
  5087. }
  5088. //相似度匹配
  5089. bool MyFindPictureClass::comparaStrTosim(int strwide, int strhight, int& RetX, int& RetY, int startX, int startY, float simi, int nstrcount)
  5090. //bool MyFindPictureClass::comparaStrTosim(COLORREF pLoadstrBuffer[11][MAX_PATH-10],int strwide,int strhight,int &RetX,int &RetY,int startX,int startY,float simi,int nstrcount)
  5091. {
  5092. return true;
  5093. }
  5094. int MyFindPictureClass::ocr(PVOID pbuffer, HWND hwnd, int left, int top, int right, int bottom, wchar_t* color, double simi, wchar_t* retstr, pMyDictInfo* MyDictCount, int* nMyDictCountsize, int NowUsingDictIndex, DWORD(*ColorDataInfo)[2000], int type)
  5095. {
  5096. //TSRuntime::add_log( "TS_OCR-start");
  5097. int index = 0;
  5098. int retIndex = -1;
  5099. bool isFind = false;
  5100. //得到当前字库的下标
  5101. usingindex = NowUsingDictIndex;
  5102. //得到当前字库个数
  5103. dictindex = nMyDictCountsize[usingindex];
  5104. if (dictindex <= 0) //字库个数==0就退出
  5105. return isFind;
  5106. //处理多个色偏
  5107. int col = 0;
  5108. wchar_t colorBuffer[MAX_PATH][16];//存储多个字符串
  5109. for (int i = 0; i < MAX_PATH; i++)
  5110. {
  5111. memset(colorBuffer[i], 0, 16);
  5112. }
  5113. short nstr = 0;//要查找色偏的个数 ,最多支持(MAX_PATH)个色偏
  5114. while (*color)
  5115. {
  5116. if (*color != L'|')
  5117. {
  5118. if (col > 13)
  5119. return retIndex;
  5120. colorBuffer[nstr][col++] = *color;
  5121. color++;
  5122. continue;
  5123. }
  5124. else
  5125. {
  5126. if (col < 1)
  5127. {
  5128. color++;
  5129. continue;
  5130. }
  5131. nstr++;
  5132. col = 0;
  5133. color++;
  5134. }
  5135. }
  5136. if (col >= 1)
  5137. nstr++;
  5138. ncolors = nstr;//记录下色偏个数
  5139. //int t=::GetTickCount();
  5140. for (int i = 0; i < nstr; i++)
  5141. {
  5142. WCHAR colorR[4] = { 0 };//字的颜色
  5143. WCHAR colorG[4] = { 0 };//字的颜色
  5144. WCHAR colorB[4] = { 0 };//字的颜色
  5145. WCHAR coloroffR[4] = { 0 };//偏色
  5146. WCHAR coloroffG[4] = { 0 };//偏色
  5147. WCHAR coloroffB[4] = { 0 };//偏色
  5148. int count = 0;
  5149. int colorL = 0;
  5150. //StrTrimW(colorBuffer[i],L" ");//移除前后空格符
  5151. while (colorBuffer[i][colorL] != L'-')
  5152. {
  5153. if (count > 6) //判断是否是非法颜色值
  5154. return retIndex;
  5155. if (count < 2)
  5156. colorR[count] = colorBuffer[i][colorL++];
  5157. else if (count > 1 && count < 4)
  5158. colorG[count - 2] = colorBuffer[i][colorL++];
  5159. else if (count > 3)
  5160. colorB[count - 4] = colorBuffer[i][colorL++];
  5161. count++;
  5162. }
  5163. //将传入的RGB转换为BGR
  5164. WCHAR colorBGR[16] = { 0 };//字的颜色
  5165. swprintf(colorBGR, L"%s%s%s", colorB, colorG, colorR);
  5166. count = 0;
  5167. colorL++;
  5168. while (colorBuffer[colorL] != L'\0')
  5169. {
  5170. if (count >= 6) //判断是否是非法色偏值
  5171. break;
  5172. if (count < 2)
  5173. coloroffR[count] = colorBuffer[i][colorL++];
  5174. else if (count > 1 && count < 4)
  5175. coloroffG[count - 2] = colorBuffer[i][colorL++];
  5176. else if (count > 3)
  5177. coloroffB[count - 4] = colorBuffer[i][colorL++];
  5178. count++;
  5179. }
  5180. //将传入的RGB转换为BGR
  5181. WCHAR coloroffBGR[16] = { 0 };//偏色
  5182. swprintf(coloroffBGR, L"%s%s%s", coloroffB, coloroffG, coloroffR);
  5183. strColors[i] = wcstol(colorBGR, NULL, 16);//整形颜色值
  5184. strColoroff[i] = wcstol(coloroffBGR, NULL, 16);//整形偏色值
  5185. m_colorOffR[i] = GetRValue(strColoroff[i]);
  5186. m_colorOffG[i] = GetGValue(strColoroff[i]);
  5187. m_colorOffB[i] = GetBValue(strColoroff[i]);
  5188. //USES_CONVERSION;
  5189. //TSRuntime::add_log( "TS_OCR-coloroffBGR:%s,strColors:%s",W2A(coloroffBGR),W2A(colorBGR));
  5190. }
  5191. if (m_parenthwnd != 0 && m_parenthwnd != ::GetDesktopWindow())//normol找图
  5192. {
  5193. RECT rec;
  5194. RECT clientrec;
  5195. ::GetClientRect(m_parenthwnd, &clientrec);
  5196. int clienthight = clientrec.bottom - clientrec.top;
  5197. int clientwide = clientrec.right - clientrec.left;
  5198. ::GetWindowRect(m_parenthwnd, &rec);
  5199. POINT point;
  5200. point.x = 0;
  5201. point.y = 0;
  5202. ::ClientToScreen(m_parenthwnd, &point);
  5203. m_Left = point.x;
  5204. m_Top = point.y;
  5205. m_Right = rec.right;
  5206. m_bottom = rec.bottom;
  5207. if (left < 0)
  5208. left = 0;
  5209. if (left >= clientwide)
  5210. left = clientwide - 1;
  5211. if (top < 0)
  5212. top = 0;
  5213. if (top >= clienthight)
  5214. top = clienthight - 1;
  5215. if (right >= clientwide)
  5216. right = clientwide - 1;
  5217. if (bottom >= clienthight)
  5218. bottom = clienthight - 1;
  5219. if (m_Left < 0)
  5220. {
  5221. if ((left + m_Left) <= 0)//越界
  5222. left = 0;
  5223. else
  5224. left = m_Left + left;
  5225. }
  5226. else
  5227. left = m_Left + left;
  5228. if (m_Top >= 0)
  5229. top = m_Top + top;
  5230. if (m_Right >= ::GetSystemMetrics(SM_CXSCREEN))
  5231. {
  5232. if ((right + m_Left) > ::GetSystemMetrics(SM_CXSCREEN))
  5233. right = ::GetSystemMetrics(SM_CXSCREEN) - 1;
  5234. else
  5235. right = right + m_Left;
  5236. }
  5237. else
  5238. right = right + m_Left;
  5239. if (m_bottom >= ::GetSystemMetrics(SM_CYSCREEN))
  5240. {
  5241. if ((bottom + m_Top) >= ::GetSystemMetrics(SM_CYSCREEN))
  5242. bottom = ::GetSystemMetrics(SM_CYSCREEN) - 1;
  5243. else
  5244. bottom = bottom + m_Top;
  5245. }
  5246. else
  5247. bottom = bottom + m_Top;
  5248. m_Left = left;
  5249. m_Top = top;
  5250. m_Right = right;
  5251. m_bottom = bottom;
  5252. }
  5253. else
  5254. {
  5255. RECT clientrec;
  5256. if (ColorDataInfo != NULL)
  5257. ::GetClientRect(m_hwnd, &clientrec);
  5258. else//gdi
  5259. ::GetClientRect(hwnd, &clientrec);
  5260. int clienthight = clientrec.bottom - clientrec.top;
  5261. int clientwide = clientrec.right - clientrec.left;
  5262. m_Left = left;
  5263. m_Top = top;
  5264. m_Right = right;
  5265. m_bottom = bottom;
  5266. if (left < 0)
  5267. m_Left = left = 0;
  5268. if (left >= clientwide)
  5269. m_Left = clientwide - 1;
  5270. if (top < 0)
  5271. m_Top = top = 0;
  5272. if (top >= clienthight)
  5273. m_Top = clienthight - 1;
  5274. if (right >= clientwide)
  5275. m_Right = clientwide - 1;
  5276. if (bottom >= clienthight)
  5277. m_bottom = clienthight - 1;
  5278. }
  5279. RECT rc;
  5280. rc.bottom = m_bottom;
  5281. rc.left = m_Left;
  5282. rc.right = m_Right;
  5283. rc.top = m_Top;
  5284. if (ColorDataInfo != NULL)
  5285. {
  5286. wndWidth = m_Right - m_Left;
  5287. wndHeight = m_bottom - m_Top;
  5288. //DX模式找字
  5289. if (!getDXBitmap(pbuffer, ColorDataInfo, 1))
  5290. {
  5291. return retIndex;
  5292. }
  5293. }
  5294. else
  5295. {
  5296. ////GDI模式找字
  5297. if (!getGDIBitmap(hwnd, rc, 1))
  5298. {
  5299. return retIndex;
  5300. }
  5301. }
  5302. GetStringInfo();
  5303. DWORD Findstrings[MAX_PATH * 10] = { 0 };
  5304. bool UnkownStr[MAX_PATH * 10] = { 0 };//未知字体
  5305. /////////////////// 处理多张图 /////////////////////
  5306. int indexstr = 0;
  5307. int findstrX = -1;
  5308. int findstrY = -1;
  5309. int X = 0;
  5310. int Y = 0;
  5311. for (int i = 0; i < dictindex; i++)
  5312. {
  5313. if (simi == 1.0)
  5314. {
  5315. //使用指定字库
  5316. int nstrvaluelen = pWndBmpStr[MyDictCount[usingindex][i].nstrvalue[0]].size();
  5317. for (int n = 0; n < nstrvaluelen; n++)
  5318. {
  5319. findstrX = pWndBmpStr[MyDictCount[usingindex][i].nstrvalue[0]].at(n).x;
  5320. findstrY = pWndBmpStr[MyDictCount[usingindex][i].nstrvalue[0]].at(n).y;
  5321. if (findstrX == -1 || findstrY == -1)
  5322. continue;
  5323. if (findstrX == 42 && findstrY == 100 && i == 36)
  5324. int aadf = 1;
  5325. //if(i==(dictindex-1))//确定是否最后一次循环
  5326. // IsLastFindStr=true;
  5327. //if(MyDictCount[usingindex][i].strwide==2)
  5328. //int aadd=0;
  5329. //TSRuntime::add_log( "TS_OCR-findstrX:%d,findstrY:%d",findstrX,findstrY);
  5330. if (comparaStr(MyDictCount[usingindex][i].nstrvalue, MyDictCount[usingindex][i].strwide, MyDictCount[usingindex][i].strhight, X, Y, findstrX, findstrY, simi, MyDictCount[usingindex][i].nstrcount))
  5331. {
  5332. int swide = MyDictCount[usingindex][i].strwide;
  5333. int shight = MyDictCount[usingindex][i].strhight;
  5334. for (int k = 0; k <= swide; k++)//判断是否是非正规字体
  5335. {
  5336. if ((pWndBmpStrxy[Y - 1][X - 1] != 0 && pWndBmpStrxy[Y - 1][X - 1] >= 16) && shight >= 11)//左上角
  5337. {
  5338. UnkownStr[indexstr] = true;//非正规字体标识
  5339. break;
  5340. }
  5341. if (pWndBmpStrxy[Y - 1][X + k] != 0 && pWndBmpStrxy[Y - 1][X + k] >= 1024)
  5342. {
  5343. UnkownStr[indexstr] = true;//非正规字体标识
  5344. break;
  5345. }
  5346. }
  5347. if (m_parenthwnd != 0 && m_parenthwnd != ::GetDesktopWindow())//normol模式
  5348. {
  5349. POINT pt;
  5350. pt.x = X + m_Left;
  5351. pt.y = Y + m_Top;
  5352. ::ScreenToClient(m_parenthwnd, &pt);
  5353. addrxy[indexstr][0] = pt.x;
  5354. addrxy[indexstr][1] = pt.y;
  5355. }
  5356. else
  5357. {
  5358. addrxy[indexstr][0] = X + m_Left;
  5359. addrxy[indexstr][1] = Y + m_Top;
  5360. }
  5361. addrxy[indexstr][2] = swide;
  5362. addrxy[indexstr][3] = shight;
  5363. //if(X==905&&Y==382)
  5364. // int aadf=1;
  5365. //if(strcmp("_",MyDictCount[usingindex][i].mydistchar)==0)
  5366. // int ddf=1;
  5367. ///////取背景图 点阵坐标
  5368. //USES_CONVERSION;
  5369. Findstrings[indexstr] = (DWORD)(MyDictCount[usingindex][i].mydistchar);
  5370. indexstr++;
  5371. ////重置背景图坐标,防止找重复图
  5372. //pWndBmpStr[MyDictCount[usingindex][i].nstrvalue[0]].at(n).x=-1;
  5373. //pWndBmpStr[MyDictCount[usingindex][i].nstrvalue[0]].at(n).y=-1;
  5374. }
  5375. if (indexstr >= MAX_PATH * 10 - 1)
  5376. break;
  5377. }
  5378. if (indexstr >= MAX_PATH * 10 - 1)
  5379. break;
  5380. }
  5381. else if (simi > 0.1 && simi < 1.0)
  5382. {
  5383. int nstrwide = MyDictCount[usingindex][i].strwide;
  5384. for (int a = 0; a < 11; a++)
  5385. {
  5386. memset(TSRuntime::pLoadstrBuffer[a], 0, nstrwide + 1);
  5387. }
  5388. //将点阵信息还原图片矩阵信息
  5389. byteTopic(MyDictCount[usingindex][i].nstrvalue, nstrwide);
  5390. for (int j = 1; j < 2048; j++)
  5391. {
  5392. //使用指定字库
  5393. int nstrvaluelen = pWndBmpStr[j].size();
  5394. for (int n = 0; n < nstrvaluelen; n++)
  5395. {
  5396. findstrX = pWndBmpStr[j].at(n).x;
  5397. findstrY = pWndBmpStr[j].at(n).y;
  5398. if (findstrX == -1 || findstrY == -1)
  5399. {
  5400. continue;
  5401. }
  5402. if (comparaStrTosim(nstrwide, MyDictCount[usingindex][i].strhight, X, Y, findstrX, findstrY, simi, MyDictCount[usingindex][i].nstrcount))
  5403. {
  5404. int swide = MyDictCount[usingindex][i].strwide;
  5405. int shight = MyDictCount[usingindex][i].strhight;
  5406. for (int k = 0; k <= swide; k++)//判断是否是非正规字体
  5407. {
  5408. if ((pWndBmpStrxy[Y - 1][X - 1] != 0 && pWndBmpStrxy[Y - 1][X - 1] >= 16) && shight >= 11)//左上角
  5409. {
  5410. UnkownStr[indexstr] = true;//非正规字体标识
  5411. break;
  5412. }
  5413. if (pWndBmpStrxy[Y - 1][X + k] != 0 && pWndBmpStrxy[Y - 1][X + k] >= 1024)
  5414. {
  5415. UnkownStr[indexstr] = true;//非正规字体标识
  5416. break;
  5417. }
  5418. }
  5419. if (m_parenthwnd != 0 && m_parenthwnd != ::GetDesktopWindow())//normol模式
  5420. {
  5421. POINT pt;
  5422. pt.x = X + m_Left;
  5423. pt.y = Y + m_Top;
  5424. ::ScreenToClient(m_parenthwnd, &pt);
  5425. addrxy[indexstr][0] = pt.x;
  5426. addrxy[indexstr][1] = pt.y;
  5427. }
  5428. else
  5429. {
  5430. addrxy[indexstr][0] = X + m_Left;
  5431. addrxy[indexstr][1] = Y + m_Top;
  5432. }
  5433. addrxy[indexstr][2] = swide;
  5434. addrxy[indexstr][3] = shight;
  5435. /////取背景图 点阵坐标
  5436. //wsprintf(retstr,L"%s%s",retstr,A2W(TSRuntime::MyDictCount[usingindex][i].mydistchar));
  5437. //Findstrings[indexstr]=(DWORD)(TSRuntime::MyDictCount[usingindex][i].mydistchar);
  5438. Findstrings[indexstr] = (DWORD)(MyDictCount[usingindex][i].mydistchar);
  5439. indexstr++;
  5440. ////重置背景图坐标,防止找重复图
  5441. //pWndBmpStr[j].at(n).x=-1;
  5442. //pWndBmpStr[j].at(n).y=-1;
  5443. }
  5444. if (indexstr >= MAX_PATH * 10 - 1)
  5445. break;
  5446. }
  5447. if (indexstr >= MAX_PATH * 10 - 1)
  5448. break;
  5449. }
  5450. }
  5451. }
  5452. ////TSRuntime::add_log( "TS_OCR-排序-start");
  5453. int addrxyBak[MAX_PATH * 10][4] = { 0 };
  5454. wchar_t tempfindstr[256] = { 0 };
  5455. wchar_t tempfindstr1[256] = { 0 };
  5456. for (int i = 0; i < indexstr - 1; i++)
  5457. {
  5458. retstr[i];
  5459. retstr[i + 1];
  5460. if (addrxy[i + 1][1] < addrxy[i][1])
  5461. {
  5462. for (int j = i + 1; j > 0; j--)
  5463. {
  5464. if (addrxy[j][1] < addrxy[j - 1][1])
  5465. {
  5466. //这里做交换操作
  5467. int tem[10 * 2] = { 0 };
  5468. ::memcpy(tem, addrxy[j - 1], 4 * 4);
  5469. ::memcpy(addrxy[j - 1], addrxy[j], 4 * 4);
  5470. ::memcpy(addrxy[j], tem, 4 * 4);
  5471. DWORD csTemp = Findstrings[j - 1];
  5472. Findstrings[j - 1] = Findstrings[j];
  5473. Findstrings[j] = csTemp;
  5474. bool bret = UnkownStr[j - 1];
  5475. UnkownStr[j - 1] = UnkownStr[j];
  5476. UnkownStr[j] = bret;
  5477. addrxyBak[j - 1][0] = addrxy[j - 1][0];
  5478. addrxyBak[j - 1][1] = addrxy[j - 1][1];
  5479. addrxyBak[j - 1][2] = addrxy[j - 1][2];
  5480. addrxyBak[j - 1][3] = addrxy[j - 1][3];
  5481. }
  5482. else
  5483. {
  5484. addrxyBak[j][0] = addrxy[j][0];
  5485. addrxyBak[j][1] = addrxy[j][1];
  5486. addrxyBak[j][2] = addrxy[j][2];
  5487. addrxyBak[j][3] = addrxy[j][3];
  5488. }
  5489. }
  5490. }
  5491. addrxyBak[i][0] = addrxy[i][0];
  5492. addrxyBak[i][1] = addrxy[i][1];
  5493. addrxyBak[i][2] = addrxy[i][2];
  5494. addrxyBak[i][3] = addrxy[i][3];
  5495. }
  5496. for (int i = 0; i < indexstr - 1; i++)
  5497. {
  5498. if (abs(addrxy[i + 1][1] - addrxy[i][1]) <= 11)
  5499. {
  5500. //这里做交换操作
  5501. for (int m = i + 1; m > 0; m--)
  5502. {
  5503. if (abs(addrxy[m][1] - addrxy[m - 1][1]) <= 11)
  5504. {
  5505. if (addrxy[m][0] < addrxy[m - 1][0])
  5506. {
  5507. int tem[10 * 2] = { 0 };
  5508. ::memcpy(tem, addrxy[m - 1], 4 * 4);
  5509. ::memcpy(addrxy[m - 1], addrxy[m], 4 * 4);
  5510. ::memcpy(addrxy[m], tem, 4 * 4);
  5511. DWORD csTemp = Findstrings[m - 1];
  5512. Findstrings[m - 1] = Findstrings[m];
  5513. Findstrings[m] = csTemp;
  5514. bool bret = UnkownStr[m - 1];
  5515. UnkownStr[m - 1] = UnkownStr[m];
  5516. UnkownStr[m] = bret;
  5517. addrxyBak[m - 1][0] = addrxy[m - 1][0];
  5518. addrxyBak[m - 1][1] = addrxy[m - 1][1];
  5519. addrxyBak[m - 1][2] = addrxy[m - 1][2];
  5520. addrxyBak[m - 1][3] = addrxy[m - 1][3];
  5521. addrxyBak[m][0] = addrxy[m][0];
  5522. addrxyBak[m][1] = addrxy[m][1];
  5523. addrxyBak[m][2] = addrxy[m][2];
  5524. addrxyBak[m][3] = addrxy[m][3];
  5525. }
  5526. else
  5527. {
  5528. addrxyBak[m][0] = addrxy[m][0];
  5529. addrxyBak[m][1] = addrxy[m][1];
  5530. addrxyBak[m][2] = addrxy[m][2];
  5531. addrxyBak[m][3] = addrxy[m][3];
  5532. }
  5533. }
  5534. else
  5535. {
  5536. addrxyBak[m][0] = addrxy[m][0];
  5537. addrxyBak[m][1] = addrxy[m][1];
  5538. addrxyBak[m][2] = addrxy[m][2];
  5539. addrxyBak[m][3] = addrxy[m][3];
  5540. break;
  5541. }
  5542. }
  5543. }
  5544. }
  5545. if (indexstr >= 1)
  5546. {
  5547. addrxyBak[indexstr - 1][0] = addrxy[indexstr - 1][0];
  5548. addrxyBak[indexstr - 1][1] = addrxy[indexstr - 1][1];
  5549. addrxyBak[indexstr - 1][2] = addrxy[indexstr - 1][2];
  5550. addrxyBak[indexstr - 1][3] = addrxy[indexstr - 1][3];
  5551. }
  5552. DWORD Strings[MAX_PATH * 10] = { 0 };//记录字符串指针地址
  5553. DWORD nindex = 0;//记录组合字个数
  5554. DWORD nindexs[MAX_PATH * 10] = { 0 };//记录组合字的下标
  5555. for (int i = 0; i < indexstr; i++)
  5556. {
  5557. int len = strlen((char*)Findstrings[i]);
  5558. len = len / sizeof(wchar_t);
  5559. if (len > 1) //是组合字
  5560. {
  5561. Strings[nindex] = Findstrings[i];
  5562. nindexs[nindex] = i;
  5563. nindex++;
  5564. }
  5565. }
  5566. if (nindex > 0)
  5567. {
  5568. for (int i = 0; i < nindex; i++)
  5569. {
  5570. for (int j = 0; j < indexstr; j++)
  5571. {
  5572. if (nindexs[i] != j)
  5573. {
  5574. char* t1 = (char*)Strings[i];
  5575. char* t2 = (char*)Findstrings[j];
  5576. char* findchar = strstr((char*)Strings[i], (char*)Findstrings[j]);
  5577. if (findchar != NULL)
  5578. {
  5579. if (addrxyBak[nindexs[i]][0] <= addrxyBak[j][0] &&
  5580. (addrxyBak[nindexs[i]][0] + addrxyBak[nindexs[i]][2]) >= (addrxyBak[j][0] + addrxyBak[j][2]) &&
  5581. addrxyBak[nindexs[i]][1] == addrxyBak[j][1])
  5582. {
  5583. for (int n = 0; n < indexstr - j; n++)
  5584. {
  5585. addrxyBak[j + n][0] = addrxyBak[j + n + 1][0];
  5586. addrxyBak[j + n][1] = addrxyBak[j + n + 1][1];
  5587. addrxyBak[j + n][2] = addrxyBak[j + n + 1][2];
  5588. Findstrings[j + n] = Findstrings[j + n + 1];
  5589. }
  5590. indexstr--;
  5591. nindexs[i + 1]--;
  5592. }
  5593. }
  5594. }
  5595. }
  5596. }
  5597. }
  5598. //char tt[MAX_PATH]={0};
  5599. //for(int i=0;i<indexstr;i++)
  5600. //{
  5601. // if(type==0)//OCR
  5602. // {
  5603. // ::strcat(tt,(char *)Findstrings[i]);
  5604. // }
  5605. //}
  5606. //过滤字--2013-5-11//过滤字--2013-5-11//过滤字--2013-5-11//过滤字--2013-5-11//过滤字--2013-5-11
  5607. int nstrs = 0;
  5608. for (int i = 0; i < indexstr; i++)
  5609. {
  5610. int j = 1;
  5611. short hight = addrxyBak[i][3];
  5612. short wide = addrxyBak[i][2];
  5613. short Y = addrxyBak[i][1];
  5614. short X = addrxyBak[i][0];
  5615. bool iscontinuefind = false;
  5616. continuefind:
  5617. //char *t=(char *)Findstrings[i];
  5618. //char *tt=(char *)Findstrings[i+j];
  5619. if (addrxyBak[i + j][0] < (X + wide) && ((addrxyBak[i + j][1] - Y) < hight))
  5620. {
  5621. bool bfind = false;
  5622. for (int n = 0; n < indexstr - i; n++)
  5623. {
  5624. if (addrxyBak[i + j + n][2] > wide && bfind == false && UnkownStr[i + j + n] == false)
  5625. {
  5626. //char *t1=(char *)Findstrings[i+n];
  5627. //char *t2=(char *)Findstrings[i+j+n];
  5628. addrxyBak[i + n][0] = addrxyBak[i + j + n][0];
  5629. addrxyBak[i + n][1] = addrxyBak[i + j + n][1];
  5630. addrxyBak[i + n][2] = addrxyBak[i + j + n][2];
  5631. addrxyBak[i + n][3] = addrxyBak[i + j + n][3];
  5632. UnkownStr[i + n] = UnkownStr[i + j + n];
  5633. Findstrings[i + n] = Findstrings[i + j + n];
  5634. }
  5635. else
  5636. {
  5637. bfind = true;
  5638. //char *t1=(char *)Findstrings[i+j+n];
  5639. //char *t2=(char *)Findstrings[i+j+1+n];
  5640. addrxyBak[i + j + n][0] = addrxyBak[i + j + 1 + n][0];
  5641. addrxyBak[i + j + n][1] = addrxyBak[i + j + 1 + n][1];
  5642. addrxyBak[i + j + n][2] = addrxyBak[i + j + 1 + n][2];
  5643. addrxyBak[i + j + n][3] = addrxyBak[i + j + 1 + n][3];
  5644. UnkownStr[i + j + n] = UnkownStr[i + j + 1 + n];
  5645. Findstrings[i + j + n] = Findstrings[i + j + 1 + n];
  5646. }
  5647. }
  5648. nstrs++;
  5649. int dwnstr = indexstr - nstrs;
  5650. if (dwnstr == 0)
  5651. {
  5652. indexstr = i + 1;
  5653. break;
  5654. }
  5655. //char tempTetstr[MAX_PATH*100]={0};
  5656. //for(int k=0;k<dwnstr;k++)
  5657. //{
  5658. // ::strcat(tempTetstr,(char *)Findstrings[k]);
  5659. //}
  5660. iscontinuefind = true;
  5661. goto continuefind;
  5662. }
  5663. else if (!iscontinuefind)
  5664. nstrs++;
  5665. }
  5666. //过滤字--2013-5-11//过滤字--2013-5-11//过滤字--2013-5-11//过滤字--2013-5-11//过滤字--2013-5-11//过滤字--2013-5-11
  5667. //TSRuntime::add_log( "TS_OCR-排序-end");
  5668. USES_CONVERSION;
  5669. char tempTet[MAX_PATH * 100] = { 0 };
  5670. int len = 0;
  5671. for (int i = 0; i < indexstr; i++)
  5672. {
  5673. if (type == 0)//OCR
  5674. {
  5675. ::strcat(tempTet, (char*)Findstrings[i]);
  5676. }
  5677. else//OCREX
  5678. {
  5679. len = strlen(tempTet);
  5680. if (len == 0)
  5681. {
  5682. sprintf(tempTet, "%s,%d,%d", (char*)Findstrings[i], addrxyBak[i][0], addrxyBak[i][1]);
  5683. }
  5684. else
  5685. {
  5686. if (len <= (MAX_PATH * 100 - 16))
  5687. sprintf(tempTet, "%s|%s,%d,%d", tempTet, (char*)Findstrings[i], addrxyBak[i][0], addrxyBak[i][1]);
  5688. }
  5689. }
  5690. }
  5691. ::wcscpy(retstr, A2W(tempTet));
  5692. retIndex = indexstr;
  5693. //for(int i=0;i<wndHeight;i++)
  5694. //{
  5695. // delete [] pWndBmpStrxy[i];
  5696. // //delete [] pWndBmpBuffer[i];
  5697. // //pWndBmpBuffer[i] = NULL;
  5698. // pWndBmpStrxy[i]=NULL;
  5699. //}
  5700. //delete [] pWndBmpStrxy;
  5701. ////delete [] pWndBmpBuffer;
  5702. //pWndBmpStrxy=NULL;
  5703. ////pWndBmpBuffer=NULL;
  5704. //TSRuntime::add_log( "TS_OCR-end");
  5705. return retIndex;
  5706. }
  5707. bool MyFindPictureClass::MySetDict(int nindex, wchar_t* dictpath, pMyDictInfo* MyDictCount, int* nMyDictCountsize, int& NowUsingDictIndex)//设置字库 加载字库
  5708. {
  5709. bool isSet = false;
  5710. BYTE MaxStrHight[MAX_PATH] = { 0 };
  5711. int indexstr = 1;
  5712. int col = 0;
  5713. HANDLE hfile = NULL;
  5714. DWORD filesize = 0, ReadFileSize = 0;
  5715. //if(!PathFileExists(dictpath))
  5716. // return isSet;
  5717. //if(nindex>=0&&nindex<20&&TSRuntime::MyDictCount[nindex]!=NULL)
  5718. //delete [] TSRuntime::MyDictCount[nindex];
  5719. if (nindex >= 0 && nindex < 20 && MyDictCount[nindex] != NULL)
  5720. delete[] MyDictCount[nindex];
  5721. hfile = ::CreateFile(dictpath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);//打开文件,
  5722. if (hfile == INVALID_HANDLE_VALUE)
  5723. {
  5724. if (TSRuntime::IsShowErrorMsg)
  5725. {
  5726. wchar_t erro[MAX_PATH * 2] = { 0 };
  5727. swprintf(erro, L"打开字库<%s>失败<错误信息=系统找不到指定文件.>", dictpath);
  5728. MessageBox(NULL, erro, L"TC", NULL);
  5729. }
  5730. return false;
  5731. }
  5732. filesize = ::GetFileSize(hfile, NULL);
  5733. char* date = new char[filesize + 1];
  5734. ::ReadFile(hfile, date, filesize, &ReadFileSize, NULL);
  5735. if (ReadFileSize < 1)
  5736. {
  5737. return false;
  5738. }
  5739. ////////////////////根据换行个数计算点阵个数
  5740. int nsize = 0;
  5741. int nadd = 0;
  5742. char* tdate = date;
  5743. while (*tdate)
  5744. {
  5745. if (*tdate != '\n')
  5746. {
  5747. if (nadd > MAX_PATH * 3)
  5748. {
  5749. CloseHandle(hfile);
  5750. return isSet;
  5751. }
  5752. nadd++;
  5753. tdate++;
  5754. }
  5755. else
  5756. {
  5757. nadd = 0;
  5758. tdate++;
  5759. nsize++;
  5760. }
  5761. }
  5762. if (nsize == 0)
  5763. {
  5764. CloseHandle(hfile);
  5765. return isSet;
  5766. }
  5767. ////////////////////根据换行个数计算点阵个数
  5768. pMyDictInfo mydict = new MYDICTINFO[nsize];
  5769. char pathBuffer[MAX_PATH * 3] = { 0 };
  5770. int picstr = 0;
  5771. bool isFind = false;
  5772. while (*date)
  5773. {
  5774. if (*date != '\n')
  5775. {
  5776. if (col > MAX_PATH * 3)
  5777. {
  5778. delete[] mydict;
  5779. return isSet;
  5780. }
  5781. pathBuffer[col++] = *date;
  5782. if ((pathBuffer[0] < 48 && pathBuffer[0] < 57) || (pathBuffer[0] < 65 && pathBuffer[0]>65)) //判断是否到乱码
  5783. {
  5784. date++;
  5785. continue;
  5786. }
  5787. date++;
  5788. }
  5789. else
  5790. {
  5791. if (col < 8)
  5792. break;
  5793. char tempstr[MAX_PATH * 3] = { 0 };
  5794. int index = 0;
  5795. while (pathBuffer[index] != '$')
  5796. {
  5797. if (index == MAX_PATH * 3 - 1)
  5798. {
  5799. delete[] mydict;
  5800. return isSet;
  5801. }
  5802. tempstr[index] = pathBuffer[index];
  5803. index++;
  5804. }
  5805. if ((index * 4) % 11 != 0)
  5806. mydict[dictindex].strwide = (index * 4 - 1) / 11; //记录当前字体字宽
  5807. else
  5808. mydict[dictindex].strwide = (index * 4) / 11; //记录当前字体字宽
  5809. index++;
  5810. int counta = 0;
  5811. char tempstrstr[16] = { 0 };
  5812. while (pathBuffer[index] != '$')
  5813. {
  5814. tempstrstr[counta] = pathBuffer[index];
  5815. index++;
  5816. counta++;
  5817. }
  5818. char* sfind = strrchr(pathBuffer, '$');
  5819. sfind++;
  5820. mydict[dictindex].strhight = atoi(sfind);//记录当前字体字高
  5821. MaxStrHight[mydict[dictindex].strhight] = mydict[dictindex].strhight;
  5822. char bytestr[DICTLENGMAX] = { 0 };
  5823. //把字体点阵还原图色矩阵信息,并且存储起来
  5824. dicthextobyte(tempstr, bytestr, index, mydict[dictindex].nstrcount);
  5825. //if(strcmp(tempstrstr,"情")==0)
  5826. // int aad=0;
  5827. for (int i = 0; i < index / 11; i++)
  5828. {
  5829. char Tstr1[11 + 1] = { 0 };
  5830. strncpy(Tstr1, &bytestr[i * 11], 11);
  5831. mydict[dictindex].nstrvalue[i] = strtol(Tstr1, NULL, 2);
  5832. }
  5833. strcpy(mydict[dictindex].mydistchar, tempstrstr);//字
  5834. ::memset(pathBuffer, 0, MAX_PATH);
  5835. col = 0;
  5836. date++;
  5837. dictindex++;
  5838. picstr = 0;
  5839. }
  5840. }
  5841. //存储当前字库
  5842. if (nindex >= 0 && nindex < 20)
  5843. {
  5844. isSet = true;
  5845. NowUsingDictIndex = nindex;
  5846. usingindex = nindex;
  5847. //TSRuntime::MyDictCount[nindex]=mydict;
  5848. MyDictCount[nindex] = mydict;
  5849. nMyDictCountsize[nindex] = dictindex;
  5850. }
  5851. else
  5852. {
  5853. delete[] mydict;
  5854. return isSet;
  5855. }
  5856. //记录字库的最高字高
  5857. for (int i = MAX_PATH; i > 0; i--)
  5858. {
  5859. if (MaxStrHight[i] != 0 && MaxStrHight[i] < 20)
  5860. {
  5861. TSRuntime::nMaxStrHight[nindex] = MaxStrHight[i];
  5862. break;
  5863. }
  5864. }
  5865. if (TSRuntime::nMaxStrHight[nindex] == 0)//如果字库中的字,字高都高于20,那设置一个默认字高值为16
  5866. TSRuntime::nMaxStrHight[nindex] = 16;
  5867. CloseHandle(hfile);
  5868. return isSet;
  5869. }