在ios 中 扫瞄二维码,条形码基本有 2中第三方的库,一个是zbar 一个是zxing,zxing 在android中表现的比较出色,但是在ios 中不是很好用,扫瞄效率低,我们一般都用zbar,但是有些 条形码就是很奇葩,用zbar无法识别,下面就是一种


ios zxing扫码问题_sed

 我用了好多ios 的app 都无法识别, 《我查查》,《快拍二维码》,《微信》,自己用zbar都不行,最后用android 手机轻松扫瞄ok,哪我知道为什么了,是zxing可以搞定这种条形码。马上就换了zxing 来测试。 去github 找到了 zxing 的demo。但是悲剧的时无法识别各种条形码。

而且工程还报错。

  报Private field 'cached_y_' not used 编译通不过,解决办法就是


删除工程“buliding setting”的"Other Warning Flags" 的后面的参数:

"-Werror" , "-Wno-unused-parameter" 等等

  然后真机debug 完全ok,但是还是无法扫瞄 条形码!为什么呢?

  我在网上着了下原因 ,问题解决了。

方法是:

 1.修改 OverlayView.m文件中的61行左右

 ​​co​

注释掉下面代码


// self.oneDMode = isOneDModeEnabled;



2.在ZXingWidgetController.m中用这个函数替换以前的函数


 也就是上面红色的部分做了修改


- (void)captureOutput:(AVCaptureOutput *)captureOutput   
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection
{
if (!decoding) {
return;
}
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
/*Lock the image buffer*/
CVPixelBufferLockBaseAddress(imageBuffer,0);
/*Get information about the image*/
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);

uint8_t* baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);
void* free_me = 0;
if (true) { // iOS bug?
uint8_t* tmp = baseAddress;
int bytes = bytesPerRow*height;
free_me = baseAddress = (uint8_t*)malloc(bytes);
baseAddress[0] = 0xdb;
memcpy(baseAddress,tmp,bytes);
}

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef newContext =
CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace,
kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst);

CGImageRef capture = CGBitmapContextCreateImage(newContext);
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
free(free_me);

CGContextRelease(newContext);
CGColorSpaceRelease(colorSpace);

CGRect cropRect = [overlayView cropRect];
if (oneDMode) {
// let's just give the decoder a vertical band right above the red line
<span style="color:#ff0000;">// cropRect.origin.x = cropRect.origin.x + (cropRect.size.width / 2) - (ONE_D_BAND_HEIGHT + 1);
// cropRect.size.width = ONE_D_BAND_HEIGHT;
// // do a rotate
// CGImageRef croppedImg = CGImageCreateWithImageInRect(capture, cropRect);
// CGImageRelease(capture);
// capture = [self CGImageRotated90:croppedImg];
// capture = [self CGImageRotated180:capture];
// // UIImageWriteToSavedPhotosAlbum([UIImage imageWithCGImage:capture], nil, nil, nil);
// CGImageRelease(croppedImg);
// CGImageRetain(capture);
// cropRect.origin.x = 0.0;
// cropRect.origin.y = 0.0;</span>
cropRect.size.width = CGImageGetWidth(capture);
cropRect.size.height = CGImageGetHeight(capture);
}

// N.B.
// - Won't work if the overlay becomes uncentered ...
// - iOS always takes videos in landscape
// - images are always 4x3; device is not
// - iOS uses virtual pixels for non-image stuff

{
float height = CGImageGetHeight(capture);
float width = CGImageGetWidth(capture);

CGRect screen = UIScreen.mainScreen.bounds;
float tmp = screen.size.width;
screen.size.width = screen.size.height;;
screen.size.height = tmp;

cropRect.origin.x = (width-cropRect.size.width)/2;
cropRect.origin.y = (height-cropRect.size.height)/2;
}
CGImageRef newImage = CGImageCreateWithImageInRect(capture, cropRect);
CGImageRelease(capture);
<span style="color:#ff0000;"> // UIImage *scrn = [[UIImage alloc] initWithCGImage:newImage];
int backCameraImageOrientation = UIImageOrientationRight;
UIImage *scrn = [[UIImage alloc] initWithCGImage:newImage scale:
(CGFloat)1.0 orientation:backCameraImageOrientation];
</span>
CGImageRelease(newImage);
Decoder *d = [[Decoder alloc] init];
d.readers = readers;
d.delegate = self;
cropRect.origin.x = 0.0;
cropRect.origin.y = 0.0;
decoding = [d decodeImage:scrn cropRect:cropRect] == YES ? NO : YES;
[d release];
[scrn release];
}



3.在ViewController.mm 文件中做下面的修改


#import "MultiFormatOneDReader.h"  

- (void)pressButton1:(UIButton *)button
{
<span style="color:#ff0000;"> ZXingWidgetController *widController = [[ZXingWidgetController alloc] initWithDelegate:self showCancel:YES OneDMode:YES];
NSMutableSet *readers = [[NSMutableSet alloc] init];
QRCodeReader *qrcodeReader = [[QRCodeReader alloc] init];

MultiFormatOneDReader *OneReaders=[[MultiFormatOneDReader alloc]init];

[readers addObject:qrcodeReader];
[readers addObject:OneReaders];</span>
widController.readers = readers;
[self presentViewController:widController animated:YES completion:^{}];
}


 

​c​

然后修改全部ok了,扫瞄条形码就完全ok了。

 看效果:

ios zxing扫码问题_ios_02

 二:



ios zxing扫码问题_ios_03


三:


ios zxing扫码问题_无法识别_04


ios zxing扫码问题_ios_05

四:


ios zxing扫码问题_ios_06