1 |
<br><br><br> //因图片pick_bg.png是设计成iphone使用的, 我这里偷点小懒,只将imageview放大成两倍,以供ipad使用,如果iphone上想使用,将下列宏定义为1.0f即可<br><br>#define kRatia 2.0f |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110 |
<em id = "__mceDel" >- ( IBAction )scanningButtonClicked:( id )sender { ZBarReaderViewController * reader = [ZBarReaderViewController new ]; UIImageView *imageview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@ "pick_bg.png" ]]; CGSize center = [UIScreen mainScreen].bounds.size; CGRect imageviewRect = imageview.frame; imageviewRect.size.width = imageviewRect.size.width * kRatia; imageviewRect.size.height = imageviewRect.size.height * kRatia; [imageview setFrame:imageviewRect]; [imageview setCenter:CGPointMake(center.width/2, center.height/2)]; [reader.view addSubview:imageview]; reader.readerDelegate = self ; //如果想要快速读取到维码信息,质量高的,更能快速识别 // [reader setVideoQuality:UIImagePickerControllerQualityTypeHigh]; reader.scanCrop = [ self
getScanCrop:imageview.frame readerViewBounds:[UIScreen mainScreen].bounds];<br> //扫描的感应框 reader.supportedOrientationsMask = ZBarOrientationMaskAll; ZBarImageScanner *scanner = reader.scanner; [scanner setSymbology: ZBAR_I25 config: ZBAR_CFG_ENABLE to: 0]; UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(30, 10, 220*kRatia, 2*kRatia)]; line.image = [UIImage imageNamed:@ "line.png" ]; [imageview addSubview:line]; //定时器 lineTimer = [ NSTimer
scheduledTimerWithTimeInterval:.01 target: self
selector: @selector (animation1:) userInfo:line repeats: YES ];<br> //为框添加阴影 [ self
addShadow:reader withImageWith:imageview.frame.size.width]; [ self
presentViewController:reader animated: NO
completion:^{ }]; } /** * 绿色线条扫瞄动画 * * @param timer 传递image-》by userInfo */ -( void )animation1:( NSTimer
*)timer { UIImageView *lineImage = timer.userInfo; UIImageView *frameImage = (UIImageView*)lineImage.superview; CGRect frameRect = frameImage.frame; CGRect lineRect = lineImage.frame; CGFloat y = lineRect.origin.y; lineRect.origin.y = fmodf((y + 1.0f * kRatia),frameRect.size.height-3.0f); //+frameImage.frame.origin.y; [lineImage setFrame:lineRect]; } /** * 设置扫瞄区域 * * @param rect 需要扫瞄区域 * @param readerViewBounds 整个屏幕区域 * * @return 单位化的frame//因确切的rect 会导致扫瞄不出来,所以需要将rect扩大一点,能够增强灵敏度 */ -(CGRect)getScanCrop:(CGRect)rect readerViewBounds:(CGRect)readerViewBounds { CGFloat x,y,width,height; x = (rect.origin.x-100) / readerViewBounds.size.width; y = (rect.origin.y) / readerViewBounds.size.height; width = (rect.size.width+200) / readerViewBounds.size.width; height = (rect.size.height+100) / readerViewBounds.size.height; CGRect croprect = CGRectMake(x, y, width, height); return
croprect; } /** * 添加扫描区域阴影部分 * @param imageWidth 图片宽度 */ -( void )addShadow:(ZBarReaderViewController *)reader withImageWith:(CGFloat)imageWidth { //最上部view CGSize frameSize = [UIScreen mainScreen].bounds.size; float
imageHalfWidth = imageWidth / 2.0f;<br> //顶端view的frame CGRect upviewFrame = CGRectMake(0, 0, frameSize.width, frameSize.height/2 - imageHalfWidth); float
upY = upviewFrame.size.height; float
upWidth = frameSize.width/2 - imageHalfWidth; float
upHeight = frameSize.height-2 * upY; //左侧的view CGRect leftViewFrame = CGRectMake(0, upY ,upWidth , upHeight); //右侧的view CGRect rightViewRect = CGRectMake(frameSize.width/2 + imageHalfWidth, upY, upWidth, upHeight); //底部view CGRect bottomViewRect = CGRectMake(0, upY + upHeight, frameSize.width ,upY ); [ self
addViewWithFrame:bottomViewRect inZBarController:reader]; [ self
addViewWithFrame:rightViewRect inZBarController:reader]; [ self
addViewWithFrame:leftViewFrame inZBarController:reader]; [ self
addViewWithFrame:upviewFrame inZBarController:reader]; } -( void )addViewWithFrame:(CGRect)frame inZBarController:(ZBarReaderViewController *)reader { UIView * greyView = [[UIView alloc] initWithFrame:frame]; greyView.alpha = 0.3; greyView.backgroundColor = [UIColor blackColor]; [reader.view addSubview:greyView]; } </em> |
想在zbar库中找到能够设置阴影的方法,目前没有成功,不知zbar里有无集成方式可以设置阴影,如知道,请告诉我一声,谢谢!
附上所使用的小框和绿色小条在code4app的一个demo中
ipad中zbar的使用+为zbar添加阴影+扫描线,布布扣,bubuko.com
原文:http://www.cnblogs.com/binglin92/p/3598753.html