Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
PBJVisionUtilities, move methods into the utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
piemonte committed Jan 19, 2017
1 parent 3b3b888 commit 4bee173
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 78 deletions.
79 changes: 1 addition & 78 deletions Source/PBJVision.m
Expand Up @@ -1493,47 +1493,6 @@ - (BOOL)canCapturePhoto
return [self isCaptureSessionActive] && !_flags.changingModes && isDiskSpaceAvailable;
}

- (UIImage *)_uiimageFromJPEGData:(NSData *)jpegData
{
CGImageRef jpegCGImage = NULL;
CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)jpegData);

UIImageOrientation imageOrientation = UIImageOrientationUp;

if (provider) {
CGImageSourceRef imageSource = CGImageSourceCreateWithDataProvider(provider, NULL);
if (imageSource) {
if (CGImageSourceGetCount(imageSource) > 0) {
jpegCGImage = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);

// extract the cgImage properties
CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL);
if (properties) {
// set orientation
CFNumberRef orientationProperty = CFDictionaryGetValue(properties, kCGImagePropertyOrientation);
if (orientationProperty) {
NSInteger exifOrientation = 1;
CFNumberGetValue(orientationProperty, kCFNumberIntType, &exifOrientation);
imageOrientation = [self _imageOrientationFromExifOrientation:exifOrientation];
}

CFRelease(properties);
}

}
CFRelease(imageSource);
}
CGDataProviderRelease(provider);
}

UIImage *image = nil;
if (jpegCGImage) {
image = [[UIImage alloc] initWithCGImage:jpegCGImage scale:1.0 orientation:imageOrientation];
CGImageRelease(jpegCGImage);
}
return image;
}

- (UIImage *)_thumbnailJPEGData:(NSData *)jpegData
{
CGImageRef thumbnailCGImage = NULL;
Expand Down Expand Up @@ -1593,42 +1552,6 @@ - (UIImage *)_squareImageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize
return newImage;
}

// http://sylvana.net/jpegcrop/exif_orientation.html
- (UIImageOrientation)_imageOrientationFromExifOrientation:(NSInteger)exifOrientation
{
UIImageOrientation imageOrientation = UIImageOrientationUp;

switch (exifOrientation) {
case 2:
imageOrientation = UIImageOrientationUpMirrored;
break;
case 3:
imageOrientation = UIImageOrientationDown;
break;
case 4:
imageOrientation = UIImageOrientationDownMirrored;
break;
case 5:
imageOrientation = UIImageOrientationLeftMirrored;
break;
case 6:
imageOrientation = UIImageOrientationRight;
break;
case 7:
imageOrientation = UIImageOrientationRightMirrored;
break;
case 8:
imageOrientation = UIImageOrientationLeft;
break;
case 1:
default:
// UIImageOrientationUp;
break;
}

return imageOrientation;
}

- (void)_willCapturePhoto
{
DLog(@"will capture photo");
Expand Down Expand Up @@ -1770,7 +1693,7 @@ - (void)_processImageWithPhotoSampleBuffer:(CMSampleBufferRef)photoSampleBuffer
photoDict[PBJVisionPhotoJPEGKey] = jpegData;

// add image
UIImage *image = [self _uiimageFromJPEGData:jpegData];
UIImage *image = [PBJVisionUtilities uiimageFromJPEGData:jpegData];
if (image) {
photoDict[PBJVisionPhotoImageKey] = image;
} else {
Expand Down
3 changes: 3 additions & 0 deletions Source/PBJVisionUtilities.h
Expand Up @@ -38,8 +38,11 @@

+ (CMSampleBufferRef)createOffsetSampleBufferWithSampleBuffer:(CMSampleBufferRef)sampleBuffer withTimeOffset:(CMTime)timeOffset;

+ (UIImage *)uiimageFromJPEGData:(NSData *)jpegData;

// orientation

+ (UIImageOrientation)uiimageOrientationFromExifOrientation:(NSInteger)exifOrientation;
+ (CGFloat)angleOffsetFromPortraitOrientationToOrientation:(AVCaptureVideoOrientation)orientation;

// storage
Expand Down
79 changes: 79 additions & 0 deletions Source/PBJVisionUtilities.m
Expand Up @@ -26,6 +26,8 @@
#import "PBJVisionUtilities.h"
#import "PBJVision.h"

#import <ImageIO/ImageIO.h>

@implementation PBJVisionUtilities

+ (AVCaptureDevice *)captureDeviceForPosition:(AVCaptureDevicePosition)position
Expand Down Expand Up @@ -99,6 +101,83 @@ + (CMSampleBufferRef)createOffsetSampleBufferWithSampleBuffer:(CMSampleBufferRef
return offsetSampleBuffer;
}

+ (UIImage *)uiimageFromJPEGData:(NSData *)jpegData
{
CGImageRef jpegCGImage = NULL;
CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)jpegData);

UIImageOrientation imageOrientation = UIImageOrientationUp;

if (provider) {
CGImageSourceRef imageSource = CGImageSourceCreateWithDataProvider(provider, NULL);
if (imageSource) {
if (CGImageSourceGetCount(imageSource) > 0) {
jpegCGImage = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);

// extract the cgImage properties
CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL);
if (properties) {
// set orientation
CFNumberRef orientationProperty = CFDictionaryGetValue(properties, kCGImagePropertyOrientation);
if (orientationProperty) {
NSInteger exifOrientation = 1;
CFNumberGetValue(orientationProperty, kCFNumberIntType, &exifOrientation);
imageOrientation = [PBJVisionUtilities uiimageOrientationFromExifOrientation:exifOrientation];
}

CFRelease(properties);
}

}
CFRelease(imageSource);
}
CGDataProviderRelease(provider);
}

UIImage *image = nil;
if (jpegCGImage) {
image = [[UIImage alloc] initWithCGImage:jpegCGImage scale:1.0 orientation:imageOrientation];
CGImageRelease(jpegCGImage);
}
return image;
}

// http://sylvana.net/jpegcrop/exif_orientation.html
+ (UIImageOrientation)uiimageOrientationFromExifOrientation:(NSInteger)exifOrientation
{
UIImageOrientation imageOrientation = UIImageOrientationUp;

switch (exifOrientation) {
case 2:
imageOrientation = UIImageOrientationUpMirrored;
break;
case 3:
imageOrientation = UIImageOrientationDown;
break;
case 4:
imageOrientation = UIImageOrientationDownMirrored;
break;
case 5:
imageOrientation = UIImageOrientationLeftMirrored;
break;
case 6:
imageOrientation = UIImageOrientationRight;
break;
case 7:
imageOrientation = UIImageOrientationRightMirrored;
break;
case 8:
imageOrientation = UIImageOrientationLeft;
break;
case 1:
default:
// UIImageOrientationUp;
break;
}

return imageOrientation;
}

+ (CGFloat)angleOffsetFromPortraitOrientationToOrientation:(AVCaptureVideoOrientation)orientation
{
CGFloat angle = 0.0;
Expand Down

0 comments on commit 4bee173

Please sign in to comment.