To figure out this function is quite practical, before the implementation of this function, there are some notes, today on the integration of Cheng Bowen, share to everyone.
The main features of this demo include:
- Customizing the Camera Interface
- Image recognition
- Search Images by image
- Information acquisition (by identifying the image to obtain the corresponding information)
Here is a simple demo, as follows:
So how do you implement such a function?
If you go through the image recognition, it is obviously unrealistic.
I was the first to study the Google API, but Google in the celestial, we all know ...
Then Baidu, tried the next, the effect is good. In addition, Baidu also has its own "map search" corresponding to the app. But we just want to learn how to achieve what kind of function, tube him.
Therefore, in order to map the function, I chose to use the Baidu API, but Baidu this API only to their "Baidu Tap" APP, not open to the outside world, I also by tapping caught the API, and then analysis.
Well, then it's the analysis process. The demo will be available later.
1. UIImage Turn NSString
After that we will use the Post method to obtain the corresponding JSON data, but in this request, to pass in the base64encoding encoded nsstring (here is the picture information)
(Don't ask me why, this is how Baidu is designed.)
uiimage* pic =[uiimage imagenamed:@ "test_1.png"]; nsdata* picturedata =uiimagepngrepresentation (pic); nsstring* picturedatastring =[picturedata base64encoding];
2. Post request
Here, the URL I caught is this: http://qingpai.baidu.com/api/irs/rex?reqid=196423494211296782&ak=eyjjdci6ijiwin0%3d& Encoding=base64
If interested in how to get, you can leave a message, if there is doubt, I will later write a special article. This is not the subject, it's not part of it.
Simple analysis of this API.
qingpai.baidu.com/api/Obviously, it is for "Baidu Pat" provides
REQID, AK, this is a binding device. You can use this as a fixed
ENCODING=BASE64 indicates how the incoming data is encoded.
The API above is fixed, and what needs to be changed is the data passed in when we post.
Specific as follows:
uiimage* pic =[uiimage imagenamed:@ "test_1.png"]; nsdata* picturedata =uiimagepngrepresentation (pic); nsstring* picturedatastring =[picturedata base64encoding]; Post request NSString *post = [NSString stringwithformat:@ "%@", picturedatastring]; NSData *bodydata = [[post stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]datausingencoding: nsutf8stringencoding];//convert bodystring to nsdata data nsurl *serverurl = [Nsurl urlwithstring:@] http://qingpai.baidu.com/ Api/irs/rex?reqid=196423494211296782&ak=eyjjdci6ijiwin0%3d&encoding=base64 "];//gets the URL address to the server Nsmutableurlrequest *request = [Nsmutableurlrequest requestwithurl:serverurl Cachepolicy:nsurlrequestreloadignoringcachedata Timeout interval:10];//request this address, Timeoutinterval:10 set to 10s timeout: The request time exceeds 10s will be considered not connected, connection timeout [request sethttpmethod:@ "POST"];//post requests [Request Sethttpbody:bodydata];//body Data [Request setvalue:@ "application/x-www-form-urlencoded" forhttpheaderfield:@ "Content-type"];//request header NSData *returnDat A = [nsurlconnection sendsynchronousrequest:request returningresponse:nil Error:nil]; NSString *result = [[NSString alloc] Initwithdata:returndata encoding:nsutf8stringencoding]; Nsmutabledictionary *addressdic = [result objectfromjsonstring]; Send request asynchronously, after success will get the data returned by the server//returned data depending on the system will return different encoded data, such as Windows Gbk,ubuntu for UTF8 ... Note the conversion encoding format NSLOG (@ "%@", addressdic);
Look at the printed information, is the JSON data, I split up after the approximate length of this:
There is a lot of data returned, which is interesting for you to analyze.
A simple introduction.
Facesatar to recognize similar star faces.
"Name": "\u90ac\u9756\u9756", "NameID": "1931", "Simi": "0.904725", "pid": "1932\/64.jpg", "width": "[]", "height": "440 "," Face_left ":" 143 "," Face_top ":" 107 "," Face_width ":" 193 "," Face_height ":" 193 "
Here is a piece of data. What we need is mainly the "name" and "Simi" attributes, respectively, the name of the star, the similarity.
Other familiarity includes the size of the picture, the position of the face, and can be used if needed.
In addition, the code here is Unicode, so if the output together may not understand what it means. (Zoom to name, single output, can see Chinese in terminal) but after we get to this "name" value, it can be displayed directly, it will display in Chinese.
Other than that. Provide a useful website for easy testing
Chinese to Unicode
Unicode to Chinese
Http://javawind.net/tools/native2ascii.jsp?action=transform
Again, the similar list here lists similar images and their sources. Specifically, we can analyze it by ourselves.
Three. Get more information
We can get the name of the person with the highest similarity in the JSON data (it can also be the name of the attraction, this API is more powerful)
You can then call the following API to display the detailed information. Baidu is a "pat" package well.
For example, the character name is "Shing", call the following URL:
http://qingpai.baidu.com/api/proxy/search?word= Shing &pn=1
I open it directly with Safari, and I can see the following effects:
Such a well-packaged WAP interface, we can directly in the UIWebView, the use of the URL open, the effect is very good.
Here, the main thing is to share some APIs that we can use directly. Of course, this API is definitely not found elsewhere.
In addition, image search we can also expand a lot of knowledge,
such as face recognition, optical character recognition, image text recognition ....
The 3 parts mentioned above have been written about the demo before. However, recently busy with the exam, and then have time to share one after another.
I hope the above content is helpful to you.
PS: Canvassing ~
I participated in the 2014 Blog Star Contest, please help cast the vote
Http://vote.blog.csdn.net/blogstar2014/details?username=hitwhylz#content
Thank you.
iOS development-with Image search function (source + parsing)