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-10 10:45:40

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

iOS 开发之 ZBarSDK 二维码扫描自定义二维码扫描页面(二)

iOS 开发之 ZBarSDK 二维码扫描自定义二维码扫描页面(二) 上一篇解决了ZBarSDK不支持64bit的问题,下面我们就可以使用ZBarSDK了. 导入ZBarSDk.h文件 附上代码: // //  MeViewController.m //  Auditory Blog // //  Created by 寒竹子 on 15/4/28. //  Copyright (c) 2015年 寒竹子. All rights reserved. // #define ScanWidth  2

二维码框架ZBarSDK的使用和自定义二维码扫描界面方法

如果你不知道ZBarSDK怎么用,请下载demo http://download.csdn.net/detail/u013686641/7858917 如果你已经配置好ZBarSDK ,那么下面这个类可以直接用 下面是效果图 // //  头文件 //  TestProject // #import <UIKit/UIKit.h> #import "ZBarSDK.h" @interface yxpQrCode :UIViewController @end // //  实

ZBar 是款桌面电脑用条形码/二维码扫描工具

windows平台python 2.7环境编译安装zbar 最近一个项目需要识别二维码,找来找去找到了zbar和zxing,中间越过无数坑,总算基本上弄明白,分享出来给大家. 一.zbar官方介绍 ZBar 是款桌面电脑用条形码/二维码扫描工具,支持摄像头及图片扫描,支持多平台,例如 iPhone,Andriod 手机,同时 ZBar封装了二维码扫描的 API 开发包. ZBar 目前条码类型有:EAN-13/UPC-A, UPC-E, EAN-8, Code 128, Code 39, Int

ios 二维码扫描

https://github.com/bmorton/ZBarSDK 我是用ZBar做的二维码扫描 其他的SDK也可以 首先是导入库文件: 1.AVFoundation.framework 2.CoreMedia.framework 3.CoreVideo.framework 4.QuartzCore.framework 5.libiconv.dylib 这个5个库文件 在ViewController.h 导入#import "ZBarSDK.h" 并且 继承 <ZBarRead

Qt之二维码扫描

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

android利用zbar二维码扫描-(解决中文乱码及扫描区域定义)

写在最前(这是对上一篇博文的问题做的更新[android利用zbar二维码扫描]) project下载   zbarLib编译project  project下载0积分 bug 在2.3的系统中Holder须要设置type,否则会黑屏(感谢网友[(α ⒎待sんа)294439435]) com.example.qu.MainActivity的第50行mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 今天发现 在com.exampl

二维码扫描(ZBar,ZXing不知道有没有问题)与arm64的问题.

做二维码扫描的时候,查了一些资料,都说ZBar的功能更全面一些,于是就用了ZBar的开源代码,导入工程中,编译一直出错主要错误信息: Undefined symbols for architecture x86_64:查了一些资料,有说把TARGET→Builte Setting→Vaild Architecture 中armv7s和arm64删除只保留armv7,虽然暂时可以解决问题,但是新的问题又来了,因为苹果官方已经说了,2月份上传的App都必须支持64位,刚好arm64就是64位,删除以

android利用zbar二维码扫描

源码下载 之前用zxing做开发,各种奇葩问题,横屏修等等,而且性能也不搞.被测试批了,没办法后来换了zbar.性能好多了. 直接上图,看看效果 2.界面上的查找框 /** * 2014-7-15 上午11:14:21 * Created By niexiaoqiang */ package com.example.qu; import android.content.Context; import android.graphics.Canvas; import android.graphics

一个不同于ZBAR的二维码扫描与识别。

现在好多人使用二维码用到第三方 ZBar,往往被不支持模拟器所困扰.经过楼主的不懈努力,终于一款原生二维码扫描识别与生成工具类完成了.大家可以轻易的添加二维码的头像,改变二维码的样式(颜色修改要慎重,有时候颜色修改的不合适会导致,二维码识别失败).废话不多说直接给demo地址:https://github.com/zcs110/scanQR.git   Code4上直接搜索scanQR就可以了.