【问题标题】:Custom camera iOS自定义相机 iOS
【发布时间】:2012-03-12 13:25:10
【问题描述】:

我有一个应用程序,我尝试在其中实现自定义相机。这是源代码:

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;

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

有什么帮助吗?我可以以其他方式旋转,或者我应该不这样拍照吗?

【问题讨论】:

    标签: ios uiimage rotation avcapturesession avcapture


    【解决方案1】:

    我找到了解决这个问题的方法:

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

    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;
    

    【讨论】:

      【解决方案2】:

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

      试试这个代码,它会帮助你..

      - (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;
      
      }
      

      【讨论】:

        猜你喜欢
        • 2016-04-04
        • 2015-11-15
        • 2019-08-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-16
        • 2016-04-05
        • 1970-01-01
        相关资源
        最近更新 更多