ZBar之自定义二维码扫描

//
//  YvanQRCodeViewController.m
//  zBar
//
//  Created by City--Online on 15/6/8.
//  Copyright (c) 2015年 CYW. All rights reserved.
//

#import "YvanQRCodeViewController.h"
#import "ZBarSDK.h"
#define SCANVIEW_EdgeTop 40.0
#define SCANVIEW_EdgeLeft 50.0
#define TINTCOLOR_ALPHA 0.2 //浅色透明度
#define DARKCOLOR_ALPHA 0.5 //深色透明度
#define VIEW_WIDTH self.view.bounds.size.width
#define VIEW_HEIGHT self.view.bounds.size.height

@interface YvanQRCodeViewController ()<ZBarReaderViewDelegate>
{
    UIView *_QrCodeline;
    NSTimer *_timer;
    //设置扫描画面
    UIView *_scanView;
    ZBarReaderView *_readerView;
}
@end

@implementation YvanQRCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"扫描二维码" ;
    //初始化扫描界面
    [ self setScanView ];
    _readerView = [[ ZBarReaderView alloc ] init ];
    _readerView.frame = CGRectMake ( 0 , 0 , VIEW_WIDTH , VIEW_HEIGHT );
    _readerView.tracksSymbols = NO ;
    _readerView.readerDelegate = self ;
    [ _readerView addSubview : _scanView ];
    //关闭闪光灯
    _readerView.torchMode = 0 ;
    [ self.view addSubview : _readerView ];
    //扫描区域
    //readerView.scanCrop =
    [ _readerView start ];
    [ self createTimer ];

}
#pragma mark -- ZBarReaderViewDelegate
-( void )readerView:( ZBarReaderView *)readerView didReadSymbols:( ZBarSymbolSet *)symbols fromImage:( UIImage *)image
{
    const zbar_symbol_t *symbol = zbar_symbol_set_first_symbol (symbols. zbarSymbolSet );
    NSString *symbolStr = [ NSString stringWithUTF8String : zbar_symbol_get_data (symbol)];
    //判断是否包含 头'http:'
    NSString *regex = @"http+:[^//s]*" ;
    NSPredicate *predicate = [ NSPredicate predicateWithFormat : @"SELF MATCHES %@" ,regex];
    UIAlertView *alertView=[[ UIAlertView alloc ] initWithTitle : @"" message :symbolStr delegate : nil cancelButtonTitle : @"取消" otherButtonTitles : nil ];
    [alertView show ];
    //判断是否包含 头'ssid:'
    NSString *ssid = @"ssid+:[^//s]*" ;
    NSPredicate *ssidPre = [ NSPredicate predicateWithFormat : @"SELF MATCHES %@" ,ssid];
    if ([predicate evaluateWithObject :symbolStr]) {
    }
    else if ([ssidPre evaluateWithObject :symbolStr]){
        NSArray *arr = [symbolStr componentsSeparatedByString : @";" ];
        NSArray * arrInfoHead = [[arr objectAtIndex : 0 ] componentsSeparatedByString : @":" ];
        NSArray * arrInfoFoot = [[arr objectAtIndex : 1 ] componentsSeparatedByString : @":" ];
        symbolStr = [ NSString stringWithFormat : @"ssid: %@ /n password:%@" ,
                     [arrInfoHead objectAtIndex : 1 ],[arrInfoFoot objectAtIndex : 1 ]];
        UIPasteboard *pasteboard=[ UIPasteboard generalPasteboard ];
        //然后,可以使用如下代码来把一个字符串放置到剪贴板上:
        pasteboard.string = [arrInfoFoot objectAtIndex : 1 ];
    }

}
- ( void )setScanView
{
    _scanView =[[ UIView alloc ] initWithFrame : CGRectMake ( 0 , 0 , VIEW_WIDTH , VIEW_HEIGHT )];
    _scanView . backgroundColor =[ UIColor clearColor ];
    //最上部view
    UIView * upView = [[ UIView alloc ] initWithFrame : CGRectMake ( 0 , 0 , VIEW_WIDTH , SCANVIEW_EdgeTop )];
    upView. alpha = TINTCOLOR_ALPHA ;
    upView. backgroundColor = [ UIColor blackColor ];
    [ _scanView addSubview :upView];
    //左侧的view
    UIView *leftView = [[ UIView alloc ] initWithFrame : CGRectMake ( 0 , SCANVIEW_EdgeTop , SCANVIEW_EdgeLeft , VIEW_WIDTH - 2 * SCANVIEW_EdgeLeft )];
    leftView. alpha = TINTCOLOR_ALPHA ;
    leftView. backgroundColor = [ UIColor blackColor ];
    [ _scanView addSubview :leftView];
    /******************中间扫描区域****************************/
    UIImageView *scanCropView=[[ UIImageView alloc ] initWithFrame : CGRectMake ( SCANVIEW_EdgeLeft , SCANVIEW_EdgeTop , VIEW_WIDTH - 2 * SCANVIEW_EdgeLeft , VIEW_WIDTH - 2 * SCANVIEW_EdgeLeft )];
    //scanCropView.image=[UIImage imageNamed:@""];
    scanCropView. layer . borderColor =[ UIColor  yellowColor]. CGColor ;
    scanCropView. layer . borderWidth = 2.0 ;
    scanCropView. backgroundColor =[ UIColor clearColor ];
    [ _scanView addSubview :scanCropView];
    //右侧的view
    UIView *rightView = [[ UIView alloc ] initWithFrame : CGRectMake ( VIEW_WIDTH - SCANVIEW_EdgeLeft , SCANVIEW_EdgeTop , SCANVIEW_EdgeLeft , VIEW_WIDTH - 2 * SCANVIEW_EdgeLeft )];
    rightView. alpha = TINTCOLOR_ALPHA ;
    rightView. backgroundColor = [ UIColor blackColor ];
    [ _scanView addSubview :rightView];
    //底部view
    UIView *downView = [[ UIView alloc ] initWithFrame : CGRectMake ( 0 , VIEW_WIDTH - 2 * SCANVIEW_EdgeLeft + SCANVIEW_EdgeTop , VIEW_WIDTH , VIEW_HEIGHT -( VIEW_WIDTH - 2 * SCANVIEW_EdgeLeft + SCANVIEW_EdgeTop )- 64 )];
    //downView.alpha = TINTCOLOR_ALPHA;
    downView. backgroundColor = [[ UIColor blackColor ] colorWithAlphaComponent : TINTCOLOR_ALPHA ];
    [ _scanView addSubview :downView];
    //用于说明的label
    UILabel *labIntroudction= [[ UILabel alloc ] init ];
    labIntroudction. backgroundColor = [ UIColor clearColor ];
    labIntroudction. frame = CGRectMake ( 0 , 5 , VIEW_WIDTH , 20 );
    labIntroudction. numberOfLines = 1 ;
    labIntroudction. font =[ UIFont systemFontOfSize : 15.0 ];
    labIntroudction. textAlignment = NSTextAlignmentCenter ;
    labIntroudction. textColor =[ UIColor whiteColor ];
    labIntroudction. text = @"将二维码对准方框,即可自动扫描" ;
    [downView addSubview :labIntroudction];
    UIView *darkView = [[ UIView alloc ] initWithFrame : CGRectMake ( 0 , downView. frame . size . height - 100.0 , VIEW_WIDTH , 100.0 )];
    darkView. backgroundColor = [[ UIColor blackColor ]  colorWithAlphaComponent : DARKCOLOR_ALPHA ];
    [downView addSubview :darkView];
    //用于开关灯操作的button
    UIButton *openButton=[[ UIButton alloc ] initWithFrame : CGRectMake ( 10 , 20 , 300.0 , 40.0 )];
    [openButton setTitle : @"开启闪光灯" forState: UIControlStateNormal ];
    [openButton setTitleColor :[ UIColor whiteColor ] forState : UIControlStateNormal ];
    openButton. titleLabel . textAlignment = NSTextAlignmentCenter ;
    openButton. backgroundColor =[ UIColor greenColor ];
    openButton. titleLabel . font =[ UIFont systemFontOfSize : 22.0 ];
    [openButton addTarget : self action : @selector (openLight) forControlEvents : UIControlEventTouchUpInside ];
    [darkView addSubview :openButton];
    //画中间的基准线
    _QrCodeline = [[ UIView alloc ] initWithFrame : CGRectMake ( SCANVIEW_EdgeLeft , SCANVIEW_EdgeTop , VIEW_WIDTH - 2 * SCANVIEW_EdgeLeft , 2 )];
    _QrCodeline . backgroundColor = [ UIColor yellowColor];
    [ _scanView addSubview : _QrCodeline ];
}
- ( void )openLight
{
    if ( _readerView.torchMode == 0 ) {
        _readerView.torchMode = 1 ;
    } else
    {
        _readerView.torchMode = 0 ;
    }
}
- ( void )viewWillDisappear:( BOOL )animated
{
    [ super viewWillDisappear :animated];
    if ( _readerView . torchMode == 1 ) {
        _readerView . torchMode = 0 ;
    }
    [ self stopTimer ];
    [ _readerView stop ];
}
//二维码的横线移动
- ( void )moveUpAndDownLine
{
    CGFloat Y= _QrCodeline.frame.origin.y ;
    //CGRectMake(SCANVIEW_EdgeLeft, SCANVIEW_EdgeTop, VIEW_WIDTH-2*SCANVIEW_EdgeLeft, 1)]
    if (VIEW_WIDTH- 2 *SCANVIEW_EdgeLeft+SCANVIEW_EdgeTop==Y){
        [UIView beginAnimations: @"asa" context: nil ];
        [UIView setAnimationDuration: 1 ];
        _QrCodeline.frame=CGRectMake(SCANVIEW_EdgeLeft, SCANVIEW_EdgeTop, VIEW_WIDTH- 2 *SCANVIEW_EdgeLeft, 1 );
        [UIView commitAnimations];
    } else if (SCANVIEW_EdgeTop==Y){
        [UIView beginAnimations: @"asa" context: nil ];
        [UIView setAnimationDuration: 1 ];
        _QrCodeline.frame=CGRectMake(SCANVIEW_EdgeLeft, VIEW_WIDTH- 2 *SCANVIEW_EdgeLeft+SCANVIEW_EdgeTop, VIEW_WIDTH- 2 *SCANVIEW_EdgeLeft, 1 );
        [UIView commitAnimations];
    }
}
- ( void )createTimer
{
    //创建一个时间计数
    _timer=[NSTimer scheduledTimerWithTimeInterval: 1.0 target: self selector: @selector (moveUpAndDownLine) userInfo: nil repeats: YES ];
}
- ( void )stopTimer
{
    if ([_timer isValid] == YES ) {
        [_timer invalidate];
        _timer = nil ;
    }
}

// 是否支持转屏
- (BOOL)shouldAutorotate
{
    return YES;
}

// 支持的屏幕方向,此处可直接返回 UIInterfaceOrientationMask 类型
// 也可以返回多个 UIInterfaceOrientationMask 取或运算后的值
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (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.
}
*/

@end

时间: 2024-10-26 22:00:05

ZBar之自定义二维码扫描的相关文章

二维码扫描工具和开发包 ZBar

问题描述 ZBar 是款桌面电脑用条形码/二维码扫描工具,支持摄像头及图片扫描,支持多平台包括 iPhone 手机.同时 ZBar 提供了二维码扫描的 API 开发包.KJFrameForAndroid.part1.rar**ZBar** 目前支持扫描,除了 Windows 平台外,还支持 Linux 及 iPhone 平台.可扫描以下类型,常见的都有.EAN-13/UPC-A, UPC-E, EAN-8, Code 128, Code 39, Interleaved 2 of 5 and QR

Qt之二维码扫描

简述 二维码(QR Code)是用某种特定的几何图形按一定规律在平面(二维方向)分布的黑白相间的图形记录数据符号信息的.是所有信息数据的一把钥匙.应用十分广泛,如:产品防伪/溯源.广告推送.网站链接.数据下载.商品交易.定位/导航.电子凭证.车辆管理.信息传递.名片交流.wifi共享等. 二维条码常用的码制: Data Matrix.MaxiCode.Aztec.QR Code.Vericode.PDF417.Ultracode.Code 49.Code 16K等. 简述 二维码与一维码 二维码

摄像头-ZBar二维码扫描,无法连续点击扫描

问题描述 ZBar二维码扫描,无法连续点击扫描 大概第三次的时候,进入摄像头的速度特别的慢..如何解决? 附上代码: CameraViewController *camera=[CameraViewController new] ; [self presentViewController:camera animated:YES completion:nil]; [camera release]; camera 这部分是用ZBarReaderView自定义的界面... 解决方案 ZBar 二维码扫

ios ZBar 二维码扫描。。。。

问题描述 ios ZBar 二维码扫描.... ld: warning: directory not found for option '-LZBarSDK' ld: '/Users/**/Documents/**/**/ios7 Scan QRCode/ZBarSDK-for-iOS-master/libzbar.a(ZBarReaderController.o)' does not contain bitcode. You must rebuild it with bitcode enabl

Android开发框架之自定义ZXing二维码扫描界面并解决取景框拉伸问题_Android

先给大家展示下效果图: 扫描内容是下面这张,二维码是用zxing库生成的 由于改了好几个类,还是去年的事都忘得差不多了,所以只能上这个类的代码了,主要就是改了这个CaptureActivity.java package com.zxing.activity; import java.io.IOException; import java.util.Vector; import android.app.Activity; import android.content.Intent; import

ios-IOS二维码扫描。。。。。。。。。。

问题描述 IOS二维码扫描.......... 希望有大神能给我分享一份完整的IOS二维码扫描DEMO,功能需求(带有打开手机相册,以及在手机中选择二维码图片识别功能,以及打开闪光灯效果,再加外部二维码图片扫描识别),求好心人啊 ,一定要帮我 解决方案 ? ? 现在很多项目中可能会用到二维码扫描.工作的时候.开发群里的朋友会问一下ios二维码扫描问题.比如编译报错.缺少系统库,等等.iOS目前扫描二维码码主要用Zxing,ZBar或者系统自带的(ios7).在系统之前的没有集成二维码的时候.主要

ios-IOS二维码扫描。。。。。。。。。

问题描述 IOS二维码扫描......... 调用打开系统相册,能从相册中识别二维码图片进行解析,以及系统灯光调用和外部扫描二维码,只求有好心人能分享demo,新人在此谢过了 解决方案 wobuzhidao sjhsadawkdowld,,ccccccc 解决方案二: ? ? 现在很多项目中可能会用到二维码扫描.工作的时候.开发群里的朋友会问一下ios二维码扫描问题.比如编译报错.缺少系统库,等等.iOS目前扫描二维码码主要用Zxing,ZBar或者系统自带的(ios7).在系统之前的没有集成二

iOS学习之二维码扫描

这几天刚好将本人高仿新浪微博的事情进行一个阶段性的tag,在此也将这个项目在实现二维码扫描这个功能来做一个简要的记录.关于高仿新浪微博的源代码,本人已经将全部代码托管到github,地址在这里.欢迎大家指正.接下来我们来看看QRCode扫描的实现. 关于二维码的扫描,网上说的最多的框架莫过于ZBar和ZXing框架,本人刚开始也是想使用第三方框架,但是,在iOS7之后苹果官方已经提供了二维码扫描方法,而且实现起来还是蛮简单的,故而选择原生态方法.不过,我们还是来看看,如何使用ZBar实现二维码扫

ZXing二维码扫描

一.  二维码 1.二维码名片 它是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的,在代码编制上巧妙地利用构成计算机内部逻辑基础的"0"."1"比特流的概念,使用若干个与二进制相对应的几何形体来表示文字数值信息,通过图象输入设备或光电扫描设备自动识读以实现信息自动处理.它具有条码技术的一些共性:每种码制有其特定的字符集:每个字符占有一定的宽度:具有一定的校验功能等.同时还具有对不同行的信息自动识别功能.及处理图形旋转变化等特