在好例子网,分享、交流、成长!
您当前所在位置:首页C/C++ 开发实例iPhone手机开发 → iOS条形码二维码识别项目源码

iOS条形码二维码识别项目源码

iPhone手机开发

下载此实例
  • 开发语言:C/C++
  • 实例大小:0.32M
  • 下载次数:20
  • 浏览次数:702
  • 发布时间:2014-04-15
  • 实例类别:iPhone手机开发
  • 发 布 人:skipple
  • 文件格式:.zip
  • 所需积分:2
 相关标签: 二维码 条形码

实例介绍

【实例简介】
【实例截图】

【核心代码】

//
//  BarcodeDecodeViewController.m
//  Barcode
//
//  Created by Stefan Hafeneger on 22.09.08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "BarcodeDecodeViewController.h"

#import <AudioToolbox/AudioServices.h>

#import "BarcodeAppDelegate.h"
#import "ResultManager.h"

@interface BarcodeDecodeViewController ()
#pragma mark Properties
@property(nonatomic, retain) BarcodeEngine *barcodeEngine;
@property(nonatomic, retain) UIImage *image;
@property(nonatomic, retain) NSString *result;
@property(nonatomic, retain) UIImageView *imageView;
@property(nonatomic, getter=isActivity) BOOL activity;
@property(nonatomic, getter=isSaved) BOOL saved;
#pragma mark Private
- (void)updateInterface;
- (void)pickImageWithSourceType:(UIImagePickerControllerSourceType)sourceType;
@end

@implementation BarcodeDecodeViewController

#pragma mark Allocation

- (id)initWithBarcodeType:(BarcodeType)type {
	self = [super initWithNibName:@"BarcodeDecodeViewController" bundle:nil];
	if(self != nil) {
		
		switch(type) {
			case BarcodeTypeDataMatrix:
				self.title = NSLocalizedString(@"DataMatrix", nil);
				break;
			case BarcodeTypeQRCode:
				self.title = NSLocalizedString(@"QR Code", nil);
				break;
		}
		
		self.barcodeEngine = [BarcodeEngine barcodeEngineWithBarcodeType:type];
		self.barcodeEngine.delegate = self;
		
		self.image = nil;
		self.result = nil;
		self.imageView = nil;
		self.activity = NO;
		self.saved = NO;
		
	}
	return self;
}

- (void)dealloc {
	self.barcodeEngine = nil;
	self.image = nil;
	self.result = nil;
	self.imageView = nil;
	[super dealloc];
}

#pragma mark Properties;

@synthesize barcodeEngine = _barcodeEngine;
@synthesize image = _image;
@synthesize result = _result;
@synthesize imageView = _imageView;
@synthesize activity = _activity;
@synthesize saved = _saved;

#pragma mark Inherent

- (void)viewDidLoad {
	
	// Clear text view.
	textView.text = @"";
	
	// Setup buttons and labels.
	[buttonDecode setTitle:NSLocalizedString(@"Decode", nil) forState:UIControlStateNormal];
	[buttonDecode setTitle:NSLocalizedString(@"Decode", nil) forState:UIControlStateHighlighted];
	[buttonDecode setTitle:NSLocalizedString(@"Decode", nil) forState:UIControlStateDisabled];
	activityLabel.text = NSLocalizedString(@"Decoding...", nil);
	[buttonCancel setTitle:NSLocalizedString(@"Cancel", nil) forState:UIControlStateNormal];
	[buttonCancel setTitle:NSLocalizedString(@"Cancel", nil) forState:UIControlStateHighlighted];
	
	// Setup scrollView.
	scrollView.delegate = self;
	scrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
	scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(15.0f, 15.0f, 15.0f, 15.0f);
	scrollView.bouncesZoom = YES;
	
	// Update interface.
	[self updateInterface];
	
}

#pragma mark Private

- (void)updateInterface {
	if(self.isActivity) {
		if(!self.navigationItem.hidesBackButton)
			[self.navigationItem setHidesBackButton:YES animated:YES];
		buttonDecode.enabled = NO;
		buttonDecode.hidden = YES;
		activityOverlayView.hidden = NO;
		activityIndicatorView.hidden = NO;
		if(![activityIndicatorView isAnimating])
			[activityIndicatorView startAnimating];
		activityLabel.hidden = NO;
		buttonCancel.hidden = NO;
		barButtonCamera.enabled = NO;
		barButtonLibrary.enabled = NO;
		barButtonOrganize.enabled = NO;
		barButtonAction.enabled = NO;
	} else {
		if(self.navigationItem.hidesBackButton)
			[self.navigationItem setHidesBackButton:NO animated:YES];
		buttonDecode.enabled = (self.image != nil && !self.barcodeEngine.isRunning);
		buttonDecode.hidden = NO;
		activityOverlayView.hidden = YES;
		activityIndicatorView.hidden = YES;
		if([activityIndicatorView isAnimating])
			[activityIndicatorView stopAnimating];
		activityLabel.hidden = YES;
		buttonCancel.hidden = YES;
		barButtonCamera.enabled = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
		barButtonLibrary.enabled = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary];
		barButtonOrganize.enabled = (self.result != nil && !self.isSaved);
		barButtonAction.enabled = (self.result != nil);
	}
	
	BarcodeAppDelegate *applicationDelegate = (BarcodeAppDelegate *)[UIApplication sharedApplication].delegate;
	
	if(applicationDelegate.scheme != nil) {
		barButtonOrganize.enabled = NO;
		barButtonAction.enabled = NO;
	}
	
}

- (void)pickImageWithSourceType:(UIImagePickerControllerSourceType)sourceType {
	// Create new image picker controller.
	UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
	imagePickerController.allowsImageEditing = NO;
	imagePickerController.delegate = self;
	imagePickerController.sourceType = sourceType;
	imagePickerController.navigationBar.barStyle = UIBarStyleBlackOpaque;
	// Show the image picker.
	[self.navigationController presentModalViewController:imagePickerController animated:YES];
}

#pragma mark IBAction

- (IBAction)decodeImage:(id)sender {
	
	// Calculate scrollView scale factor.
	CGPoint origin = scrollView.contentOffset;
	CGSize size = scrollView.contentSize;
	CGFloat scale = self.image.size.width / size.width;
	
	// Calculate target area.
	CGRect rect = CGRectMake((origin.x   60.0f) * scale, (origin.y   26.0f) * scale, 200.0f * scale, 200.0f * scale);
	
	// Decode image.
	[self.barcodeEngine decodeImage:self.image withRect:rect];
	
}

- (IBAction)cancelDecode:(id)sender {
	[self.barcodeEngine cancelOperation];
}

- (IBAction)pickImageFromCamera:(id)sender {
	[self pickImageWithSourceType:UIImagePickerControllerSourceTypeCamera];
}

- (IBAction)pickImageFromLibrary:(id)sender {
	[self pickImageWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}

- (IBAction)organizeResult:(id)sender {
	if(!self.isSaved) {
		BarcodeAppDelegate *applicationDelegate = (BarcodeAppDelegate *)[UIApplication sharedApplication].delegate;
		[applicationDelegate addResultWithString:self.result];
		UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Saved Result." message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
		[alertView show];
		[alertView release];
		self.saved = YES;
		[self updateInterface];
	}
}

- (IBAction)showActionSheet:(id)sender {
	
	// Show action sheet for result.
	[[ResultManager sharedResultManager] showActionSheetForResult:[[ResultManager sharedResultManager] resultFromString:self.result] fromToolbar:toolbar];
	
}

#pragma mark UIScrollViewDelegate

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
	return self.imageView;
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
	
}

#pragma mark UINavigationControllerDelegate

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
	// Nothing to do yet.
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
	// Nothing to do yet.
}

#pragma mark UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)imagePickerController didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
	
	self.image = image;
	
	CGFloat scale = fmaxf(320.0f / image.size.width, 252 / image.size.height);
	
	if(self.imageView != nil)
		[self.imageView removeFromSuperview];
	UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
	imageView.frame = CGRectMake(0.0f, 0.0f, image.size.width * scale, image.size.height * scale);
	[scrollView addSubview:imageView];
	self.imageView = imageView;
	[imageView release];
	
	scrollView.contentSize = CGSizeMake(image.size.width * scale, image.size.height * scale);
	scrollView.maximumZoomScale = 1 / scale;
	scrollView.minimumZoomScale = 1.0f;
	scrollView.contentOffset = CGPointZero;
	
	self.result = nil;
	textView.text = @"";
	
	// Dismiss image picker.
	[self.navigationController dismissModalViewControllerAnimated:YES];
	[imagePickerController release];
	
	[self updateInterface];
	
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)imagePickerController {
	[self.navigationController dismissModalViewControllerAnimated:YES];
	[imagePickerController release];
}

#pragma mark BarcodeEngineDelegate

- (void)barcodeEngine:(BarcodeEngine *)barcodeEngine willDecodeImage:(UIImage *)image {
	self.result = nil;
	textView.text = nil;
	self.activity = YES;
	[self updateInterface];
}

- (void)barcodeEngine:(BarcodeEngine *)barcodeEngine didDecodeImage:(UIImage *)image withString:(NSString *)string {
	AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
	self.result = string;
	textView.text = string;
	self.activity = NO;
	self.saved = NO;
	[self updateInterface];
	
	BarcodeAppDelegate *applicationDelegate = (BarcodeAppDelegate *)[UIApplication sharedApplication].delegate;
	
	if(applicationDelegate.scheme != nil)
		[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@://barcode?status=success&result=%@&userinfo=%@", applicationDelegate.scheme, string, applicationDelegate.userinfo]]];
	
}

- (void)barcodeEngine:(BarcodeEngine *)barcodeEngine didNotDecodeImage:(UIImage *)image {
	UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Could not decode barcode!" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
	[alertView show];
	[alertView release];
	self.activity = NO;
	[self updateInterface];
	
	BarcodeAppDelegate *applicationDelegate = (BarcodeAppDelegate *)[UIApplication sharedApplication].delegate;
	
	if(applicationDelegate.scheme != nil)
		[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@://barcode?status=failed&userinfo=%@", applicationDelegate.scheme, applicationDelegate.userinfo]]];
	
}

- (void)barcodeEngineDidCancelOperation:(BarcodeEngine *)barcodeEngine {
	self.activity = NO;
	[self updateInterface];
	
	BarcodeAppDelegate *applicationDelegate = (BarcodeAppDelegate *)[UIApplication sharedApplication].delegate;
	
	if(applicationDelegate.scheme != nil)
		[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@://barcode?status=cancel&userinfo=%@", applicationDelegate.scheme, applicationDelegate.userinfo]]];
	
}

- (void)barcodeEngineDidStopOperation:(BarcodeEngine *)barcodeEngine {
	[self updateInterface];
}

@end

标签: 二维码 条形码

实例下载地址

iOS条形码二维码识别项目源码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警