Objective c + Tesseract issue - objective-c

I've simple code that performs recognition text on image as below:
#import "ParseLinesFromImage.h"
#import <TesseractOCR/TesseractOCR.h>
#implementation ParseLinesFromImage
- (void) performRecognitionWithImage:(UIImage*)image
{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
G8Tesseract* tesseract = [[G8Tesseract alloc] initWithLanguage:#"eng"];
tesseract.language = #"eng";
tesseract.engineMode = G8OCREngineModeTesseractCubeCombined;
tesseract.pageSegmentationMode = G8PageSegmentationModeAuto;
tesseract.maximumRecognitionTime = 60.0;
tesseract.image = [image g8_blackAndWhite];
BOOL recognized = [tesseract recognize];
if (recognized == YES)
{
NSDictionary *array = [self parseValidLines:tesseract.recognizedText];
dispatch_async(dispatch_get_main_queue(), ^{
[self.delegate sucessWithText:array];
});
}
else
{
dispatch_async(dispatch_get_main_queue(), ^{
[self.delegate failedTextReader:nil];
});
}
});
}
Code define in .h file like below:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#protocol ParseLinesFromImageDelegate<NSObject>
#required
-(void) sucess:(nullable NSDictionary*)info;
-(void) failed:(nullable NSError*) error;
#end
#interface ParseLinesFromImage: NSObject
#property (nonatomic, weak, nullable) id <ParseLinesFromImageDelegate> delegate;
- (void) performRecognitionWithImage:(UIImage* _Nonnull)image;
#end
But when running to line marked as 1, system push an exeption
"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImage g8_blackAndWhite]: unrecognized selector sent to instance 0x60c0000b9e00'"
In my view it may be an ambiguous between UIImage+G8Filters.h(define g8_blackAndWhite) and UIImage but I look into TesseractOCR example, it's the same as mine.
Please help me to find out the reason and how to fix it, thanks.
PS: Thanks you for reading my question, after google and follow answer from https://github.com/gali8/Tesseract-OCR-iOS/wiki/Installation (side note), the issue goes away and in my case, I added _all-load to my project.

Related

copyWithZone error -[DataObject setECGCount:]: unrecognized selector sent to instance

My application crashed with the following error:
-[Data Object set ECGCount:]: unrecognized selector sent to instance 0x281671ad0
I am trying to pass data between my main viewController class into DataObject class which will then finally pass this information into IntervalGraph class.
I am doing this using copyWithZone however am getting stuck.
here is the viewContoller.h Class:
#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"
#interface ViewController : UIViewController<CPTPlotDataSource>
#property (nonatomic, assign) int eCGCount;
#property (nonatomic, assign) int eCGheartRate;
#end
and the relevant code in viewController.m
#property (nonatomic) DataObject *maxRecordedHR, *minRecordedHR, *currentData, *continuousData, *seizureData;
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter addObserver:self
selector:#selector(sensorDataUpdated)
name:#"LYRDeviceCommunicatorDataUpdated"
object:nil];
self.rollingAverageHRArray = [[NSMutableArray alloc]initWithCapacity:180];
self.currentData = [[DataObject alloc]init];
self.continuousData = [[DataObject alloc]init];
self.seizureData = [[DataObject alloc]init];
}
//fired by a 1 second timer
-(void)startIntervalGraph {
[self sensorDataUpdated];
if (self.currentData.heartrate == 0 )
{NSLog(#"No data");
} else {
self.rollingAverageLabel.text = [NSString stringWithFormat:#"rolling 3 min average %1.1f", self.currentData.rollingaverageheartrate];
[self.intervalGraph addDataObject:self.currentData]; // something wrong here
[self.intervalGraph updateGraph];
NSLog(#"Updating Interval Graph");
}
}
-(void) sensorDataUpdated {
//Heart Rate
self.currentData.heartrate = self.eCGheartRate;
NSLog(#"Current Data %d", self.currentData.heartrate);
}
-(void)setECGCount:(int)eCGCount
{
self.eCGheartRate = (int) eCGCount;
eCGCount = self.eCGCount;
NSLog(#"eCGCount %d", self.eCGCount);
}
then the DataObject.h class
#import <Foundation/Foundation.h>
#interface DataObject : NSObject <NSCopying>
#property int heartrate;
#property float rollingaverageheartrate;
#property float rrinterval;
#property NSTimeInterval occurrence;
#property NSInteger minHRsetting;
#property NSInteger maxHRsetting;
#property int alarmtripped;
#end
and the DataObject.m
#import "DataObject.h"
#import "CRPC_300SDK.h"
#import "ViewController.h"
#implementation DataObject
- (id)copyWithZone:(NSZone *)zone
{
id copy = class alloc] init];
if (copy) {
// Set primitives
[copy setECGCount:self.heartrate]; //THIS IS WHERE THE CRASH OCCURS.
//[copy setRrinterval:self.rrinterval];
[copy setRollingaverageheartrate:self.rollingaverageheartrate];
[copy setOccurrence:self.occurrence];
[copy setMinHRsetting:self.minHRsetting];
[copy setMaxHRsetting:self.maxHRsetting];
[copy setAlarmtripped:self.alarmtripped];
}
return copy;
}
When i call
[copy setECGCOUNT:self.heartrate];
The app crashes with the following error:
2020-08-20 11:58:57.884543+0100 PulseGuardian[3927:2317144]
-[DataObject setECGCount:]: unrecognized selector sent to instance 0x281671ad0
2020-08-20 11:58:57.885908+0100 PulseGuardian[3927:2317144] *
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[DataObject setECGCount:]:
unrecognized selector sent to instance 0x281671ad0'
How can i fix this problem?
As #Larme pointed out in the comments, i was referencing the wrong method in the
DataObject.m
The code read:
[copy setECGCount:self.heartrate];
when it should have read:
[copy setHeartrate:self.heartrate];
once code was changed the app worked as it should. Silly error on my behalf but will leave this here in case it helps anybody else in the future.

React Native and Objective C delegates

I am quite new to react native and and the bridging mechanism with native code, especially when the framework has delegates. Assume I am trying to bridge the following framework:
#protocol BRPtouchNetworkDelegate;
#class PLNetworkModule;
#interface BRPtouchNetworkManager : NSObject <NSNetServiceBrowserDelegate,NSNetServiceDelegate>
#property(retain, nonatomic) NSMutableArray* registeredPrinterNames;
#property(assign, nonatomic) BOOL isEnableIPv6Search;
- (int)startSearch: (int)searchTime;
- (NSArray*)getPrinterNetInfo;
- (BOOL)setPrinterNames:(NSArray*)strPrinterNames;
- (BOOL)setPrinterName:(NSString*)strPrinterName;
- (id)initWithPrinterNames:(NSArray*)strPrinterNames;
- (id)initWithPrinterName:(NSString*)strPrinterName;
#property (nonatomic, assign) id <BRPtouchNetworkDelegate> delegate;
#end
#protocol BRPtouchNetworkDelegate <NSObject>
-(void) didFinishSearch:(id)sender;
#end
The following is the bridge module I implemented:
RCTBRPtouchNetworkManager.h
#import <React/RCTBridgeModule.h>
#import <BRPtouchPrinterKit/BRPtouchPrinterKit.h>
#interface RCTBRPtouchNetworkManager : NSObject <RCTBridgeModule, BRPtouchNetworkDelegate>
#end
RCTBRPtouchNetworkManager.m
#import "RCTBRPtouchNetworkManager.h"
#import <BRPtouchPrinterKit/BRPtouchPrinterKit.h>
#import <React/RCTLog.h>
#implementation RCTBRPtouchNetworkManager {
BRPtouchNetworkManager *_networkManager;
}
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(addEvent:(NSString *)name location:(NSString *)location)
{
RCTLogInfo(#"Pretending to create an event %# at %#", name, location); //a dummy method to test the bridge
}
RCT_EXPORT_METHOD(startSearchWithTimeout:(int)time) {
RCTLogInfo(#"Bridge started search with time %d", time);
_networkManager = [[BRPtouchNetworkManager alloc] init];
_networkManager.delegate = self; //I'm setting delegate here
_networkManager.isEnableIPv6Search = NO;
NSString * path = [[NSBundle mainBundle] pathForResource:#"PrinterList" ofType:#"plist"];
if( path )
{
NSDictionary *printerDict = [NSDictionary dictionaryWithContentsOfFile:path];
NSArray *printerList = [[NSArray alloc] initWithArray:printerDict.allKeys];
[_networkManager setPrinterNames:printerList];
} else {
RCTLogInfo(#"PrinterList path not found");
}
// Start printer search
[_networkManager startSearch: 5.0];
}
- (void)didFinishSearch:(id)sender {
NSLog(#"didFinishedSearch"); //this delegate method is not called
}
#end
I can easily call the dummy method and see the results in the logs. However, the delegate method didFinishSearch() is never called. I call this from javascript as follows:
componentDidMount() {
let networkManager = NativeModules.BRPtouchNetworkManager;
networkManager.startSearchWithTimeout(5.0);
}
I there something I am missing? Am I implementing delegate properly? Is this kind of functionality even possible (can't seem to not since the delegate method was used by iOS community for a long time). Your help is much appreciated.
EDIT
I found that adding the following to my bridge manager file made the delegate to fire (thanks to this post)
- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}
However, even though this solves the problem, I'd like a more technical understanding on what is going on here since I can't seem to exactly grasp it. Thank you
I know this isn’t an an answer to the post but for the bit where you’ve asked for a more technical understanding - dispatch_get_main_queue(); puts the delegate method responses on to the main thread. Since JS is single threaded any process on the background thread won’t be visible to it.

NSUserDefaults NSColor extension error

This is my first time creating an extension and I'm extending NSUserDefaults. I'm just using the code that I found on Apple's developer page, but I'm getting errors in the project in my .h and .m files. I created the files for macOS as the Cocoa class type. How can I fix this? Here's the code along with the errors:
NSUserDefaults+MyColourSupport.h
#import <Foundation/Foundation.h>
#interface NSUserDefaults(MyColourSupport)
-(void)setColor:(NSColor *)aColor forKey:(NSString *)aKey;
-(NSColor*)colorForKey:(NSString *)aKey;
#end
NSUserDefaults+MyColourSupport.m
#import "NSUserDefaults+MyColourSupport.h"
#implementation NSUserDefaults(MyColourSupport)
-(void)setColor:(NSColor*)aColor forKey:(NSString*)aKey {
NSData *theData = [NSArchiver archivedDataWithRootObject:aColor];
[self setObject:theData forKey:aKey];
}
-(NSColor*)colorForKey:(NSString*)aKey {
NSColor *theColor = nil;
NSData *theData = [self dataForKey:aKey];
if (theData != nil)
theColor = (NSColor*)[NSUnarchiver unachiveObjectWithData:theData];
return theColor;
}
#end

Implementing ZBar QR Code Reader in UIView

I really need help here. I'm pretty new to iOS/Objective-C so sorry if the problem resolution is obvious or if my code is terrible. Be easy on me!! :-)
I'm struggling to integrate ZBarSDK for reading QR Codes into an iPad app i'm building. If I use ZBarReaderController (of which there are plenty of tutorials and guides on implementing), it works fine. However I want to make the camera come up in a UIView as opposed to the fullscreen camera.
Now I have gotten as far as making the camera view (readerView) come up in the UIView (ZBarReaderView) as expected, but I get an error when it scans a code. The error does not come up until a code is scanned making me believe this is either delegate related or something else.
Here's the important parts of my code: (ZBarSDK.h is imported at the PCH file)
SignInViewController.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#class AVCaptureSession, AVCaptureDevice;
#interface SignInViewController : UIViewController
< ZBarReaderDelegate >
{
ZBarReaderView *readerView;
UITextView *resultText;
}
#property (nonatomic, retain) UIImagePickerController *imgPicker;
#property (strong, nonatomic) IBOutlet UITextView *resultText;
#property (strong, nonatomic) IBOutlet ZBarReaderView *readerView;
-(IBAction)StartScan:(id) sender;
SignInViewController.m
#import "SignInViewController.h"
#interface SignInViewController ()
#end
#implementation SignInViewController
#synthesize resultText, readerView;
-(IBAction)StartScan:(id) sender
{
readerView = [ZBarReaderView new];
readerView.readerDelegate = self;
readerView.tracksSymbols = NO;
readerView.frame = CGRectMake(30,70,230,230);
readerView.torchMode = 0;
readerView.device = [self frontFacingCameraIfAvailable];
ZBarImageScanner *scanner = readerView.scanner;
[scanner setSymbology: ZBAR_I25
config: ZBAR_CFG_ENABLE
to: 0];
[self relocateReaderPopover:[self interfaceOrientation]];
[readerView start];
[self.view addSubview: readerView];
resultText.hidden=NO;
}
- (void) readerControllerDidFailToRead: (ZBarReaderController*) reader
withRetry: (BOOL) retry{
NSLog(#"the image picker failing to read");
}
- (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info
{
NSLog(#"the image picker is calling successfully %#",info);
id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults];
ZBarSymbol *symbol = nil;
NSString *hiddenData;
for(symbol in results)
hiddenData=[NSString stringWithString:symbol.data];
NSLog(#"the symbols is the following %#",symbol.data);
resultText.text=symbol.data;
NSLog(#"BARCODE= %#",symbol.data);
NSLog(#"SYMBOL : %#",hiddenData);
resultText.text=hiddenData;
}
The error I get when a code is scanned:
2012-12-16 14:28:32.797 QRTestApp[7970:907] -[SignInViewController readerView:didReadSymbols:fromImage:]: unrecognized selector sent to instance 0x1e88b1c0
2012-12-16 14:28:32.799 QRTestApp[7970:907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SignInViewController readerView:didReadSymbols:fromImage:]: unrecognized selector sent to instance 0x1e88b1c0'
I'm not too worried about what happens with the results just yet, just want to get over this error. Took me ages just to get the camera to come up in the UIView due to severe lack of tutorial or documentation on ZBarReaderView (for beginners anyway). Thanks all.
I fixed this. Had an incorrect delegate name. It should have been "ZbarReaderViewDelegate". I was missing the "view" part. Doh!!
#interface SignInViewController : UIViewController
< ZBarReaderViewDelegate >
{
ZBarReaderView *readerView;
UITextView *resultText;
}

Why I get this error? unrecognized selector sent to instance [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
I want highlight text on UIWebView
I'm getting the error "unrecognized selector sent to instance 0x756cef0" when calling -[UIWebView highlightAllOccurencesOfString:]. The selector was declared in WBHighlight.h and I use forward declaration in WBSecondViewController.h.
WBSecondViewController.h
#class WBHighlight;
#import <UIKit/UIKit.h>
#interface WBSecondViewController : UIViewController <UIWebViewDelegate, UIScrollViewDelegate>
#property (weak, nonatomic) IBOutlet WBHighlight *webView;
- (IBAction)searchButtonPressed:(id)sender;
- (IBAction)clearHighlights:(id)sender;
#end
WBSecondViewController.m
#import "WBSecondViewController.h"
#import "WBHighlight.h"
#interface WBSecondViewController ()
#end
#implementation WBSecondViewController
-(IBAction)searchButtonPressed:(id)sender{
NSLog(#"highlighttes");
[_webView highlightAllOccurencesOfString:#"cat"];
}
-(IBAction)clearHighlights:(id)sender{
[_webView removeAllHighlights];
}
WBHighlight.h
#import <UIKit/UIKit.h>
#interface WBHighlight : UIWebView{
}
- (NSInteger)highlightAllOccurencesOfString:(NSString*)str;
- (void)removeAllHighlights;
#end
WBHighlight.m
#import "WBHighlight.h"
- (NSInteger)highlightAllOccurencesOfString:(NSString*)str
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"UIWebViewSearch" ofType:#"js"];
NSString *jsCode = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[self stringByEvaluatingJavaScriptFromString:jsCode];
NSString *startSearch = [NSString stringWithFormat:#"uiWebview_HighlightAllOccurencesOfString('%#')",str];
[self stringByEvaluatingJavaScriptFromString:startSearch];
NSString *result = [self stringByEvaluatingJavaScriptFromString:#"uiWebview_SearchResultCount"];
return [result integerValue];
}
- (void)removeAllHighlights
{
[self stringByEvaluatingJavaScriptFromString:#"uiWebview_RemoveAllHighlights()"];
}
#end
any idea?
This issue is due to you connected the WBHighlight outlet to a UIWebView.
Probably the WBHighlight is a subclassed UIWebView in which highlightAllOccurencesOfString: method is declared and defined. This method is not present in standard UIWebView, that's why it is throwing an error like: unrecognized selector sent to instance.
You need to change the class of UIWebView to WBHighlight in interface builder.
Go to your identity inspector
Select your WebView
Change the class of UIWebView to WBHighlight
Hekiru, You need to make the object of WBHighlight in order to call that method. And, for this you need to import that class and make object.
Let say,
WBHighlight *objWBHighlight = [WBHighlight new];
//Then, call that required method:
int someVarToAssign = [objWBHighlight highlightAllOccurencesOfString:#"fsfsf"];
Hope, it'll sort-out your problem.Try it.
In any concern let me know. :)