GithubHelp home page GithubHelp logo

Comments (9)

mikebuss avatar mikebuss commented on July 26, 2024

Hey @chazsmi - can you try calling startScanningWithResultBlock in viewDidAppear instead of viewWillAppear? I've seen this work in the past with Apple's AVFoundation sample project.

If that's unacceptable for your project for some reason, I would try using the raw AVFoundation classes and narrow down if the issue exists there or in MTBBarcodeScanner. I'm happy to help with this if you can wait until the weekend!

from mtbbarcodescanner.

chazsmi avatar chazsmi commented on July 26, 2024

Hi @mikebuss, thanks for your quick reply. Tried viewDidAppear and the preview didnt load at all. Any help you can give would be great. Thanks again

from mtbbarcodescanner.

mikebuss avatar mikebuss commented on July 26, 2024

I modified the sample project to start/stop scanning in viewDidAppear and viewWillDissappear respectively and linked it below. Can you let me know what your project is doing differently than this one? This seems to work on my end.

https://dl.dropboxusercontent.com/u/1462749/MTBBarcodeScanner-9-1-2015.zip

from mtbbarcodescanner.

mikebuss avatar mikebuss commented on July 26, 2024

I'm going to close this for now as it seems to be working in the demo project I linked. If you encounter this issue, please try to replicate it in the project above and I'll be happy to take a look!

from mtbbarcodescanner.

chazsmi avatar chazsmi commented on July 26, 2024

Hi Mike,

Sorry I'v been slow to reply. Only got back to this today, a little bit stumped ive looked at your code and tried to make my methods implement the scanner in the same way. Here is my code:

- (void)viewDidAppear:(BOOL)animated
{
    _util = [[Utils alloc] init];

    if ([self.scanner isScanning]) {
        [self.scanner stopScanning];
    } else {
        [MTBBarcodeScanner requestCameraPermissionWithSuccess:^(BOOL success) {
            if (success) {
                [self startScanning];
            } else {
                [_util showAlert:@"No camera permission" msg:@"You need to give permission to you use your camera to use the scanner"];
            }
        }];
    }
    [super viewDidAppear:animated];
}

and here is my startScanning method

- (void)startScanning {

    self.uniqueCodes = [[NSMutableArray alloc] init];

    [self.scanner startScanningWithResultBlock:^(NSArray *codes) {
        for (AVMetadataMachineReadableCodeObject *code in codes) {
            if (code.stringValue && [self.uniqueCodes indexOfObject:code.stringValue] == NSNotFound) {
                [self.uniqueCodes addObject:code.stringValue];

                NSLog(@"Found unique code: %@", code.stringValue);

                NSString *way = @"plus";

                if (self.refControl.selectedSegmentIndex == 0) {
                    // item finished
                    way = @"minus";
                }

                if ([self.quantity.text isEqualToString:@""]) {

                    self.quantity.text = @"1";
                }

                NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
                NSString *userId = [defaults objectForKey:@"userID"];
                NSLog(@"%@", self.quantity.text);
                NSDictionary *params = @{@"barcode" : code.stringValue,
                                         @"way" : way,
                                         @"quantity" : self.quantity.text,
                                         @"userId" : userId};

                AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

                NSString* HOST_URL = [NSString stringWithFormat:@"%@%@", appDelegate.URL, @"/api/v1/getitembybarcode"];

                [_util showLoading:self.camera];

                AFHTTPRequestOperationManager *operationManager = [_util setCache];

                [operationManager POST:HOST_URL parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject){

                    if ([responseObject[@"success"] intValue] == 1)
                    {
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"RELOAD_DATA"object:nil];
                        NSString *alertMsg = [NSString stringWithFormat:@"%@%@", responseObject[@"responseData"][@"name"], @" updated"];
                        [_util showAlert:alertMsg msg:alertMsg];
                    } else {
                        [_util showAlert:@"Error finding product" msg:responseObject[@"responseData"]];
                    }

                    [_util stopLoading];

                }failure:^(AFHTTPRequestOperation *operation, NSError *error){

                    // Enter what happens here if failure happens
                    NSLog(@"%@su", error);

                }];

            }
        }
    }];
}

- (void)viewWillDisappear:(BOOL)animated {
    [self.scanner stopScanning];
    [super viewWillDisappear:animated];
}

Thanks for your help

from mtbbarcodescanner.

mikebuss avatar mikebuss commented on July 26, 2024

That code looks fine to me. Can you try doing an NSAssert(self.scanner) at the top of startScanning to make sure the scanner isn't nil when you come back to the view? I don't see the code where self.scanner is created, so that's my first guess.

MTBBarcodeScanner will assert that a camera is present and scanning is permitted when you call startScanningWithResultBlock:, so there aren't really any failure states. You might check if the previewView is nil when you come back to the view as well.

Another idea would be to add a button to start/stop scanning (just as a test) and make sure you can toggle scanning that way. If so, we can narrow it down to the UIView willAppear willDisappear methods.

Hope this helps.

from mtbbarcodescanner.

chazsmi avatar chazsmi commented on July 26, 2024

Hi Mike,

Here is the console after NSLogging the self.scanner and the UIView after loading the view for the first time then going away and back again:

2015-09-15 21:25:49.722  [647:196033] Scanner called
2015-09-15 21:25:49.723  [647:196033] (null)
2015-09-15 21:25:49.725 [647:196033] <UIView: 0x14561c1b0; frame = (0 56; 600 544); autoresize = RM+BM; layer = <CALayer: 0x17422f520>>
2015-09-15 21:25:53.100 [647:196033] Scanner called
2015-09-15 21:25:53.100 [647:196033] <MTBBarcodeScanner: 0x170270640>
2015-09-15 21:25:53.100 [647:196033] <UIView: 0x14561c1b0; frame = (0 56; 600 0); autoresize = RM+BM; layer = <CALayer: 0x17422f520>>

and heres the whole file:

@interface ScanViewController ()
@property (nonatomic, strong) MTBBarcodeScanner *scanner;
@property (nonatomic, strong) NSMutableArray *uniqueCodes;
@property (nonatomic, weak) IBOutlet UIView *camera;
@property (nonatomic, strong) Utils *util;
@end

@implementation ScanViewController

- (void)viewWillAppear:(BOOL)animated
{
    _util = [[Utils alloc] init];

    NSLog(@"Scanner called");
    NSLog(@"%@", self.camera);
   // if (self.scanner) {
        self.scanner = [[MTBBarcodeScanner alloc] initWithPreviewView:self.camera];
    //}

    if ([self.scanner isScanning]) {
        [self.scanner stopScanning];
    } else {
        [MTBBarcodeScanner requestCameraPermissionWithSuccess:^(BOOL success) {
            if (success) {
                [self startScanning];
            } else {
                [_util showAlert:@"No camera permission" msg:@"You need to give permission to you use your camera to use the scanner"];
            }
        }];
    }
    [super viewWillAppear:animated];
}

//- (MTBBarcodeScanner *)scanner {
//    NSLog(@"Scanner called");
//    NSLog(@"%@", self.scanner);
//    NSLog(@"%@", self.camera);
//    if (self.scanner) {
//        self.scanner = [[MTBBarcodeScanner alloc] initWithPreviewView:self.camera];
//    }
//    return self.scanner;
//}

- (void)startScanning {

    self.uniqueCodes = [[NSMutableArray alloc] init];

    [self.scanner startScanningWithResultBlock:^(NSArray *codes) {
        for (AVMetadataMachineReadableCodeObject *code in codes) {
            if (code.stringValue && [self.uniqueCodes indexOfObject:code.stringValue] == NSNotFound) {
                [self.uniqueCodes addObject:code.stringValue];

                NSLog(@"Found unique code: %@", code.stringValue);

                NSString *way = @"plus";

                if (self.refControl.selectedSegmentIndex == 0) {
                    // item finished
                    way = @"minus";
                }

                if ([self.quantity.text isEqualToString:@""]) {

                    self.quantity.text = @"1";
                }

                NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
                NSString *userId = [defaults objectForKey:@"userID"];
                NSLog(@"%@", self.quantity.text);
                NSDictionary *params = @{@"barcode" : code.stringValue,
                                         @"way" : way,
                                         @"quantity" : self.quantity.text,
                                         @"userId" : userId};

                AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

                NSString* HOST_URL = [NSString stringWithFormat:@"%@%@", appDelegate.URL, @"/api/v1/getitembybarcode"];

                [_util showLoading:self.camera];

                AFHTTPRequestOperationManager *operationManager = [_util setCache];

                [operationManager POST:HOST_URL parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject){

                    if ([responseObject[@"success"] intValue] == 1)
                    {
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"RELOAD_DATA"object:nil];
                        NSString *alertMsg = [NSString stringWithFormat:@"%@%@", responseObject[@"responseData"][@"name"], @" updated"];
                        [_util showAlert:alertMsg msg:alertMsg];
                    } else {
                        [_util showAlert:@"Error finding product" msg:responseObject[@"responseData"]];
                    }

                    [_util stopLoading];

                }failure:^(AFHTTPRequestOperation *operation, NSError *error){

                    // Enter what happens here if failure happens
                    NSLog(@"%@su", error);

                }];

            }
        }
    }];
}

- (IBAction)actionChange:(id)sender
{

}

- (void)viewDidDisappear:(BOOL)animated {
    [self.scanner stopScanning];
    [super viewDidDisappear:animated];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation

 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

//- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
//{
//    [[self view] endEditing:YES];
//}

@end

from mtbbarcodescanner.

mikebuss avatar mikebuss commented on July 26, 2024

@chazsmi can you create a small project to illustrate the issue you're having? If you can send that my way, I can take a closer look.

The code you mentioned doesn't seem to produce the output you linked. It's outputting:

2015-09-15 21:25:53.100 [647:196033] <MTBBarcodeScanner: 0x170270640>

But the only line that outputs the scanner, like so:

NSLog(@"%@", self.scanner);

Is currently commented out:

//- (MTBBarcodeScanner *)scanner {
//    NSLog(@"Scanner called");
//    NSLog(@"%@", self.scanner);
//    NSLog(@"%@", self.camera);
//    if (self.scanner) {
//        self.scanner = [[MTBBarcodeScanner alloc] initWithPreviewView:self.camera];
//    }
//    return self.scanner;
//}

So I'll need some sort of project to work with. If you want to start with the example I provided and try to break it with your use-case, I'd be happy to take a look if you could send it my way!

from mtbbarcodescanner.

mikebuss avatar mikebuss commented on July 26, 2024

Also it looks like the frame of the UIView changes from one log statement to the next. In the second one, the height is 0:

2015-09-15 21:25:53.100 [647:196033] <UIView: 0x14561c1b0; frame = (0 56; 600 0); autoresize = RM+BM; layer = <CALayer: 0x17422f520>>

from mtbbarcodescanner.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.