/
CamLingualViewController.m
665 lines (555 loc) · 23.7 KB
/
CamLingualViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
//
// CamLingualViewController.m
// CamLingual
//
// Created by Tsuneo Yoshioka on 8/11/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "CamLingualViewController.h"
#import "OCRWebService.h"
#import <AssetsLibrary/ALAsset.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import "NSMutableDictionary+ImageMetadata.h"
@implementation CamLingualViewController
@synthesize toolbar;
@synthesize webView;
@synthesize imageView;
@synthesize imageButton;
@synthesize notificationLabel;
@synthesize activityIndicatorView;
@synthesize progressView;
@synthesize ocrTextView;
@synthesize translateTextView;
@synthesize sourceLangTableView;
@synthesize destLangTableView;
@synthesize sourceLangLabel;
@synthesize destLangLabel;
@synthesize langSelectView;
@synthesize mainView;
@synthesize topView;
@synthesize aOCRTextViewController;
@synthesize photoButton;
@synthesize albumButton;
@synthesize ocrText = _ocrText;
@synthesize translateText = _translateText;
@synthesize image = _image;
@synthesize imageCropRect;
@synthesize imagemetadata = _imagemetadata;
@synthesize cropImage = _cropImage;
@synthesize imageCropViewController;
@synthesize cameraToolbarController;
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
sourceLangArray = [[NSArray alloc] initWithObjects:@"BRAZILIAN", @"BULGARIAN", @"BYELORUSSIAN", @"CATALAN", @"CROATIAN", @"CZECH", @"DANISH", @"DUTCH", @"ENGLISH", @"ESTONIAN", @"FINNISH", @"FRENCH", @"GERMAN", @"GREEK", @"HUNGARIAN", @"INDONESIAN", @"ITALIAN", @"LATIN", @"LATVIAN", @"LITHUANIAN", @"MOLDAVIAN", @"POLISH", @"PORTUGUESE", @"ROMANIAN", @"RUSSIAN", @"SERBIAN", @"SLOVAK", @"SLOVENIAN", @"SPANISH", @"SWEDISH", @"TURKISH", @"UKRAINIAN", nil];
destLangArray = [[NSArray alloc] initWithObjects:@"AFRIKAANS", @"ALBANIAN", @"AMHARIC", @"ARABIC", @"ARMENIAN", @"AZERBAIJANI", @"BASQUE", @"BELARUSIAN", @"BENGALI", @"BIHARI", @"BULGARIAN", @"BURMESE", @"CATALAN", @"CHEROKEE", @"CHINESE", @"CHINESE_SIMPLIFIED", @"CHINESE_TRADITIONAL", @"CROATIAN", @"CZECH", @"DANISH", @"DHIVEHI", @"DUTCH", @"ENGLISH", @"ESPERANTO", @"ESTONIAN", @"FILIPINO", @"FINNISH", @"FRENCH", @"GALICIAN", @"GEORGIAN", @"GERMAN", @"GREEK", @"GUARANI", @"GUJARATI", @"HEBREW", @"HINDI", @"HUNGARIAN", @"ICELANDIC", @"INDONESIAN", @"INUKTITUT", @"ITALIAN", @"JAPANESE", @"KANNADA", @"KAZAKH", @"KHMER", @"KOREAN", @"KURDISH", @"KYRGYZ", @"LAOTHIAN", @"LATVIAN", @"LITHUANIAN", @"MACEDONIAN", @"MALAY", @"MALAYALAM", @"MALTESE", @"MARATHI", @"MONGOLIAN", @"NEPALI", @"NORWEGIAN", @"ORIYA", @"PASHTO", @"PERSIAN", @"POLISH", @"PORTUGUESE", @"PUNJABI", @"ROMANIAN", @"RUSSIAN", @"SANSKRIT", @"SERBIAN", @"SINDHI", @"SINHALESE", @"SLOVAK", @"SLOVENIAN", @"SPANISH", @"SWAHILI", @"SWEDISH", @"TAJIK", @"TAMIL", @"TAGALOG", @"TELUGU", @"THAI", @"TIBETAN", @"TURKISH", @"UKRAINIAN", @"URDU", @"UZBEK", @"UIGHUR", @"VIETNAMESE", nil];
aOCRWebService = [[OCRWebService alloc] init];
aOCRWebService.user_name = @"YOSHIOKATSUNEO";
aOCRWebService.license_code = @"BE21E7D3-1D0A-4405-8465-A547917C333C";
aGoogleTranslateAPI = [[GoogleTranslateAPI alloc] init];
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
imagePickerCamera = [[UIImagePickerController alloc] init];
imagePickerCamera.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerCamera.delegate = self;
}
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){
imagePickerPhoto = [[UIImagePickerController alloc] init];
imagePickerPhoto.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerPhoto.delegate = self;
}
currentView = LANG_SELECT_VIEW;
self.ocrText = @"Here is OCR text";
self.translateText = @"Here is translate text";
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
cameraToolbarController = [[CameraToolbarController alloc] init];
self.imageCropRect = CGRectMake(0, 0, 1.0, 1.0);
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"locationManager:didUpdateToLocation:%@ fromLocation:%@", newLocation, oldLocation);
}
-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
NSLog(@"locationManager:didUpdateHeading:%@", newHeading);
}
- (void)reloadLang:(BOOL)fInit;
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
sourceLang = [defaults valueForKey:@"sourceLang"];
destLang = [defaults valueForKey:@"destLang"];
if(!destLang){destLang = @"ENGLISH";}
if(sourceLang){
int index = [sourceLangArray indexOfObject:sourceLang];
NSUInteger indexes[] = {0, index};
NSIndexPath *indexpath = [NSIndexPath indexPathWithIndexes:indexes length:2];
[sourceLangTableView selectRowAtIndexPath:indexpath animated:NO scrollPosition:(fInit?UITableViewScrollPositionMiddle:0)];
photoButton.enabled = YES;
albumButton.enabled = YES;
}else{
photoButton.enabled = NO;
albumButton.enabled = NO;
}
{
int index = [destLangArray indexOfObject:destLang];
NSUInteger indexes[] = {0, index};
NSIndexPath *indexpath = [NSIndexPath indexPathWithIndexes:indexes length:2];
[destLangTableView selectRowAtIndexPath:indexpath animated:NO scrollPosition:(fInit?UITableViewScrollPositionMiddle:0)];
}
sourceLangLabel.text = [sourceLang substringToIndex:3];
destLangLabel.text = [destLang substringToIndex:3];
}
- (void)reloadView
{
mainView.hidden = !(currentView == MAIN_VIEW);
langSelectView.hidden = !(currentView == LANG_SELECT_VIEW);
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
[self reloadView];
[self reloadLang:YES];
self.imageView.image = self.cropImage;
[self.imageButton setImage:self.cropImage forState:UIControlStateNormal];
ocrTextView.text = self.ocrText;
translateTextView.text = self.translateText;
NSLog(@"viewController=%@", self.navigationController);
NSLog(@"viewController.controllers=%@", self.navigationController.viewControllers);
}
- (void)viewDidUnload
{
[self setWebView:nil];
[self setImageView:nil];
[self setToolbar:nil];
[self setNotificationLabel:nil];
[self setActivityIndicatorView:nil];
[self setProgressView:nil];
[self setOcrTextView:nil];
[self setTranslateTextView:nil];
[self setSourceLangTableView:nil];
[self setDestLangTableView:nil];
[self setSourceLangLabel:nil];
[self setDestLangLabel:nil];
[self setLangSelectView:nil];
[self setMainView:nil];
[self setAOCRTextViewController:nil];
[self setPhotoButton:nil];
[self setAlbumButton:nil];
[self setImageCropViewController:nil];
[self setTopView:nil];
[self setImageButton:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
// Return YES for supported orientations
// return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
sleep(0);
}
- (void)dealloc {
[aOCRWebService release]; aOCRWebService = nil;
[aGoogleTranslateAPI release]; aGoogleTranslateAPI = nil;
[webView release];
[imageView release];
[popover release];
[actionSheetButtonIndex2sourceType release];
[toolbar release];
[notificationLabel release];
[activityIndicatorView release];
[progressView release];
[ocrTextView release];
[translateTextView release];
[sourceLangTableView release];
[destLangTableView release];
[sourceLangLabel release];
[destLangLabel release];
[langSelectView release];
[mainView release];
[aOCRTextViewController release];
[photoButton release];
[albumButton release];
[imageCropViewController release];
[topView release];
self.cameraToolbarController = nil;
[imageButton release];
[super dealloc];
}
- (void)errorAlert:(NSError*)error
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[error localizedDescription] message:[[error userInfo] description] delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", nil) otherButtonTitles:nil];
[alertView show];
[alertView release];
}
- (void)alert:(NSString*)message
{
NSError *error = [NSError errorWithDomain:@"" code:0 userInfo:[NSDictionary dictionaryWithObject:message forKey:NSLocalizedDescriptionKey]];
[self errorAlert:error];
}
- (void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo
{
//[activityIndicatorView stopAnimating];
// notificationLabel.text = @"UIImageWriteToSavedPhotosAlbum end...";
;
}
- (void)showGoogleTranslatePage:(NSString*)text
{
NSString *encodedOcrText = (NSString*) CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)text, NULL, (CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ", kCFStringEncodingUTF8);
NSString *urlstr = [NSString stringWithFormat:@"http://translate.google.com/m/translate"];
NSURL *url = [NSURL URLWithString:urlstr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Encoding"];
[request setValue:@"CamLingual" forHTTPHeaderField:@"User_Agent"];
NSString *bodystr = [NSString stringWithFormat:@"rls=en&q=%@",encodedOcrText];
[request setHTTPBody:[bodystr dataUsingEncoding:NSUTF8StringEncoding]];
[webView loadRequest:request];
notificationLabel.text = @"";
}
- (void)OCRWebServiceProgress:(OCRWebService *)aOCRWebServer message:(NSString *)message progress:(float)progress
{
notificationLabel.text = message;
if(progress>=0){
progressView.progress = progress;
progressView.hidden = NO;
}else{
progressView.hidden = YES;
}
}
-(void)OCRWebService:(OCRWebService *)aOCRWebService didFailWithError:(NSError *)error
{
[self errorAlert:error];
[activityIndicatorView stopAnimating];
notificationLabel.hidden = YES;
progressView.hidden = YES;
}
-(void)startTranslate:(NSString*)ocrText
{
NSLog(@"%s: start", __FUNCTION__);
ocrTextView.text = self.ocrText = ocrText;
[aGoogleTranslateAPI translate:ocrText sourceLang:sourceLang destLang:destLang delegate:self];
// [self showGoogleTranslatePage:ocrText];
destStartLang = destLang;
[activityIndicatorView startAnimating];
notificationLabel.text = @"Connecting for translation...";
notificationLabel.hidden = NO;
progressView.hidden = YES;
NSLog(@"%s: end", __FUNCTION__);
}
-(void)OCRWebServiceDidFinish:(OCRWebService *)aOCRWebService ocrText:(NSString*)ocrText;
{
[self startTranslate:ocrText];
}
- (void)translate:(GoogleTranslateAPI *)aGoogleTranslateAPI didFailWithError:(NSError *)error
{
[self errorAlert:error];
[activityIndicatorView stopAnimating];
notificationLabel.hidden = YES;
progressView.hidden = YES;
}
- (void)translateProgress:(GoogleTranslateAPI *)aGoogleTranslateAPI message:(NSString *)message
{
notificationLabel.text = message;
notificationLabel.hidden = NO;
progressView.hidden = NO;
}
- (void)translateDidFinished:(GoogleTranslateAPI *)aGoogleTranslateAPI text:(NSString *)text
{
sourceDoneLang = sourceStartLang;
destDoneLang = destStartLang;
translateTextView.text = self.translateText = text;
notificationLabel.hidden = YES;
progressView.hidden = YES;
[activityIndicatorView stopAnimating];
}
- (void)startOCR
{
NSLog(@"calling ocr");
[activityIndicatorView startAnimating];
notificationLabel.text = @"Connecting for OCR...";
notificationLabel.hidden = NO;
progressView.hidden = YES;
NSString *docdir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] ;
NSString *imagefile = [docdir stringByAppendingPathComponent:@"image.jpg"];
[aOCRWebService OCRWebServiceRecognize:imagefile ocrLanguage:sourceLang outputDocumentFormat:@"TXT" delegate:self];
sourceStartLang = sourceLang;
NSLog(@"%s: end", __FUNCTION__);
}
- (void)didFinishUIImageJPEGRepresentation:(NSData*)data
{
NSLog(@"%s: start", __FUNCTION__);
NSString *docdir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] ;
NSString *imagefile = [[docdir stringByAppendingPathComponent:@"image.jpg"] copy];
NSError * error = nil;
NSLog(@"writeToFile start");
if(![data writeToFile:imagefile options:0 error:&error]){
NSLog(@"writeToFile: %@, %@", error, [error userInfo]);
}
[data release]; data = nil;
NSLog(@"writeToFile end");
notificationLabel.text = @"Written to JPEG file.";
[self startOCR];
NSLog(@"%s: end", __FUNCTION__);
}
- (void)asyncWriteToJpeg
{
notificationLabel.text = @"Writing to JPEG file...";
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"UIImageJPEGRepresentation start");
NSData * data = [UIImageJPEGRepresentation(self.cropImage, 0.2) retain];
NSLog(@"UIImageJPEGRepresentation end");
dispatch_async(dispatch_get_main_queue(), ^{
[self didFinishUIImageJPEGRepresentation:data];
});
});
}
-(void)didCancelImageCropViewController:(id)sender
{
((ImageCropViewController*)sender).view.window.rootViewController = self;
if(f_imageCropAsPreview){
if(imagePicker == imagePickerCamera){
[self openPhoto:self];
}else{
[self openAlbum:self];
}
}
}
-(void)didFinishImageCropViewController:(id)sender cropImage:(UIImage *)cropImage cropRect:(CGRect)cropRect;
{
((ImageCropViewController*)sender).view.window.rootViewController = self;
self.imageView.image = self.cropImage = cropImage;
[self.imageButton setImage:self.cropImage forState:UIControlStateNormal];
self.imageCropRect = cropRect;
[self.imageView setNeedsDisplay];
[activityIndicatorView startAnimating];
notificationLabel.text = @"";
notificationLabel.hidden = NO;
if(f_imageCropAsPreview && imagePicker == imagePickerCamera){
if(locationManager){
[self.imagemetadata setLocation:locationManager.location];
}
[locationManager stopUpdatingLocation];
NSLog(@"Saving to Camera Roll...");
notificationLabel.text = @"Saving to Camera Roll...";
ALAssetsLibrary *alalib = [[ALAssetsLibrary alloc] init];
NSLog(@"writeImageToSavedPhotosAlbum start");
[alalib writeImageToSavedPhotosAlbum:(self.image.CGImage) metadata:self.imagemetadata completionBlock:^(NSURL *assetURL, NSError *error){
NSLog(@"completionBlock:%@:%@", assetURL, error);
[self asyncWriteToJpeg];
}];
NSLog(@"writeImageToSavedPhotosAlbum end");
[alalib release];
}else{
[self asyncWriteToJpeg];
}
}
- (void)dumpview:(UIView*)view count:(int)count
{
int index = 0;
NSLog(@"dumpview(%d): view=%@, class=%@", count, view, [view class]);
for (UIView *v in view.subviews){
NSLog(@"index:%d", index);
[self dumpview:v count:count+1];
index++;
}
}
- (void)dumpitems:(NSArray*)items count:(int)count
{
NSLog(@"dumpitems(%d): o=%@, class=%@", count, items, [items class]);
for (NSObject *o in items){
if([items isKindOfClass:[NSArray class]]){
[self dumpitems:(NSArray*)o count:count+1];
}else{
NSLog(@"dumpitems-item(%d): o=%@, class=%@", count, o, [o class]);
}
}
}
-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
NSLog(@"willShowViewController:%@, %@, %@", viewController, viewController.toolbarItems, viewController.tabBarItem);
}
-(void)captureButtonAction:(id)sender
{
imagePickerCamera.showsCameraControls = NO;
[imagePickerCamera takePicture];
}
-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
NSLog(@"willShowViewController:%@, %@, %@", viewController, viewController.toolbarItems, viewController.tabBarItem);
NSLog(@"====================test1=======================");
if(imagePicker == imagePickerCamera){
/* Skip capture page */
UIButton *button = [[[[[[[viewController.view.subviews objectAtIndex:2] subviews] objectAtIndex:1] subviews] objectAtIndex:1] subviews] objectAtIndex:0];
[self dumpview:button count:0];
id target = [[button allTargets] anyObject];
NSString *str = [[button actionsForTarget:target forControlEvent:UIControlEventTouchUpInside] objectAtIndex:0];
SEL action = NSSelectorFromString(str);
[button removeTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[button addTarget:self action:@selector(captureButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
NSLog(@"====================test2=======================");
[self dumpview:viewController.view count:0];
NSLog(@"====================test3=======================");
sleep(0);
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[locationManager stopUpdatingLocation];
[picker dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
currentView = MAIN_VIEW;
[self reloadView];
ocrTextView.text = self.ocrText = @"";
translateTextView.text = self.translateText = @"";
NSLog(@"%s: start", __FUNCTION__);
NSLog(@"[picker dismissModalViewControllerAnimated:YES]");
[picker dismissModalViewControllerAnimated:NO];
[popover dismissPopoverAnimated:NO];
NSLog(@"imageview.image=image start");
UIImage *image = (UIImage*)[info objectForKey:UIImagePickerControllerOriginalImage];
self.imageView.image = self.image = self.cropImage= image;
[self.imageButton setImage:self.cropImage forState:UIControlStateNormal];
self.imageCropRect = CGRectMake(0, 0, 1.0, 1.0);
NSLog(@"imageview.image=image ends");
NSLog(@"[imageView setNeedsDisplay] start");
[imageView setNeedsDisplay];
NSLog(@"[imageView setNeedsDisplay] end");
NSLog(@"editingInfo=[%@]", info);
self.imagemetadata = (NSMutableDictionary*)[info objectForKey:UIImagePickerControllerMediaMetadata];
NSLog(@"metadata=[%@]", self.imagemetadata);
f_imageCropAsPreview = YES;
[imageCropViewController show:self image:self.image cropRect:self.imageCropRect delegate:self];
NSLog(@"%s: end", __FUNCTION__);
}
- (IBAction)openPhoto:(id)sender {
if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
[self alert:@"Camera is not supportted in this device."];
return;
}
[locationManager startUpdatingLocation];
imagePicker = imagePickerCamera;
[self presentModalViewController:imagePicker animated:YES];
}
- (IBAction)openAlbum:(id)sender {
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){
[self alert:@"PhotoLibrary is not supportted in this device."];
return;
}
imagePicker = imagePickerPhoto;
[self presentModalViewController:imagePicker animated:YES];
}else{
if(!popover){
imagePicker = imagePickerPhoto;
popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
[popover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}else{
[popover dismissPopoverAnimated:YES];
[popover release]; popover = nil;
}
}
}
- (void)didFinishOCRTextViewController:(NSString *)text
{
[self startTranslate:text];
}
- (IBAction)editOCRText:(id)sender {
[aOCRTextViewController show:self text:ocrTextView.text delegate:self];
}
- (IBAction)cropImage:(id)sender {
f_imageCropAsPreview = NO;
[imageCropViewController show:self image:self.image cropRect:self.imageCropRect delegate:self];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
[self.ocrTextView resignFirstResponder];
UITouch *touch = [touches anyObject];
CGPoint pt = [touch locationInView:[sourceLangLabel superview]];
// NSLog(@"touchbegin at (%f, %f)", pt.x, pt.y);
if(sourceLang && sourceLangLabel.frame.origin.x <= pt.x && pt.x <= sourceLangLabel.frame.origin.x + sourceLangLabel.frame.size.width && sourceLangLabel.frame.origin.y <= pt.y && pt.y <= destLangLabel.frame.origin.y + destLangLabel.frame.size.height){
if(currentView == MAIN_VIEW){
currentView = LANG_SELECT_VIEW;
}else{
currentView = MAIN_VIEW;
}
[self reloadView];
if(mainView.hidden == NO){
if(self.cropImage && ![sourceDoneLang isEqual:sourceLang]){
[self startOCR];
}else if(![destDoneLang isEqual:destLang]){
[self startTranslate:ocrTextView.text];
}
}
}
}
-(NSArray*)tableViewToArray:(UITableView*)tableView
{
if(tableView == sourceLangTableView){
return sourceLangArray;
}else if(tableView == destLangTableView){
return destLangArray;
}
return nil;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
int index = [indexPath indexAtPosition:1];
NSArray * array = [self tableViewToArray:tableView];
NSString *lang = [array objectAtIndex:index];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:lang];
if(cell == nil){
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:lang] autorelease];
cell.textLabel.text = [[lang substringToIndex:1] stringByAppendingString:[[lang substringFromIndex:1] lowercaseString]];
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int index = [indexPath indexAtPosition:1];
NSArray *array = [self tableViewToArray:tableView];
NSString *lang = [array objectAtIndex:index];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if(tableView == sourceLangTableView){
[defaults setValue:lang forKey:@"sourceLang"];
}else if(tableView == destLangTableView){
[defaults setValue:lang forKey:@"destLang"];
}
[self reloadLang:NO];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int count = 0;
if(tableView == sourceLangTableView){
count = [sourceLangArray count];
}else if(tableView == destLangTableView){
count = [destLangArray count];
}
return count;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if(tableView == sourceLangTableView){
return @"From";
}else if(tableView == destLangTableView){
return @"To";
}
return nil;
}
@end