-
このコメントは管理者の承認待ちです
@interface CATMainViewController ()
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *captureVideoPreviewLayer;
@property (nonatomic, strong) AVCaptureSession *captureSession;
@property (strong) UIImage *capturedImage;
- (void) initCaptureSession;
- (void) startCaptureSession;
- (void) stopCaptureSession;
- (void) cameraPreviewTouched:(UIGestureRecognizer *) sender;
- (void) respondWithData:(NSData *) data;
@end
@implementation CATMainViewController
- (void) respondWithData:(NSData *) data {
NSError *error = nil;
// 正常データはJSON解析を行う
NSDictionary *responseJson = [NSJSONSerialization JSONObjectWithData: data
options: 0
error: &error];
if (responseJson != nil) {
NSDictionary *result = responseJson[@"cloudarApi"];
CFBooleanRef succeeded = (__bridge CFBooleanRef) result[@"success"];
if (succeeded == kCFBooleanTrue) {
NSArray *matchedInfoArray = result[@"detectInfo"];
if (matchedInfoArray.count > 0) {
// 最も一致した可能性の高い画像に紐づくデータを表示
NSDictionary *matchedInfo = matchedInfoArray[0];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle: @"認識成功"
message: matchedInfo[@"value"]
delegate: nil
cancelButtonTitle: @"OK"
otherButtonTitles: nil];
[alertView show];
} else {
// 画像認識失敗
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle: @"認識失敗"
message: nil
delegate: nil
cancelButtonTitle: @"OK"
otherButtonTitles: nil];
[alertView show];
}
} else {
// エラー
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle: @"エラー"
message: result[@"errorMessage"]
delegate: nil
cancelButtonTitle: @"OK"
otherButtonTitles: nil];
[alertView show];
}
}
}
- (void) cameraPreviewTouched:(UIGestureRecognizer *) sender {
if (self.capturedImage != nil) {
// リクエストパラメータ作成
NSData *capturedImagePNGData = UIImagePNGRepresentation(self.capturedImage);
NSCharacterSet *abCharacterSet = [NSCharacterSet alphanumericCharacterSet];
NSString *base64ImageData = [[capturedImagePNGData base64EncodedStringWithOptions: 0] stringByAddingPercentEncodingWithAllowedCharacters: abCharacterSet];
NSString *requestBodyString = [NSString stringWithFormat: @"apiKey=%@&binData=%@",
CloudARAPIKey, base64ImageData];
NSData *requestBodyData = [requestBodyString dataUsingEncoding: NSUTF8StringEncoding];
// URLリクエスト作成
NSURL *apiURL = [NSURL URLWithString: @"http://api.ns-cloudar.com/api/marker/v1/imageSearch.json"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: apiURL];
[request setHTTPMethod: @"POST"];
[request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)requestBodyData.length]
forHTTPHeaderField: @"Content-Length"];
[request setHTTPBody: requestBodyData];
// 通信開始
[NSURLConnection sendAsynchronousRequest: request
queue:[NSOperationQueue mainQueue]
completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error) {
// サーバ応答対応
if ([response isKindOfClass:[NSHTTPURLResponse class]] &&
data != nil && error == nil) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
if (httpResponse.statusCode == 200 ||
httpResponse.statusCode == 403) {
[self respondWithData: data];
} else {
NSString *title = [NSString stringWithFormat: @"HTTP status %ld", (long) httpResponse.statusCode];
NSString *dataString = [[NSString alloc] initWithData: data
encoding: NSUTF8StringEncoding];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle: title
message: dataString
delegate: nil
cancelButtonTitle: @"OK"
otherButtonTitles: nil];
[alertView show];
}
}
}];
}
}
@end