vista una silueta recortes recorte recortar previa pantalla mac imagenes imagen hacer fotos eliminar editar cómo como captura ios avcapturesession

ios - una - gimp recortar silueta



Cómo recortar una imagen de AVCapture a un rect visto en la pantalla (4)

Esto me está volviendo loco porque no puedo hacer que funcione. Tengo el siguiente escenario:

Estoy usando una AVCaptureSession y una AVCaptureVideoPreviewLayer para crear mi propia interfaz de cámara. La interfaz muestra un rectángulo. A continuación se muestra AVCaptureVideoPreviewLayer que llena toda la pantalla.

Quiero que la imagen capturada se recorte de una manera, que la imagen resultante muestre exactamente el contenido que se ve en el rect en la pantalla.

Mi configuración se ve así:

_session = [[AVCaptureSession alloc] init]; AVCaptureSession *session = _session; session.sessionPreset = AVCaptureSessionPresetPhoto; AVCaptureDevice *camera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; if (camera == nil) { [self showImagePicker]; _isSetup = YES; return; } AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session]; captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; captureVideoPreviewLayer.frame = self.liveCapturePlaceholderView.bounds; [self.liveCapturePlaceholderView.layer addSublayer:captureVideoPreviewLayer]; NSError *error; AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:camera error:&error]; if (error) { HGAlertViewWrapper *av = [[HGAlertViewWrapper alloc] initWithTitle:kFailedConnectingToCameraAlertViewTitle message:kFailedConnectingToCameraAlertViewMessage cancelButtonTitle:kFailedConnectingToCameraAlertViewCancelButtonTitle otherButtonTitles:@[kFailedConnectingToCameraAlertViewRetryButtonTitle]]; [av showWithBlock:^(NSString *buttonTitle){ if ([buttonTitle isEqualToString:kFailedConnectingToCameraAlertViewCancelButtonTitle]) { [self.delegate gloameCameraViewControllerDidCancel:self]; } else { [self setupAVSession]; } }]; } [session addInput:input]; NSDictionary *options = @{ AVVideoCodecKey : AVVideoCodecJPEG }; _stillImageOutput = [[AVCaptureStillImageOutput alloc] init]; [_stillImageOutput setOutputSettings:options]; [session addOutput:_stillImageOutput]; [session startRunning]; _isSetup = YES;

Estoy capturando la imagen de esta manera:

[_stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) { if (error) { MWLogDebug(@"Error capturing image from camera. %@, %@", error, [error userInfo]); _capturePreviewLayer.connection.enabled = YES; } else { NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]; UIImage *image = [[UIImage alloc] initWithData:imageData]; CGRect cropRect = [self createCropRectForImage:image]; UIImage *croppedImage;// = [self cropImage:image toRect:cropRect]; UIGraphicsBeginImageContext(cropRect.size); [image drawAtPoint:CGPointMake(-cropRect.origin.x, -cropRect.origin.y)]; croppedImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); self.capturedImage = croppedImage; [_session stopRunning]; } }];

En el método createCropRectForImage: he intentado varias formas de calcular el rect para cortar la imagen, pero sin éxito hasta el momento.

- (CGRect)createCropRectForImage:(UIImage *)image { CGPoint maskTopLeftCorner = CGPointMake(self.maskRectView.frame.origin.x, self.maskRectView.frame.origin.y); CGPoint maskBottomRightCorner = CGPointMake(self.maskRectView.frame.origin.x + self.maskRectView.frame.size.width, self.maskRectView.frame.origin.y + self.maskRectView.frame.size.height); CGPoint maskTopLeftCornerInLayerCoords = [_capturePreviewLayer convertPoint:maskTopLeftCorner fromLayer:self.maskRectView.layer.superlayer]; CGPoint maskBottomRightCornerInLayerCoords = [_capturePreviewLayer convertPoint:maskBottomRightCorner fromLayer:self.maskRectView.layer.superlayer]; CGPoint maskTopLeftCornerInDeviceCoords = [_capturePreviewLayer captureDevicePointOfInterestForPoint:maskTopLeftCornerInLayerCoords]; CGPoint maskBottomRightCornerInDeviceCoords = [_capturePreviewLayer captureDevicePointOfInterestForPoint:maskBottomRightCornerInLayerCoords]; float x = maskTopLeftCornerInDeviceCoords.x * image.size.width; float y = (1 - maskTopLeftCornerInDeviceCoords.y) * image.size.height; float width = fabsf(maskTopLeftCornerInDeviceCoords.x - maskBottomRightCornerInDeviceCoords.x) * image.size.width; float height = fabsf(maskTopLeftCornerInDeviceCoords.y - maskBottomRightCornerInDeviceCoords.y) * image.size.height; return CGRectMake(x, y, width, height); }

Esa es mi versión actual, pero ni siquiera tiene las proporciones correctas. ¡Alguien podría ayudarme por favor!

También he intentado usar este método para recortar mi imagen:

- (UIImage*)cropImage:(UIImage*)originalImage toRect:(CGRect)rect{ CGImageRef imageRef = CGImageCreateWithImageInRect([originalImage CGImage], rect); CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef); CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef); CGContextRef bitmap = CGBitmapContextCreate(NULL, rect.size.width, rect.size.height, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo); if (originalImage.imageOrientation == UIImageOrientationLeft) { CGContextRotateCTM (bitmap, radians(90)); CGContextTranslateCTM (bitmap, 0, -rect.size.height); } else if (originalImage.imageOrientation == UIImageOrientationRight) { CGContextRotateCTM (bitmap, radians(-90)); CGContextTranslateCTM (bitmap, -rect.size.width, 0); } else if (originalImage.imageOrientation == UIImageOrientationUp) { // NOTHING } else if (originalImage.imageOrientation == UIImageOrientationDown) { CGContextTranslateCTM (bitmap, rect.size.width, rect.size.height); CGContextRotateCTM (bitmap, radians(-180.)); } CGContextDrawImage(bitmap, CGRectMake(0, 0, rect.size.width, rect.size.height), imageRef); CGImageRef ref = CGBitmapContextCreateImage(bitmap); UIImage *resultImage=[UIImage imageWithCGImage:ref]; CGImageRelease(imageRef); CGContextRelease(bitmap); CGImageRelease(ref); return resultImage; }

¿Alguien tiene la "combinación correcta" de métodos para hacer que esto funcione? :)


AVMakeRectWithAspectRatioInsideRect esta api es de AVFoundation, devolverá la región de recorte para la imagen dado el tamaño del cultivo.


En Swift 3:

private func cropToPreviewLayer(originalImage: UIImage) -> UIImage { let outputRect = previewLayer.metadataOutputRectOfInterest(for: previewLayer.bounds) var cgImage = originalImage.cgImage! let width = CGFloat(cgImage.width) let height = CGFloat(cgImage.height) let cropRect = CGRect(x: outputRect.origin.x * width, y: outputRect.origin.y * height, width: outputRect.size.width * width, height: outputRect.size.height * height) cgImage = cgImage.cropping(to: cropRect)! let croppedUIImage = UIImage(cgImage: cgImage, scale: 1.0, orientation: originalImage.imageOrientation) return croppedUIImage }


He resuelto este problema utilizando la función metadataOutputRectOfInterestForRect .

Funciona con cualquier orientación.

[_stillImageOutput captureStillImageAsynchronouslyFromConnection:stillImageConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) { if (error) { [_delegate cameraView:self error:@"Take picture failed"]; } else { NSData *jpegData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]; UIImage *takenImage = [UIImage imageWithData:jpegData]; CGRect outputRect = [_previewLayer metadataOutputRectOfInterestForRect:_previewLayer.bounds]; CGImageRef takenCGImage = takenImage.CGImage; size_t width = CGImageGetWidth(takenCGImage); size_t height = CGImageGetHeight(takenCGImage); CGRect cropRect = CGRectMake(outputRect.origin.x * width, outputRect.origin.y * height, outputRect.size.width * width, outputRect.size.height * height); CGImageRef cropCGImage = CGImageCreateWithImageInRect(takenCGImage, cropRect); takenImage = [UIImage imageWithCGImage:cropCGImage scale:1 orientation:takenImage.imageOrientation]; CGImageRelease(cropCGImage); } } ];

La imagen tomada es imagen dependiente de imageOrientation . Puede eliminar información de orientación para un mayor procesamiento de imágenes.

UIGraphicsBeginImageContext(takenImage.size); [takenImage drawAtPoint:CGPointZero]; takenImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();


echa un vistazo a this biblioteca, podría ayudarte a lograr lo que quieres :)