项目开发涉及到图片扫描(Moodstocks),鉴于网上比较少此类的开发文章,顺手写下开发步骤方便以后开发。
Moodstocks官网 : https://moodstocks.com/apps/
参考官方文档 : https://moodstocks.com/docs/ios/getting-started-small-image-database/
次例子项目 是直接扫描图片读取信息,如果要点击屏幕或点击按钮扫描图片读取信息则要根据官方另一文档开发。
下载官方项目例子 : https://moodstocks.com/downloads/
1、搭建开发环境,首先下载SDK : https://moodstocks.com/docs/ios/getting-started-small-image-database/
2、导入必要Frameworks:
3、在项目targets -》build settings -》Linking-》other linker flags改成-Objc。
4、环境已搭建好先bulid一下 确认项目不报错,下面直接上代码:
1)、在AppDelegate.h中导入
#import <Moodstocks/Moodstocks.h>
@property(nonatomic,strong)MSScanner *scanner;
2)、在AppDelegate.m中
#import "AppDelegate.h"
#define MS_API_KEY @"yxqbi2coobqbyc9xwrj8"
#define MS_API_SECRET @"1hFcyEnktFOLzObq"
//API KEY
//yxqbi2coobqbyc9xwrj8
//API SECRET
//1hFcyEnktFOLzObq
@interface AppDelegate ()
//{
// MSScanner *_scanner;
//}
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSString *path = [MSScannercachesPathFor:@"scanner.db"];
_scanner = [[MSScanneralloc] init];
[_scanneropenWithPath:path key:MS_API_KEYsecret:MS_API_SECRETerror:nil];
// [_scanner syncInBackground];
// Create the progression and completion blocks:
void (^completionBlock)(MSSync *,NSError *) = ^(MSSync *op,NSError *error) {
if (error)
NSLog(@"Sync failed with error: %@", [errorms_message]);
else
NSLog(@"Sync succeeded (%li images(s))", (long)[_scannercount:nil]);
};
void (^progressionBlock)(NSInteger) = ^(NSInteger percent) {
NSLog(@"Sync progressing: %li%%", (long)percent);
};
// Launch the synchronization
[_scannersyncInBackgroundWithBlock:completionBlock progressBlock:progressionBlock];
return YES;
}
- (void)applicationWillTerminate:(UIApplication *)application {
[_scannerclose:nil];
}
3)、在ViewController.h中
#import <Moodstocks/Moodstocks.h>
@propertyMSScanner *scanner;
4)、在ViewController.m中
#import "ViewController.h"
#include "AppDelegate.h"
#import "MBProgressHUD.h"
@interface ViewController () <MSAutoScannerSessionDelegate,UIActionSheetDelegate>
{
MSAutoScannerSession *_scannerSession;
}
@property (weak) IBOutlet UIView *videoPreview;(满屏的view)
@property (nonatomic,strong) AppDelegate *appDelegate;
@end
@implementation ViewController
staticint kMSResultTypes = MSResultTypeImage |
MSResultTypeQRCode |
MSResultTypeEAN13;
- (void)viewDidLoad {
[superviewDidLoad];
_appDelegate = (AppDelegate *)[[UIApplicationsharedApplication] delegate];
self.scanner = _appDelegate.scanner;
_scannerSession = [[MSAutoScannerSessionalloc] initWithScanner:_scanner];
_scannerSession.resultTypes =kMSResultTypes;
_scannerSession.delegate =self;
CALayer *videoPreviewLayer = [self.videoPreviewlayer];
[videoPreviewLayersetMasksToBounds:YES];
CALayer *captureLayer = [_scannerSessioncaptureLayer];
[captureLayersetFrame:[self.videoPreviewbounds]];
[videoPreviewLayerinsertSublayer:captureLayer
below:[[videoPreviewLayer sublayers] objectAtIndex:0]];
[_scannerSessionstartRunning];
}
-(void)viewWillAppear:(BOOL)animated
{
[self.viewaddSubview:self.videoPreview];
}
- (void)dealloc
{
[_scannerSessionstopRunning];
}
- (void)sessionWillStartServerRequest:(id)scannerSession
{
MBProgressHUD *hud = [MBProgressHUDshowHUDAddedTo:self.viewanimated:YES];
hud.labelText =@"Searching...";
}
- (void)session:(id)scannerSession didFindResult:(MSResult *)result
{
[MBProgressHUDhideHUDForView:self.viewanimated:YES];
NSLog(@"-----==----- %@",result);
NSString *title = [result type] == MSResultTypeImage ? @"Image" : @"Barcode";
NSString *message = [NSStringstringWithFormat:@"%@:\n%@", title, [resultstring]];
UIActionSheet *aSheet = [[UIActionSheetalloc] initWithTitle:message
delegate:self
cancelButtonTitle:@"OK"
destructiveButtonTitle:nil
otherButtonTitles:nil];
[aSheetshowInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
[_scannerSessionresumeProcessing];
}
//旋转
//- (void)updateInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
//{
// [_scannerSession setInterfaceOrientation:interfaceOrientation];
//
// AVCaptureVideoPreviewLayer *captureLayer = (AVCaptureVideoPreviewLayer *) [_scannerSession captureLayer];
//
// captureLayer.frame = self.view.bounds;
//
// // AVCapture orientation is the same as UIInterfaceOrientation
// switch (interfaceOrientation) {
// case UIInterfaceOrientationPortrait:
// [[captureLayer connection] setVideoOrientation:AVCaptureVideoOrientationPortrait];
// break;
// case UIInterfaceOrientationPortraitUpsideDown:
// [[captureLayer connection] setVideoOrientation:AVCaptureVideoOrientationPortraitUpsideDown];
// break;
// case UIInterfaceOrientationLandscapeLeft:
// [[captureLayer connection] setVideoOrientation:AVCaptureVideoOrientationLandscapeLeft];
// break;
// case UIInterfaceOrientationLandscapeRight:
// [[captureLayer connection] setVideoOrientation:AVCaptureVideoOrientationLandscapeRight];
// break;
// default:
// break;
// }
//}
完成以上步骤,项目编译直接扫描图片就可以出来信息了。
附:
如果是在iOS7 版本一下环境下开发的项目,Xcode升级后在项目在iOS7运行会报错
解决办法:在项目targets -》build settings -》Build Options-》Enable Bitcode 的属性YES改成NO,如下图
转载请注明出处:http://blog.csdn.net/sevenquan/article/details/50684981