我有一个应用程序,我试图实现自定义摄像头。这是源代码:

AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];

    captureVideoPreviewLayer.frame = self.vImagePreview.bounds;
    [self.vImagePreview.layer addSublayer:captureVideoPreviewLayer];

    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    //device.position ;
    NSError *error = nil;
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
    [session addInput:input];
    [session startRunning];

    stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
    NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
    [stillImageOutput setOutputSettings:outputSettings];

    [session addOutput:stillImageOutput];
. 然后我试图创建照片并将其发送到其他视图控制器:
-(IBAction) captureNow
{
    AVCaptureConnection *videoConnection = nil;
    for (AVCaptureConnection *connection in stillImageOutput.connections)
    {
        for (AVCaptureInputPort *port in [connection inputPorts])
        {
            if ([[port mediaType] isEqual:AVMediaTypeVideo] )
            {
                videoConnection = connection;
                break;
            }
        }
        if (videoConnection) { break; }
    }

    //NSLog(@"about to request a capture from: %@", stillImageOutput);

    AcceptingPhotoViewController *photo = [[AcceptingPhotoViewController alloc] initWithNibName:@"AcceptingPhotoViewController" bundle:nil];

    [stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
     {
         CFDictionaryRef exifAttachments = CMGetAttachment( imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);

         NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
         UIImage *image = [[UIImage alloc] initWithData:imageData];

         photo.image = [[UIImage alloc] init ];
         photo.image = image;
         photo.photoFromCamera = YES;

         [self.navigationController pushViewController:photo animated:NO];
     }];
}
.

但在我的目标类中,此图像在90度上旋转到左侧:

我试图旋转它:

float newSide = MAX([image size].width, [image size].height);
                    CGSize size =  CGSizeMake(newSide, newSide);
                    UIGraphicsBeginImageContext(size);
                    CGContextRef ctx = UIGraphicsGetCurrentContext();
                    CGContextTranslateCTM(ctx, newSide/2, newSide/2);
                    CGContextRotateCTM(ctx, 1.57079633);
                    CGContextDrawImage(UIGraphicsGetCurrentContext(),CGRectMake(-[image size].width/2,-[image size].height/2,size.width, size.height),image.CGImage);
                    UIImage *i = UIGraphicsGetImageFromCurrentImageContext();
                    UIGraphicsEndImageContext();
                    image = i;
.

我的图像在右侧旋转,但它显示镜像和拉伸。

任何帮助?我可以以其他方式旋转,或者也许我应该拍照不喜欢这个?

有帮助吗?

解决方案

我发现解决这个问题:

我以这种方式旋转和变换图像

UIView* rotatedViewBox = [[UIView alloc] initWithFrame: CGRectMake(0, 0, image.size.width, image.size.height)];
                    float angleRadians = 90 * ((float)M_PI / 180.0f);
                    CGAffineTransform t = CGAffineTransformMakeRotation(angleRadians);
                    rotatedViewBox.transform = t;
                    CGSize rotatedSize = rotatedViewBox.frame.size;

                UIGraphicsBeginImageContext(rotatedSize);
                CGContextRef bitmap = UIGraphicsGetCurrentContext();
                CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
                CGContextRotateCTM(bitmap, angleRadians);

                CGContextScaleCTM(bitmap, 1.0, -1.0);
                CGContextDrawImage(bitmap, CGRectMake(-image.size.width / 2, -image.size.height / 2, image.size.width, image.size.height), [image CGImage]);

                UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
                UIGraphicsEndImageContext();
                image = newImage;

                CGSize newSize = CGSizeMake(image.size.height, image.size.width);
                UIGraphicsBeginImageContext(newSize);

                // Tell the old image to draw in this new context, with the desired
                // new size
                [image drawInRect:CGRectMake(0,0,image.size.height,image.size.width)];

                // Get the new image from the context
                UIImage* newImage2 = UIGraphicsGetImageFromCurrentImageContext();

                // End the context
                UIGraphicsEndImageContext();

                image = newImage2;
.

其他提示

我也可能同样的问题只是复制和粘贴。

尝试这段代码它将有助于u ..

- (UIImage *)fixrotation:(UIImage *)image{
    if (image.imageOrientation == UIImageOrientationDown) return image;
CGAffineTransform transform = CGAffineTransformIdentity;

switch (image.imageOrientation) {
    case UIImageOrientationDown:
    case UIImageOrientationDownMirrored:
        transform = CGAffineTransformTranslate(transform, image.size.width, image.size.height);
        transform = CGAffineTransformRotate(transform, M_PI);
        break;

    case UIImageOrientationLeft:
    case UIImageOrientationLeftMirrored:
        transform = CGAffineTransformTranslate(transform, image.size.width, 0);
        transform = CGAffineTransformRotate(transform, M_PI_2);
        break;

    case UIImageOrientationRight:
    case UIImageOrientationRightMirrored:
        transform = CGAffineTransformTranslate(transform, 0, image.size.height);
        transform = CGAffineTransformRotate(transform, -M_PI_2);
        break;
    case UIImageOrientationUp:
    case UIImageOrientationUpMirrored:
        break;
}

switch (image.imageOrientation) {
    case UIImageOrientationUpMirrored:
    case UIImageOrientationDownMirrored:
        transform = CGAffineTransformTranslate(transform, image.size.width, 0);
        transform = CGAffineTransformScale(transform, -1, 1);
        break;

    case UIImageOrientationLeftMirrored:
    case UIImageOrientationRightMirrored:
        transform = CGAffineTransformTranslate(transform, image.size.height, 0);
        transform = CGAffineTransformScale(transform, -1, 1);
        break;
    case UIImageOrientationUp:
    case UIImageOrientationDown:
    case UIImageOrientationLeft:
    case UIImageOrientationRight:
        break;
}

// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
CGContextRef ctx = CGBitmapContextCreate(NULL, image.size.width, image.size.height,
                                         CGImageGetBitsPerComponent(image.CGImage), 0,
                                         CGImageGetColorSpace(image.CGImage),
                                         CGImageGetBitmapInfo(image.CGImage));
CGContextConcatCTM(ctx, transform);
switch (image.imageOrientation) {
    case UIImageOrientationLeft:
    case UIImageOrientationLeftMirrored:
    case UIImageOrientationRight:
    case UIImageOrientationRightMirrored:
        // Grr...
        CGContextDrawImage(ctx, CGRectMake(0,0,image.size.height,image.size.width), image.CGImage);
        break;

    default:
        CGContextDrawImage(ctx, CGRectMake(0,0,image.size.width,image.size.height), image.CGImage);
        break;
}

// And now we just create a new UIImage from the drawing context
CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
UIImage *img = [UIImage imageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);
return img;

}
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top