The introduction

Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Function: Scan bank card identification information (bank name, bank card number) and capture bank card image

Application scenario: Rapid filling of bank card number, such as merchant entry and real-name authentication

Principle:

  1. Customize the camera and utilize the third-party library SDKlibexbankcardios.alibbexbankcard.aIdentify (identifyUnlimited number of times, free)

2. Add a custom scan interface (with a hollow window in the middle and a scan line moving back and forth)

Support ID card number identification

I. Scan bank cards

1.1 Introduce third-party SDKS and header files

exbankcard.h BankCard.h exbankcardcore.h libexbankcardios.a libbexbankcard.a

1.2 Obtaining Information (Decoding)

#pragmaMark -- Scanning bank cards --
- (void)parseBankImageBuffer:(CVImageBufferRef)imageBuffer {
    #if TARGET_IPHONE_SIMULATOR
    
    #else
    size_t width_t= CVPixelBufferGetWidth(imageBuffer);
    size_t height_t = CVPixelBufferGetHeight(imageBuffer);
    CVPlanarPixelBufferInfo_YCbCrBiPlanar *planar = CVPixelBufferGetBaseAddress(imageBuffer);
    size_t offset = NSSwapBigIntToHost(planar->componentInfoY.offset);

    unsigned char* baseAddress = (unsigned char *)CVPixelBufferGetBaseAddress(imageBuffer);
    unsigned char* pixelAddress = baseAddress + offset;

    size_t cbCrOffset = NSSwapBigIntToHost(planar->componentInfoCbCr.offset);
    uint8_t *cbCrBuffer = baseAddress + cbCrOffset;

    CGSize size = CGSizeMake(width_t, height_t);
    CGRect effectRect = [IDCardRectManager getEffectImageRect:size];
    CGRect rect = [IDCardRectManager getGuideFrame:effectRect];

    int width = ceilf(width_t);
    int height = ceilf(height_t);

    unsigned char result [512];
    int resultLen = BankCardNV12(result, 512, pixelAddress, cbCrBuffer, width, height, rect.origin.x, rect.origin.y, rect.origin.x+rect.size.width, rect.origin.y+rect.size.height);

    if(resultLen > 0) {

        int charCount = [IDCardRectManager docode:result len:resultLen];
        if(charCount > 0) {
         // CGRect subRect = [IDCardRectManager getCorpCardRect:width height:height guideRect:rect charCount:charCount];
        
            self.isHasResult = YES;
            if ([self.captureSession isRunning]) {
                [self.captureSession stopRunning];
            }
            UIImage *image = [UIImage getImageStream:imageBuffer];
           // __block UIImage *subImg = [UIImage getSubImage:subRect inImage:image];

            char *numbers = [IDCardRectManager getNumbers];

            NSString *numberStr = [NSString stringWithCString:numbers encoding:NSASCIIStringEncoding];
            NSString *bank = [BankCardSearch getBankNameByBin:numbers count:charCount];

             NSLog(@"\n Card number %@\n bank type %@",numberStr,bank);

            BankCardInfo *model = [JYBDBankCardInfo new];

            model.bankNumber = numberStr;
            model.bankName = bank;
            AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
            dispatch_async(dispatch_get_main_queue(), ^{
            
                if (self.finish)
                {
                    self.finish(model, image); }}); } } CVPixelBufferUnlockBaseAddress(imageBuffer,0);
    self.isInProcessing = NO;
    #endif
   
}


Copy the code

1.3 Picture Clipping


- (void)parseBankImageBuffer:(CVImageBufferRef)imageBuffer {
    size_t width_t= CVPixelBufferGetWidth(imageBuffer);
    size_t height_t = CVPixelBufferGetHeight(imageBuffer);
    CGSize size = CGSizeMake(width_t, height_t);

    CGRect effectRect = [IDCardRectManager getEffectImageRect:size];
    CGRect rect = [IDCardRectManager getGuideFrame:effectRect];

            UIImage *image = [UIImage getImageStream:imageBuffer];/ / image artwork
            __block UIImage *subImg = [UIImage getSubImage:rect inImage:image];/ / cutting the rect
}
Copy the code
  • Utility methods

+ (UIImage *)getImageStream:(CVImageBufferRef)imageBuffer {
    CIImage *ciImage = [CIImage imageWithCVPixelBuffer:imageBuffer];
    CIContext *temporaryContext = [CIContext contextWithOptions:nil];
    CGImageRef videoImage = [temporaryContext createCGImage:ciImage fromRect:CGRectMake(0.0, CVPixelBufferGetWidth(imageBuffer), CVPixelBufferGetHeight(imageBuffer))];
    
    UIImage *image = [[UIImage alloc] initWithCGImage:videoImage];
    
    CGImageRelease(videoImage);
    return image;
}

+ (UIImage *)getSubImage:(CGRect)rect inImage:(UIImage*)image {
    CGImageRef subImageRef = CGImageCreateWithImageInRect(image.CGImage, rect);
    
    CGRect smallBounds = CGRectMake(0.0.CGImageGetWidth(subImageRef), CGImageGetHeight(subImageRef));
    
    UIGraphicsBeginImageContext(smallBounds.size);
    
    CGContextRef context = UIGraphicsGetCurrentContext(a);CGContextDrawImage(context, smallBounds, subImageRef);
    
    UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
    CFRelease(subImageRef);
    
    UIGraphicsEndImageContext(a);return smallImage;
}

Copy the code

1.4 Query bank name by numbers and charCount

Kunnan.blog.csdn.net/article/det…

II FaQs

2.1 iOS13 Adaptation: Present Half-screen problem

The collection interface is displayed in full screen

Blog.csdn.net/z929118967/…

- (NSMutableArray *)FullScreenClasss{
    
    if(_FullScreenClasss == nil){

        [_FullScreenClasss addObject:@"KNAVCapture4IDfrontViewController"];
    }
    return _FullScreenClasss;
    
}

Copy the code

2.2 Undefined symbols for architecture arm64

Undefined symbols for architecture arm64:
  "_ZIM_SaveImage", referenced from:
      ImgSave(tagIMG, char const*) in libbexbankcard.a(gjimage.o)
  "_ZIM_LoadImage", referenced from:
      ImgLoad(tagIMG&, char const*) in libbexbankcard.a(gjimage.o)
  "_ZIM_DoneImage", referenced from:
      ImgLoad(tagIMG&, char const*) in libbexbankcard.a(gjimage.o)
ld: symbol(s) not found for architecture arm64

Copy the code

Solution: search for ENABLE_TESTABILITY to NO under TARGETS and PROJECT under build Settings

RN project, Dead Code Stripping set to yes

see also

From CSDN download Demo source: https://download.csdn.net/download/u011018979/19268420

If you cannot download the Demo, please pay attention to the official account: [iOS Reverse] to obtain it

Refer to the article: kunnan.blog.csdn.net/article/det…

Welcome to # Applets: iOS Reverse