How to crop scanned barcode using Zbar?

大城市里の小女人 提交于 2019-12-21 03:01:28

问题


I am using below code to scan bar code using Zbar sdk.

I go through related question of stackoverflow but that answer is not working for me.

Till now I have done,

-(IBAction)btnCLick
    {
        ZBarReaderViewController *reader = [ZBarReaderViewController new];
        reader.readerDelegate = self;
        reader.supportedOrientationsMask = ZBarOrientationMaskAll;

        ZBarImageScanner *scanner = reader.scanner;
        [scanner setSymbology:ZBAR_I25 config:ZBAR_CFG_ENABLE to:0];


        [self presentModalViewController:reader animated:YES];
        [reader release];
    }

- (void) imagePickerController: (UIImagePickerController*) aReader
     didFinishPickingMediaWithInfo: (NSDictionary*) info
    {

        id<NSFastEnumeration> results = [info objectForKey:ZBarReaderControllerResults];

        ZBarSymbol *symbol = nil;

        for(symbol in results)
        {
            break;
        }
        txtViewResult.text = symbol.data;
        imgViewResult.image = [info objectForKey:UIImagePickerControllerOriginalImage];

        [self advanceCrop:imgViewResult];
        [reader dismissModalViewControllerAnimated:YES];

    }

advanceCrop Method is doing crop,

-(void)advanceCrop:(UIImageView *)aimgViewResult
    {


        CGRect r =CGRectMake(50/426., 1-(20+250)/320., 150/426., 250/320.);
        reader.scanCrop = r;


        r = CGRectMake(20, 50, 250, 150);

        imgViewResult.frame = r;
        NSLog(@"imgview height:%f width:%f",imgViewResult.frame.size.height, imgViewResult.frame.size.width);


        /*

       // CGRect r = CGRectFromString(lblImage.text);
       CGRect r = CGRectMake(50/426., 1-(20+250)/320., 150/426., 250/320.);

        reader.scanCrop = r; 
        NSLog(@"r: x:%f  y:%f height :%f  width:%f",r.origin.x ,r.origin.y ,r.size.height , r.size.width);

       // aLblImage.text = NSStringFromCGRect(r);

        r.origin.x *= 426;
        r.origin.y *= 320;
        r.size.width *= 426;
        r.size.height *= 320;

        imgViewResult.frame = r;
        NSLog(@"imgview height:%f width:%f",imgViewResult.frame.size.height, imgViewResult.frame.size.width);
         */
    }

If I am getting Image as below and I want to crop it, is it possible with Zbar?


回答1:


Try this. If CGRect = CGRectMake(x, y, width, height);, then do:

float A = y / readerView.bounds.size.height;   
float B = 1 - (x + width) / readerView.bounds.size.width;   
float C = (y + height) / readerView.bounds.size.height;   
float D = 1 - x / readerView.bounds.size.width;;  
CGRectMake( A, B, C, D );


来源:https://stackoverflow.com/questions/8726370/how-to-crop-scanned-barcode-using-zbar

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