123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- //
- // KMDocumentAIManager.m
- // PDF Master
- //
- // Created by 丁林圭 on 2023/2/2.
- //
- #import "KMDocumentAIManager.h"
- @interface KMDocumentAIManager ()
- //@property (nonatomic, assign) COCRModelType modelType;
- @property (nonatomic, assign) NSUInteger finishIndex;
- @property (nonatomic, retain) NSMutableArray *images;
- //@property (nonatomic, retain) COCREngine * ocrEngine;
- //@property (nonatomic, retain) CIMEngine * imEngine;
- @end
- @implementation KMDocumentAIManager
- - (id)init {
- if (self = [super init]) {
- self.images = [NSMutableArray array];
- }
- return self;
- }
- + (KMDocumentAIManager *)defaultManager {
- static KMDocumentAIManager *singleton = nil;
- static dispatch_once_t pred;
- dispatch_once(&pred, ^{
- singleton = [[KMDocumentAIManager alloc] init];
- });
- return singleton;
- }
- //- (void)recognitionImages:(NSArray *)images withModelType:(COCRModelType)modelType {
- // self.finishIndex = 0;
- // [self.images removeAllObjects];;
- // [self.images addObjectsFromArray:images];
- //
- // self.ocrEngine = [[COCREngine alloc] init];
- // [self.ocrEngine setModelWithType:modelType];
- // if(!self.ocrEngine) {
- // NSError *error = [NSError errorWithDomain:@"PDFReaderBatchErrorDomain" code:-1 userInfo:nil];
- // if([self.delegate respondsToSelector:@selector(documentAIManager:didFailureImageAtIndex:error:)]) {
- // [self.delegate documentAIManager:self didFailureImageAtIndex:self.finishIndex error:error];
- // }
- //
- // if([self.delegate respondsToSelector:@selector(documentAIDidFinish:)]) {
- // [self.delegate documentAIDidFinish:self];
- // }
- // } else {
- //
- // if(self.finishIndex < self.images.count) {
- // if([self.delegate respondsToSelector:@selector(documentAIManagerDidStart:)]) {
- // [self.delegate documentAIManagerDidStart:self];
- // }
- // [self recognitionImage:self.images[self.finishIndex]];
- // }
- // }
- //}
- - (void)recognitionImage:(NSImage *)image {
- // NSString *fileName = [NSString stringWithFormat:@"%@.png", @"recognitionImage"];
- // NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName];
- // if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
- // [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
- // }
- //
- // [image.TIFFRepresentation writeToURL:[NSURL fileURLWithPath:filePath] atomically:YES];
- // NSMutableArray * tResults = [[NSMutableArray alloc] init];
- //
- // COCRErrorType errorType = [self.ocrEngine processWithOCRImageInputPath:filePath andResult:tResults];
- // NSMutableArray * results = [[NSMutableArray alloc] init];
- // for (COCRResult * cOcrResult in tResults) {
- // CGRect rect = cOcrResult.textBounds;
- //
- // rect.origin.y = image.size.height - cOcrResult.textBounds.origin.y;
- // cOcrResult.textBounds = rect;
- // [results addObject:cOcrResult];
- // }
- //
- // if(CIMGErrorTypeNone == errorType) {
- // if([self.delegate respondsToSelector:@selector(documentAIManager:didFinishOCRImageAtIndex:results:)]) {
- // [self.delegate documentAIManager:self didFinishOCRImageAtIndex:self.finishIndex results:results];
- // }
- // self.finishIndex++;
- // if(self.finishIndex < self.images.count) {
- // [self recognitionImage:self.images[self.finishIndex]];
- // } else {
- // if([self.delegate respondsToSelector:@selector(documentAIDidFinish:)]) {
- // [self.delegate documentAIDidFinish:self];
- // }
- // }
- // } else {
- // NSError *error = [NSError errorWithDomain:@"PDFReaderBatchErrorDomain" code:errorType userInfo:nil];
- // if([self.delegate respondsToSelector:@selector(documentAIManager:didFailureImageAtIndex:error:)]) {
- // [self.delegate documentAIManager:self didFailureImageAtIndex:self.finishIndex error:error];
- // }
- // }
- }
- - (void)increaseImages:(NSArray *)images {
- self.finishIndex = 0;
- [self.images removeAllObjects];;
- [self.images addObjectsFromArray:images];
- // self.imEngine = [[CIMEngine alloc] init];
- // if(!self.imEngine) {
- // NSError *error = [NSError errorWithDomain:@"PDFReaderBatchErrorDomain" code:-1 userInfo:nil];
- // if([self.delegate respondsToSelector:@selector(documentAIManager:didFailureImageAtIndex:error:)]) {
- // [self.delegate documentAIManager:self didFailureImageAtIndex:self.finishIndex error:error];
- // }
- //
- // if([self.delegate respondsToSelector:@selector(documentAIDidFinish:)]) {
- // [self.delegate documentAIDidFinish:self];
- // }
- // } else {
- // if(self.finishIndex < self.images.count) {
- // if([self.delegate respondsToSelector:@selector(documentAIManagerDidStart:)]) {
- // [self.delegate documentAIManagerDidStart:self];
- // }
- // [self increaseImage:self.images[self.finishIndex]];
- // }
- // }
- }
- - (void)increaseImage:(NSImage *)image {
- NSString *fileName = [NSString stringWithFormat:@"%@.png", @"increaseImage"];
-
- NSString *outName = [NSString stringWithFormat:@"%@.jpg", @"increaseOutImage"];
- NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName];
- NSString *fileOutPath = [NSTemporaryDirectory() stringByAppendingPathComponent:outName];
- if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
- [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
- }
-
- if ([[NSFileManager defaultManager] fileExistsAtPath:fileOutPath]) {
- [[NSFileManager defaultManager] removeItemAtPath:fileOutPath error:nil];
- }
-
- // [image.TIFFRepresentation writeToURL:[NSURL fileURLWithPath:filePath] atomically:YES];
- // CIMGErrorType errorType = [self.imEngine processWithInputPath:filePath andOutputPath:fileOutPath];
- // if(CIMGErrorTypeNone == errorType) {
- // if([self.delegate respondsToSelector:@selector(imageEngineManager:didFinishCIImageAtIndex:outImage:)]) {
- // [self.delegate imageEngineManager:self didFinishCIImageAtIndex:self.finishIndex outImage:fileOutPath];
- // }
- // self.finishIndex++;
- // if(self.finishIndex < self.images.count) {
- // [self increaseImage:self.images[self.finishIndex]];
- // } else {
- // if([self.delegate respondsToSelector:@selector(documentAIDidFinish:)]) {
- // [self.delegate documentAIDidFinish:self];
- // }
- // }
- // } else {
- // NSError *error = [NSError errorWithDomain:@"PDFReaderBatchErrorDomain" code:errorType userInfo:nil];
- // if([self.delegate respondsToSelector:@selector(documentAIManager:didFailureImageAtIndex:error:)]) {
- // [self.delegate documentAIManager:self didFailureImageAtIndex:self.finishIndex error:error];
- // }
- // }
- }
- @end
|