Vin
Vin

Reputation: 10548

Managing views while integrating zxing for iPhone

I am trying to integrate zxing in my already existing iPhone app. I have referred to the ScanTest example enclosed in the project download, and have succeeded in building and running the scanner.

My problem lies in managing the views of the project to incorporate the scanner. Currently I have an already existing view controller(VC-A), which needs the scanner ability. I created a new view controller(VC-B) to launch the ZXingWidgetController scanner view. So the flow looks like the following:

VC-A-> presentModalViewController(VC-B)->VC-B-> presentModalViewController(ZXingWidgetController)

Now as is in the ScanTest sample application, when the scanning is complete, a dissmissModalViewController is called from VC-B which dismisses ZXingWidgetController's view. The problem is whatever way I try, I am unable to dismiss VC-B to come back to VC-A. I have created a set of delegate methods to notify VC-A, when the scanning is completed/canceled. I get the scan data in those methods, but am unable to dismiss VC-B's view.

I don't want to modify my already existing view controller VC-A, therefore I am unable to incorporate ZXingWidgetController directly in it, discarding VC-B altogether(cause it would require making VC-A a .mm file).

EDIT: Now I am doing away with ZXingWidgetController's modal, altogether. Using this code in viewDidLoad on VC-B

//Create custom overlay
OverlayView *scannerView = [[OverlayView alloc]initWithFrame:CGRectMake(27, 107, 267, 253) cancelEnabled:NO oneDMode:NO];
scannerView.displayedMessage = nil;
widController = [[ZXingWidgetController alloc]init ];

//set the overlay of widController
[widController setOverlayView:scannerView];

QRCodeReader* qrcodeReader = [[QRCodeReader alloc] init];
NSSet *readers = [[NSSet alloc ] initWithObjects:qrcodeReader,nil];
[qrcodeReader release];
widController.readers = readers;
widController.delegate = self;
[readers release];
[scannerView release];
NSBundle *mainBundle = [NSBundle mainBundle];
widController.soundToPlay = [NSURL fileURLWithPath:[mainBundle pathForResource:@"beep-beep" ofType:@"aiff"] isDirectory:NO];

//Add scanner to the view
  [self.view addSubview:widController.view];

Now the camera view of ZXingWidgetController is not visible at all. Has anyone customized the overlay for Zxing? I don't seem to find any similar problem on SO.

Upvotes: 1

Views: 4323

Answers (2)

S-A-I
S-A-I

Reputation: 61

Just a small note to consider; on testing Mountain Lion with Xcode 4.4 I couldn't get one of my existing projects to compile because of ZBar. I can't say whether ZBar will be updated to work with Xcode 4.4 or whether something will change in Xcode 4.4 that will enable ZBar to work. Who knows!

I've updated my projects to use Zxing instead even though it is harder to get working and to configure it.

Update: I've started using ZXingObjC now (https://github.com/TheLevelUp/ZXingObjC). It's easier to get working than Zxing and it's also got a lot of the features that are missing in Zxing in it.

Upvotes: 2

Wim Fikkert
Wim Fikkert

Reputation: 106

With ZXing you can access the overlay view directly and add/change its subviews. For example, putting an imageview on top of this overview is done as follows:

ZXingWidgetController *widController = [[ZXingWidgetController alloc] initWithDelegate:self showCancel:YES OneDMode:NO];

UIImage *qrOverlayImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"overlay-image" ofType:@"png"]];

UIImageView *qrOverlayImageView = [[[UIImageView alloc] initWithImage:qrOverlayImage] autorelease];
qrOverlayImageView.contentMode = UIViewContentModeScaleAspectFit;
qrOverlayImageView.backgroundColor = [UIColor clearColor];

[widController.overlayView addSubview:qrOverlayImageView];

Use PNGs with transparency, add UILabels etc to create your custom overlay programmatically.

Upvotes: 3

Related Questions