IOS development-image search function implementation (source code + resolution), ios image search

Source: Internet
Author: User

IOS development-image search function implementation (source code + resolution), ios image search

The image search function is quite practical. When I implemented this function, I had some notes. Today I will integrate them into blog posts and share them with you.


The main functions of this demo include:

  • Custom photo page
  • Image Recognition
  • Search by Image
  • Information Acquisition (obtain the corresponding information through the identified image)


The following is a simple demonstration:



How can this function be implemented?

It is obviously unrealistic to complete image recognition by yourself.

I first studied Google API, but Google knows it in tianchao...


Then Baidu tried it and the effect was good. In addition, Baidu also has its own App to search for images. However, we only want to learn how to implement the functions.


Therefore, in the image search function, I chose to use the Baidu API. However, Baidu's API is only provided to their "Baidu Pat" APP and is not open to the outside world, I also captured the API through Pat and analyzed it.


Well, the next step is the analysis process. The demo will be provided later.


1. Convert UIImage to NSString

Then we will use the post method to obtain the corresponding json data, but in this request, we need to input the base64Encoding encoded NSString (Here we put the image information)

(Don't ask me why Baidu is designed like this)

    UIImage* pic =[UIImage imageNamed:@"test_1.png"];    NSData* pictureData =UIImagePNGRepresentation(pic);    NSString* pictureDataString =[pictureData base64Encoding];

2. POST request

Here, the url I caught is like this: http://qingpai.baidu.com/api/irs/rex? Reqid = 196423494211296782 & ak = eyJjdCI6IjIwIn0 % 3D & encoding = base64

If you are interested in how to obtain it, you can leave a message. If you are confused, I will write another article later. This is not the topic of this project, so it will not be involved.

Perform a simple analysis on this API.

Qingbai.baidu.com/api/ is clearly provided for "Baidu Pat"

Reqid and ak are bound to the device. You can use this here.

Encoding = base64 indicates the encoding method of incoming data.


The above API is fixed. what needs to be changed is the data passed in during post.

The details are 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 encoding: NSUTF8StringEncoding] dataUsingEncoding: NSUTF8StringEncoding]; // convert bodyString to NSData NSURL * ServerUrl = [NSURL URLWithString: @ "http://qingpai.baidu.com/api/irs/rex? Reqid = 196423494211296782 & ak = signature % 3D & encoding = base64 "]; // obtain the server url NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: serverUrl cachePolicy: Invalid timeoutInterval: 10]; // request this address, timeoutInterval: 10 is set to 10 s Timeout: The request time exceeds 10 s will be considered unable to connect, connection timeout [request setHTTPMethod: @ "POST"]; // POST request [request setHTTPBody: bodyData]; // body data [request setValue: @ "application/x -Www-form-urlencoded "forHTTPHeaderField: @" content-type "]; // request Header NSData * returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil]; NSString * result = [[NSString alloc] initWithData: returnData encoding: NSUTF8StringEncoding]; NSMutableDictionary * addressDic = [result objectFromJSONString]; // send a request asynchronously, after the operation is successful, the data returned by the server // The returned data will be returned with different Encoding Based on the system. For example, if windows is GBK or Ubunt U is utf8... // Note the conversion encoding format NSLog (@ "% @", addressDic );

Let's take a look at the printed information, which is json data. After I split the data, the length is as follows:



There are a lot of returned data. If you are interested, you can analyze them one by one.

A brief introduction.

Facesatar indicates recognition of similar star faces.

"name": "\u90ac\u9756\u9756","nameid": "1931","simi": "0.904725","pid": "1932\/64.jpg","width": "440","height": "440","face_left": "143","face_top": "107","face_width": "193","face_height": "193"

Here is a data item. What we need is the "name" and "simi" attributes, which respectively indicate the star name and similarity.

Other familiarity includes the image size and face position, which can be used if needed.

In addition, the name encoding here is Unicode, so the output may not understand what it means. (Zoom down to name, single output, and Chinese can be seen on the terminal) However, after we get this "name" value, it can be directly displayed and Chinese will be displayed.


In addition, it provides an easy-to-use website for testing.

Conversion from Chinese to Unicode
Unicode to Chinese
Http://javawind.net/tools/native2ascii.jsp? Action = transform


Furthermore, the similar list lists similar images and their sources. You can analyze it by yourself.


3. Obtain detailed information

We can use the json data to obtain the name of the person with the highest similarity (or the scenic spot name, which is a powerful API)

Then, call the following API to display the detailed information. Baidu has encapsulated "Pat.

For example, if the character name is "Jing", the following URL is called:

Http://qingpai.baidu.com/api/proxy/search? Word = Jing & pn = 1

You can see the following results by using Safari:



This encapsulated WAP interface can be opened directly in UIWebView using a URL, and the effect will be good.



Here we will share some APIs that can be used directly. Of course, this API cannot be found elsewhere.


In addition, we can develop a lot of knowledge about image search,

For example, face recognition, optical character recognition, image text recognition ....

The three parts mentioned above have previously written related demos. However, I have been busy with the exam recently and will share it one by one later.


I hope the above content will help you.


Ps: canvassing ~

I participated in the 2014 blog star selection competition. Could you please vote?
Http://vote.blog.csdn.net/blogstar2014/details? Username = hitwhylz # content

Thank you.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.