How Do I Change The Frame Of A ZBar Reader?
Ok, so I'm using the ZBar SDK to scan barcodes in my iPhone app. I've successfully implemented the sample code, but now I want to change the frame of the scanner view (i.e: To half the screen size). I've trie开发者_JAVA技巧d setting the frame of the reader's view in viewDidLoad, but it resizes itself. I know this is going to be one of those really simple things I just missed, but any help would be much appreciated. Cheers.
EDIT: I got it to work. Here's my code:
ZBarReaderViewController *reader = [ZBarReaderViewController new];
reader.readerDelegate = self;
ZBarImageScanner *scanner = reader.scanner;
[reader setShowsZBarControls:NO];
[reader.readerView setScanCrop:(CGRect){ { 0, 0 }, { 0.43, 1 } }];
[reader.readerView start];
[self.view addSubview:reader.view];
overlayView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[listTableView setFrame:CGRectMake(0, 208, 320, 208)];
[overlayView addSubview:listTableView];
[self.view addSubview:overlayView];
Instead of using ZBarReaderViewController, try using ZBarReaderView. This worked for me and saved my lot of time. Hope it helps you.
ZBarReaderView*reader = [ZBarReaderView new];
ZBarImageScanner * scanner = [ZBarImageScanner new];
[scanner setSymbology: ZBAR_I25
config: ZBAR_CFG_ENABLE
to: 0];
reader = [reader initWithImageScanner:scanner];
reader.readerDelegate = self;
reader.tracksSymbols = YES;
reader.frame = CGRectMake(20, 38, 283, 347);
reader.torchMode = 0;
dispatch_async(dispatch_get_main_queue(), ^{[reader start];});
[self.view addSubview:reader];
I worked it out. This is what I had to do:
- Add ZBarReaderViewController's view as subview of my own view (which annoyingly fills the entire view no matter what its frame is).
- Change scan size of ZBarReaderViewController to whatever size I want it to be (BEWARE: Setting this frame is not like setting one normally, just ask if you need help).
- Add any views that you want visible to the ZBarReaderViewController's overlay view.
This was very difficult and unintuitive and broke many of Apple's code design guidelines but, in the end, is still doable.
You can create your own view and view controller,and add the ZBarReaderViewController's view as a subview of your own view;
another way to modify the properties of the scanning view controller is to import the ZBarSDK project and compile and link it yourself, rather than using the binary version of the SDK. Then, you can make any changes to the view controller that you need to (keep in mind their license...should probably read that first)
Try this It may help you:
ZBarReaderViewController *reader= [ZBarReaderViewController new];
reader.readerDelegate = self;
reader.supportedOrientationsMask = ZBarOrientationMaskAll;
ZBarImageScanner *scanner = reader.scanner;
// TODO: (optional) additional reader configuration here
// reader.showsCameraControls = NO; // for UIImagePickerController
reader.showsZBarControls = NO;
// EXAMPLE: disable rarely used I2/5 to improve performance
[scanner setSymbology:ZBAR_I25|ZBAR_QRCODE
config: ZBAR_CFG_ENABLE
to: 0];
[reader viewDidLoad];
[reader viewWillAppear:NO];
[reader viewDidAppear:NO];
[self.viewScan addSubview:reader.view];
here,self.viewScan
is any view of your current controller.
so scanning area now self.viewScan
view.
The best way to do it so it will be inside a sampleView :
UIView *view = [self sampleView];
CALayer *viewLayer = [view layer];
[viewLayer setMasksToBounds:YES];
CGRect bounds = [view bounds];
[reader.view.layer setFrame:bounds];
[viewLayer insertSublayer:reader.view.layer below:[[viewLayer sublayers] objectAtIndex:0]];
精彩评论