2011-11-16 1 views
9

AVFoundation 카메라의 줌 기능으로 작업하고 있습니다. AVCaptureVideoPreviewLayer가있는 뷰의 크기를 조정하여 줌 기능을 구현했습니다. 이제 확대 된 이미지를 캡처하고 싶습니다.AVFoundation에서 확대 된 미리보기 뷰 캡쳐

// zoom the preview view using core graphics 
[previewView setTransform:CGAffineTransformMakeScale(2.0, 2.0)]; 

지금 내가 previewView에서 사전에

덕분에이 확대 된 이미지를 캡처 할

// create a uiview subclass for showing the camera feed 
UIView *previewView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 430)]; 
[[self view] addSubview:previewView]; 

CGRect layerRect = CGRectMake(0, 0, 320, 430); 
[[self avCaptureVideoPreviewLayer] setBounds:layerRect]; 

[[self previewLayer] setPosition:CGPointMake(CGRectGetMidX(layerRect), CGRectGetMidY(layerRect))]; 

// add the video previewview layer to the preview view 
[[previewView layer] addSublayer:[self avCaptureVideoPreviewLayer]]; 

코드 미리보기를 확대하기위한 : 여기

은보기에 AVFoundationVideoPreviewLayer를 추가하는 내 코드입니다 .

답변

7

마지막으로 내 앱에서 확대/축소 된 사진 캡처 기능을 추가 할 수있었습니다. 조금 추한지만 작동합니다. UIImage + 그물에서 코드 크기를 조정 한 다음 줌 배율로 이미지 크기를 조정하고 확대 된 영역 [카메라 뷰에서 볼 수있는]에 대한 오프셋 위치를 계산 한 다음 잘라 냈습니다.

지금 내 줌 비율이 최대, 즉 6 배 일 때 내 앱이 충돌하는 경우가 있습니다. 확대 이미지가 너무 커서 메모리 문제가 나타납니다. 다른 질문으로이 질문을 할 것입니다.

이 코드를 사용하는 것이 유용 할 수 있습니다.

if (cameraZoom > 1.0) 
{ 
     // save the images original size 
     CGSize orgSize = [image size]; 

     // resize the image to the zoom scale 
     image = [image resizedImage:CGSizeMake(image.size.width * cameraZoom, image.size.height *cameraZoom) interpolationQuality:kCGInterpolationNone];   

     // now calculate the offset x and offset y 
     CGFloat offsetX = (image.size.width/2) - (orgSize.width /2) ; 
     CGFloat offsetY = (image.size.height/2) - (orgSize.height /2); 

     // crop the image from the offset position to the original width and height 
     image = [image croppedImage:CGRectMake(offsetX, offsetY, orgSize.width, orgSize.height)]; 
    } 


    UIButton *imgBtn = (UIButton *)[self.view viewWithTag:500]; 
    [imgBtn setImage:image forState:UIControlStateNormal]; 
+0

다른 어떤 더 나은 솔루션이 있습니까? – kadungon

+1

충돌과 관련하여 : 먼저 메모리 문제를 해결하기 위해 크기 조정보다 자르기 만하면됩니다. 대부분의 이미지를 나중에 삭제하면 큰 이미지를 계산하지 마십시오. – CipherCom

+0

나는 당신에게 코드를 시도했지만 cameraZoom을 이해할 수 없었고 결과는 항상 빈 이미지였다. 더 공유 할 수 있습니까? –

2

당신은 크기를 조정 한 다음 메모리 문제가되지 않습니다 그런 식으로 첫 번째 작물을 시도해야한다.

당신이 지금하고있는 일은 다음과 같습니다 : 600x600 이미지는 6x 일 때 3600X3600이되고, 그런 다음 자르면 다시 600X600이됩니다. 하지만 6X의 자르기로 600X600을 시도하면 100X100이되고 600X600으로 다시 크기를 조정하면됩니다.

원본 해상도가 낮 으면 두 프로세스의 출력 이미지가 조금씩 다를 수 있지만 원래 해상도가 8MP/5MP/3MP (최고 5/4s/4/3G)이면 출력이 거의 비슷할 수 있습니다 .

더 나은 결과를 얻으려면 저해상도에서 처리하고 고해상도에서는 위에서 제안한 것과 반대로 사용하십시오.

0

자르기를 먼저 수행 한 다음 코어 그래픽 이미지 변환을 사용하여 확대/축소 할 수있었습니다. 원래의 이미지 크기와 스케일을 이미지 버퍼에 유지하고 개별 픽셀의 변환 작업을 통해 줌 기능을 완전히 수행합니다.

UIGraphicsBeginImageContextWithOptions(imageSize,NO,1.0); // this will crop 

CGContextRef context = UIGraphicsGetCurrentContext(); 


CGRect newRect = CGRectMake(0, 0, width, height); 

// apply a scaling transform to zoom 
// rotate about the center 
// First we scale, then rotate, then translate 
// actual matrix multiplication steps are applied in reverse when using 
// right-handed coordinate system 
// M' = M * translatation * rotation * scale 
CGContextTranslateCTM(context, (width - scaledWidth)*0.5f, 
           (height-scaledHeight)*0.5f); 
CGContextScaleCTM(context,scaleFactor,scaleFactor); 

[sourceImage drawInRect:newRect]; 

newImage = UIGraphicsGetImageFromCurrentImageContext(); 
if (newImage == nil) { 
    NSLog(@"could not scale image"); 
} 

// pop the context to get back to the default 
UIGraphicsEndImageContext(); 
4

을 나는 이미지 규모와 작물을 설정 해요 :이 코드 변환 (내 경우에는 내가 단지 똑바로 현재 초상화를 사용) 계정에 장치 방향을 고려하지 않고, 쉽게 추가 회전을 수행 할 수 있습니다 직접적으로 :

AVCaptureConnection *stillImageConnection = [_stillImageOutput.connections objectAtIndex:0]; 
[stillImageConnection setVideoScaleAndCropFactor:imagePreviewScale]; 

그것은 단지 비디오 연결을하지만 여전히 이미지도 매력처럼 작동을 위해 일하기로.

+1

이것은 올바른 대답이어야합니다.두 줄의 코드가 완벽하게 작동합니다. 감사합니다 호세! – picciano