2013-05-22 71 views
1

我一直在搜索互聯網,並一直在尋找任何類型的代碼來幫助我放大使用ZXing條形碼的高低。Zxing在ios AVFoundation中的縮放功能

我開始用的代碼從他們的網站的git這裏

https://github.com/zxing/zxing

從那時起,我已經能夠增加默認分辨率1920×1080。

self.captureSession.sessionPreset = AVCaptureSessionPreset1920x1080;

這將是很好,但問題是,我正在掃描非常小的條碼,即使1920x1080會工作,它不給我任何一種縮放捕捉更接近較小的條碼,而不失焦點。現在這個決議對我有很大幫助,但它不夠接近。

我想我需要做的是將捕獲會話設置爲一個1920x1080的滾動視圖,然後設置實際的圖像捕獲從我的屏幕邊界獲取,以便我可以放大和縮小滾動視圖本身就實現了「放大」類的影響。

問題是我真的不知道從哪裏開始......任何想法?

回答

2

好吧,因爲我在這裏看到了這個多次,似乎沒有人有答案。我以爲我會分享我自己的答案。

有2個屬性沒有人似乎知道。病態涵蓋了兩個。

現在,第一個適用於iOS 6+。 Apple增加了一個名爲setVideoScaleAndCropfactor的屬性。

這個設置返回一個這是一個CGFloat類型。唯一的缺點是必須將該值設置爲AVConnection,然後將連接設置爲stillImageCapture。它不適用於iOS 6中的任何其他功能。現在爲了做到這一點,您必須將其設置爲異步工作,並且必須循環使用解碼器的代碼才能在該範圍內工作並拍攝照片。

最後一件事是您必須自己縮放預覽圖層。

這一切聽起來像很多工作。它確實是。但是,這會將您的原始掃描圖片設置爲1920x1080或任何其他設置。而不是縮放當前圖像,該圖像會拉伸像素導致解碼器錯過條形碼。

SP,這將是這個樣子

stillImageConnection = [stillImageOutput connectionWithMediaType:AVMediaTypeVideo]; 
[stillImageConnection setVideoOrientation:AVCaptureVideoOrientationPortrait]; 
[stillImageConnection setVideoScaleAndCropFactor:effectiveScale]; 
[stillImageOutput setOutputSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCMPixelFormat_32BGRA] 
                   forKey:(id)kCVPixelBufferPixelFormatTypeKey]]; 
[stillImageOutput captureStillImageAsynchronouslyFromConnection:stillImageConnection 
               completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) 
{ 
    if(error) 
     return; 
    NSString *path = [NSString stringWithFormat:@"%@%@", 
         [[NSBundle mainBundle] resourcePath], 
         @"/blank.wav"]; 
    SystemSoundID soundID; 
    NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO]; 
    AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID); 
    AudioServicesPlaySystemSound(soundID); 
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(imageDataSampleBuffer); 
    /*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); 

    Decoder* d = [[Decoder alloc] init]; 
    [self decoding:d withimage:&capture]; 
}]; 

} 

現在在iOS的7未來將改變一切,我剛纔說的第二個。您有一個名爲videoZoomFactor的新屬性。這是一個CGFloat。然而,它改變了堆棧頂部的所有內容,而不僅僅是影響像靜止圖像捕捉。

在其他詞中,您不必手動縮放預覽圖層。你不必經過一些stillimagecaptureloop,你不必將其設置爲AVConnection。您只需設置CGFloat併爲您縮放所有內容。

現在我知道它會有一段時間才能發佈iOS 7應用程序。所以我會認真思考如何以困難的方式做到這一點。快速提示。我會使用捏和縮放手勢來設置您的CGFloat setvideoscaleandcropfactor。不要忘記在你的didload中將值設置爲1,並且可以從那裏進行縮放。同時在您的手勢中,您可以使用它來執行CATransaction以縮放預覽圖層。

繼承人怎麼辦姿勢捕獲和預覽層

- (IBAction)handlePinchGesture:(UIPinchGestureRecognizer *)recognizer 
    { 
effectiveScale = recognizer.scale; 
if (effectiveScale < 1.0) 
    effectiveScale = 1.0; 
if (effectiveScale > 25) 
    effectiveScale = 25; 

stillImageConnection = [stillImageOutput connectionWithMediaType:AVMediaTypeVideo]; 
[stillImageConnection setVideoScaleAndCropFactor:effectiveScale]; 

[CATransaction begin]; 
[CATransaction setAnimationDuration:0]; 
[prevLayer setAffineTransform:CGAffineTransformMakeScale(effectiveScale, effectiveScale)]; 
[CATransaction commit]; 

} 

樣品希望這可以幫助別人了!我可以繼續,只是爲了這個視頻教程。取決於我想要的是什麼樣的需求。