How do I use the scanCrop property of a ZBar reader?

我的未来我决定 提交于 2019-12-09 05:42:24

问题


I am using the ZBar SDK for iPhone in order to scan a barcode. I want the reader to scan only a specific rectangle instead of the whole view, for doing that it is needed to set the scanCrop property of the reader to the desired rectangle.

I'm having hard time with understanding the rectangle parameter that has to be set.

Can someone please tell me what rect should I give as an argument if on portrait view its coordinates would be: CGRectMake( A, B, C, D )?


回答1:


From the zbar's ZBarReaderView Class documentation :

CGRect scanCrop

The region of the video image that will be scanned, in normalized image coordinates. Note that the video image is in landscape mode (default {{0, 0}, {1, 1}})

The coordinates for all of the arguments is in a normalized float, which is from 0 - 1. So, in normalized value, theView.width is 1.0, and theView.height is 1.0. Therefore, the default rect is {{0,0},{1,1}}.

So for example, if I have a transparent UIView named scanView as a scanning region for my readerView. Rather than do :

readerView.scanCrop = scanView.frame;

We should do this, normalizing every arguments first :

CGFloat x,y,width,height;  
x = scanView.frame.origin.x / readerView.bounds.size.width;
y = scanView.frame.origin.y / readerView.bounds.size.height;
width = scanView.frame.size.width / readerView.bounds.size.width;
height = scanView.frame.size.height / readerView.bounds.size.height;

readerView.scanCrop = CGRectMake(x, y, width, height);

It works for me. Hope that helps.




回答2:


You can use scan crop area by doing this. reader.scanCrop = CGRectMake(x,y,width,height); for eg. reader.scanCrop = CGRectMake(.25,0.25,0.5,0.45); I used this and its working for me.




回答3:


come on!!! this is the right way to adjust the crop area;

I had wasted tons of time on it;

readerView.scanCrop = [self getScanCrop:cropRect readerViewBounds:contentView.bounds]; - (CGRect)getScanCrop:(CGRect)rect readerViewBounds:(CGRect)rvBounds{ CGFloat x,y,width,height; x = rect.origin.y / rvBounds.size.height; y = 1 - (rect.origin.x + rect.size.width) / rvBounds.size.width; width = rect.size.height / rvBounds.size.height; height = rect.size.width / rvBounds.size.width; return CGRectMake(x, y, width, height); }



来源:https://stackoverflow.com/questions/6970350/how-do-i-use-the-scancrop-property-of-a-zbar-reader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!