import UIKit
class ViewController: UIViewController ,UINavigationControllerDelegate,UIImagePickerControllerDelegate{
@IBOutlet weak var carImageView: UIImageview!
@IBOutlet weak var carName: UILabel!
@IBOutlet weak var carDetails: UILabel!
@IBAction func onLibrary(_sender; UIButton) {
let vc = UIImagePickerController()
vc.sourceType = .photoLibrary
vc.allowsEditing = true
vc.delegate = self
present(vc, animated: true)
@IBAction func onCamera( sender: UIButton) {
let vc = UIImagePickerController()
vc.sourceType = .camera
vc.allowsEditing = true
vc.delegate = self
present(vc, animated: true)
uncimagepickerController(_ picker: UIImagePickerController, dFinishPickingMediaWithInfo info:[UIImagePickerController.InfoKey :Any]) {
picker.dismiss(animated: true)
guard let image = info[.editedImage] as? UIImage
else {
return
}
carImageView.image = image
let access_token = get baidu token()
if access token == "" {
return
}
else {
get_car_type(access_token: access_token)
}
func get_car type(access token; String) {
let image = carImageView.image
guard let data = image!.jpegData(compressionQuality: 0.5)
else {
return
}
let data_base64_str = data.base64Encodedstring()
let carType host = "https://aip.baidubce.com/rest/2.0/image-classify/vl/car" + "?access token=" + "\(access token)"
if let url = URL(string: carType host) {
var request = URLRequest(url:url)
request.setValue("application/x-www-form-urlencoded",forHTTPHeaderField:"Content-Type")
request.httpMethod = "POST"
let cs = NSCharacterSet(charactersIn: "/=+%").inverted
let image_urlEncode = data _base64_str.addingPercentEncoding(withAllowedCharacters: cs
let postString = "image=\(image_urlEncode!) &top_num=5&baike num=l"
request.httpBody = postString.data(using: utf)
URLSession.shared.dataTask(with; request){ (data,response, error) in
if error != nil {
print("error")
}
else {
if let json = try? JSONSerialization,jsonObject(with:data!, options: .allowFragments) as? [String : Any]
let result = json("result"l as! Array<Dictionary<Any>>
let name = result[0]["name"] as! String
var description = "非车类"
if name != "非车类" {
let baike_info = result[0]["baike info") as! Dictionary<String,String>
if let details = baike info["description"] {
description = details
}
else{
description = "百度百科无此车资料"
}
DispatchQueue.main.async {
self.carDetails.textm = description
self.carNametext = name
}
}
}
}
.resume()
}
}
func get_baidu_token()-> String {
let API_Key = "EIpAlhdD7i5152EsiNqpU0F1"
let Secret_Key = "p8EZWg4agjDOxABmKvBYSNyZ1YzGFgbm"
let token_host="https://aip.baidubce.com/oauth/2.0/token?grant_ient_credentialssclient_id-\(API_Key)&client_secret-\(Secret Key)"
var access token=""
let semaphore: DispatchSemaphore = DispatchSemaphore(value:0)
if let url= URListring:token host) {
URLSession.shared.dataTask(with; url) { (data, response, error) in
if error != nil {
print("network error")
}
else {
if let json = try? JSONSerializationjsonObject(with:data!,options:allowFragments)as? [String :Any] {
access_token = json["access_token"l as! String
}
semaphore.signal()
}
.resume()
}
else {
print("url error")
}
semaphore.wait()
return access_token
}
}