8
0
mirror of https://gitee.com/shuto/customCamera.git synced 2026-05-21 00:00:01 +08:00

Update code to use the camera

This commit is contained in:
Christophe BOUCAUT
2015-07-08 14:04:47 +02:00
parent ad89df75c0
commit 36f3e63e09
7 changed files with 301 additions and 57 deletions
+12 -3
View File
@@ -1,4 +1,5 @@
#import <UIKit/UIKit.h>
#import "CameraParameter.h"
@interface AVCamViewController : UIViewController
{
@@ -9,8 +10,12 @@
CGRect frameBtnThumb;
UIImage *capturedImage;
NSData *capturedImageData;
UIImage *capturedImage;
NSData *capturedImageData;
BOOL isRotated;
CGFloat fDist;
}
@@ -23,9 +28,13 @@
@property (weak, nonatomic) IBOutlet UISlider *opacitySlider;
@property (nonatomic, retain) CameraParameter *params;
- (id) initWithPhoto:(NSString *)sttPhoto WithCallback:(void(^)(UIImage*))callback;
- (id) initWithParams:(CameraParameter *)parameter WithCallback:(void(^)(UIImage*))callback;
+131 -28
View File
@@ -26,7 +26,7 @@ static void * SessionRunningAndDeviceAuthorizedContext = &SessionRunningAndDevic
@property (nonatomic, weak) IBOutlet UIImageView *imgSmallThumbNail;
@property (nonatomic, weak) IBOutlet UIImageView *imgBigThumbNail;
@property (nonatomic, retain) NSString *strPhotoName;
- (IBAction)onTapThumb:(id)sender;
- (IBAction)onTapCameraFlash:(id)sender;
@@ -54,6 +54,7 @@ static void * SessionRunningAndDeviceAuthorizedContext = &SessionRunningAndDevic
@end
@implementation AVCamViewController
@synthesize params;
- (BOOL)isSessionRunningAndDeviceAuthorized
{
@@ -69,10 +70,9 @@ static void * SessionRunningAndDeviceAuthorizedContext = &SessionRunningAndDevic
- (id) initWithPhoto:(NSString *)sttPhoto WithCallback:(void(^)(UIImage*))callback {
- (id) initWithParams:(CameraParameter *)parameter WithCallback:(void(^)(UIImage*))callback {
self = [super initWithNibName:nil bundle:nil];
self.strPhotoName = sttPhoto;
self.params = parameter;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
self = [super initWithNibName:@"AVCamViewController_iPad" bundle:nil];
@@ -92,12 +92,6 @@ static void * SessionRunningAndDeviceAuthorizedContext = &SessionRunningAndDevic
{
[super viewDidLoad];
UIImage* defaultImage = [UIImage imageNamed:self.strPhotoName];
// defaultImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.strPhotoName]]];
self.imgBigThumbNail.image = defaultImage;
self.imgSmallThumbNail.image = defaultImage;
// Create the AVCaptureSession
@@ -121,18 +115,24 @@ static void * SessionRunningAndDeviceAuthorizedContext = &SessionRunningAndDevic
[self setBackgroundRecordingID:UIBackgroundTaskInvalid];
NSError *error = nil;
AVCaptureDevice *videoDevice = [AVCamViewController deviceWithMediaType:AVMediaTypeVideo preferringPosition:AVCaptureDevicePositionBack];
AVCaptureDeviceInput *videoDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
AVCaptureDevice *videoDevice;
if(params.nDefaultCamera == 0)
{
videoDevice = [AVCamViewController deviceWithMediaType:AVMediaTypeVideo preferringPosition:AVCaptureDevicePositionBack];
}
else{
videoDevice = [AVCamViewController deviceWithMediaType:AVMediaTypeVideo preferringPosition:AVCaptureDevicePositionFront];
}
AVCaptureDeviceInput *videoDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
dispatch_async(dispatch_get_main_queue(), ^{
if(![videoDevice hasTorch])
{
self.btnFlash.hidden = YES;
self.cameraButton.center = self.btnThumb.center;
self.btnThumb.center = self.btnFlash.center;
// self.cameraButton.center = self.btnThumb.center;
// self.btnThumb.center = self.btnFlash.center;
}
});
@@ -195,6 +195,8 @@ static void * SessionRunningAndDeviceAuthorizedContext = &SessionRunningAndDevic
}
@@ -242,10 +244,11 @@ static void * SessionRunningAndDeviceAuthorizedContext = &SessionRunningAndDevic
return YES;
}
- (BOOL)shouldAutorotate
{
// Disable autorotation of the interface when recording is in progress.
return ![self lockInterfaceRotation];
return ![self lockInterfaceRotation];
}
- (NSUInteger)supportedInterfaceOrientations
@@ -374,6 +377,38 @@ static void * SessionRunningAndDeviceAuthorizedContext = &SessionRunningAndDevic
AVCaptureDevice *videoDevice = [AVCamViewController deviceWithMediaType:AVMediaTypeVideo preferringPosition:preferredPosition];
AVCaptureDeviceInput *videoDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:nil];
dispatch_async(dispatch_get_main_queue(), ^{
[self.btnFlash setImage:[UIImage imageNamed:@"icon_flash_auto.png"] forState:UIControlStateNormal];
self.btnFlash.tag = 0;
if ([videoDevice hasTorch] && [videoDevice hasFlash]){
[videoDevice lockForConfiguration:nil];
[videoDevice setTorchMode:NO];
[videoDevice setFlashMode:AVCaptureFlashModeOn];
[videoDevice unlockForConfiguration];
[self.btnFlash setImage:[UIImage imageNamed:@"icon_flash_auto.png"] forState:UIControlStateNormal];
self.btnFlash.tag = 0;
self.btnFlash.hidden = NO;
return;
}
if(![videoDevice hasTorch])
{
self.btnFlash.hidden = YES;
// self.cameraButton.center = self.btnThumb.center;
// self.btnThumb.center = self.btnFlash.center;
}
else if([videoDevice hasTorch] && params.bSwitchFlash)
{
self.btnFlash.hidden = NO;
// self.btnThumb.center = self.cameraButton.center;
// self.cameraButton.center = CGPointMake(self.cameraButton.center.x - fDist, self.cameraButton.center.y);
}
});
[[self session] beginConfiguration];
@@ -622,21 +657,14 @@ static void * SessionRunningAndDeviceAuthorizedContext = &SessionRunningAndDevic
self.opacitySlider.transform = trans;
[self.opacitySlider addTarget:self action:@selector(onChangeOpacitySlider) forControlEvents:UIControlEventValueChanged];
// CGRect bounds = [[UIScreen mainScreen] bounds];
// float fX = bounds.size.width - opacitySlider.frame.size.width;
// float fHeight = bounds.size.width * 2 / 3;
// float fY = (bounds.size.height - fHeight) / 2;
// float fWidth = opacitySlider.frame.size.width;
//
//
//
// opacitySlider.frame = CGRectMake(fX, fY, fWidth, fHeight);
self.opacitySlider.value = 1;
}
-(void) initialize
{
fDist = self.btnThumb.center.x - self.cameraButton.center.x;
capturedImage = [[UIImage alloc] init];
capturedImageData = [[NSData alloc] init];
[self addOpacitySlider];
@@ -646,9 +674,81 @@ static void * SessionRunningAndDeviceAuthorizedContext = &SessionRunningAndDevic
self.saveBgPanel.hidden = YES;
self.btnDeletePicture.hidden = YES;
self.btnSaveImage.hidden = YES;
self.imgBigThumbNail.image = [UIImage imageWithData:self.params.bgImageData];
self.imgSmallThumbNail.image = [UIImage imageWithData:self.params.bgImageData];
self.btnThumb.hidden = !params.bMiniature;
self.btnFlash.hidden = !params.bSwitchFlash;
self.cameraButton.hidden = !params.bSwitchCamera;
self.opacitySlider.hidden = !params.bOpacity;
if (!params.bgImageData) {
self.imgBigThumbNail.hidden = YES;
self.imgSmallThumbNail.hidden = YES;
}
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if(params.nDefaultFlash == 1)
{
[self.btnFlash setImage:[UIImage imageNamed:@"icon_flash.png"] forState:UIControlStateNormal];
self.btnFlash.tag = 1;
if ([device hasTorch] && [device hasFlash]){
[device lockForConfiguration:nil];
[device setTorchMode:YES];
[device setFlashMode:AVCaptureFlashModeOn];
[device unlockForConfiguration];
}
}
else{
[self.btnFlash setImage:[UIImage imageNamed:@"icon_flash_auto.png"] forState:UIControlStateNormal];
self.btnFlash.tag = 0;
if ([device hasTorch] && [device hasFlash]){
[device lockForConfiguration:nil];
[device setTorchMode:NO];
[device setFlashMode:AVCaptureFlashModeOn];
[device unlockForConfiguration];
}
}
// if(!params.bSwitchFlash)
// {
// self.cameraButton.center = self.btnThumb.center;
// self.btnThumb.center = self.btnFlash.center;
// }
// if(!params.bMiniature)
// {
// self.cameraButton.center = self.btnThumb.center;
// }
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
if(params.bgImageData1)
{
isRotated = !isRotated;
if(isRotated)
{
self.imgBigThumbNail.image = [UIImage imageWithData:params.bgImageData1];
self.imgSmallThumbNail.image = [UIImage imageWithData:params.bgImageData1];
}
else{
self.imgBigThumbNail.image = [UIImage imageWithData:params.bgImageData];
self.imgSmallThumbNail.image = [UIImage imageWithData:params.bgImageData];
}
}
}
- (void) takePicture
{
[self setLockInterfaceRotation:YES];
@@ -694,7 +794,11 @@ static void * SessionRunningAndDeviceAuthorizedContext = &SessionRunningAndDevic
[self.view addSubview:activityIndicator];
[activityIndicator startAnimating];
[[[ALAssetsLibrary alloc] init] writeImageToSavedPhotosAlbum:[capturedImage CGImage] orientation:(ALAssetOrientation)[capturedImage imageOrientation] completionBlock:nil];
if(params.bSaveInGallery)
{
[[[ALAssetsLibrary alloc] init] writeImageToSavedPhotosAlbum:[capturedImage CGImage] orientation:(ALAssetOrientation)[capturedImage imageOrientation] completionBlock:nil];
}
[self.view setUserInteractionEnabled:NO];
_callback([UIImage imageWithData:capturedImageData]);
@@ -715,7 +819,6 @@ static void * SessionRunningAndDeviceAuthorizedContext = &SessionRunningAndDevic
return;
}
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
CGFloat fMaxZoomFactor = device.activeFormat.videoMaxZoomFactor;
if(fMaxZoomFactor > 5)
+31
View File
@@ -0,0 +1,31 @@
#import <Foundation/Foundation.h>
#import <Cordova/CDV.h>
@interface CameraParameter : NSObject
{
}
@property(nonatomic, retain) NSData *bgImageData;
@property(nonatomic, retain) NSData *bgImageData1;
@property(nonatomic, assign) BOOL bMiniature;
@property(nonatomic, assign) BOOL bSaveInGallery;
@property(nonatomic, assign) int nCameraFlashMode;
@property(nonatomic, retain) NSString* strCameraBGColor;
@property(nonatomic, retain) NSString* strCameraPressedBG;
@property(nonatomic, assign) CGFloat fQuality;
@property(nonatomic, assign) BOOL bOpacity;
@property(nonatomic, assign) int nDefaultFlash;
@property(nonatomic, assign) BOOL bSwitchFlash;
@property(nonatomic, assign) int nDefaultCamera;
@property(nonatomic, assign) BOOL bSwitchCamera;
-(id) initWithCommand :(CDVInvokedUrlCommand *)command;
@end
+77
View File
@@ -0,0 +1,77 @@
#import "CameraParameter.h"
@implementation CameraParameter
{
}
@synthesize bgImageData;
@synthesize bgImageData1;
@synthesize bMiniature;
@synthesize bSaveInGallery;
@synthesize nCameraFlashMode;
@synthesize strCameraBGColor;
@synthesize strCameraPressedBG;
@synthesize fQuality;
@synthesize bOpacity;
@synthesize nDefaultFlash;
@synthesize bSwitchFlash;
@synthesize nDefaultCamera;
@synthesize bSwitchCamera;
-(id) initWithCommand :(CDVInvokedUrlCommand *)command
{
if(self = [super init])
{
// imgBackgroundBase64: null, // background picture in base64.
// imgBackgroundBase64OtherOrientation: null, // background picture in base64 for second orientation. If it's not defined, imgBackgroundBase64 is used.
// miniature: true, // active or disable the miniature function.
// saveInGallery: false, // save or not the picture in gallery.
// cameraBackgroundColor: "#e26760", // color of the camera button.
// cameraBackgroundColorPressed: "#dc453d", // color of the pressed camera button.
// // To get supported color formats, go to see method parseColor : http://developer.android.com/reference/android/graphics/Color.html#parseColor(java.lang.String)
// quality: 100, // picture's quality : range 0 - 100 : http://developer.android.com/reference/android/graphics/Bitmap.html#compress(android.graphics.Bitmap.CompressFormat, int, java.io.OutputStream) (parameter "quality")
// opacity: true, // active or disable the opacity function.
// defaultFlash: this.FlashModes.DISABLE, // default state for flash. See CustomCamera.FlashModes for corrects values.
// switchFlash: true, // active or disable the switch flash button.
// defaultCamera: this.CameraFacings.BACK, // default camera used. See CustomCamera.CameraFacings for corrects values.
// switchCamera: true // active or disable the switch camera button.
NSString *strData = [command argumentAtIndex:0];
if(strData)
{
bgImageData = [[NSData alloc] initWithBase64Encoding:strData];
}
else{
bgImageData = nil;
}
NSString *strData1 = [command argumentAtIndex:1];
if(strData1)
{
bgImageData1 = [[NSData alloc] initWithBase64Encoding:strData1];
}
else{
bgImageData1 = nil;
}
bMiniature = [[command argumentAtIndex:2] boolValue];
bSaveInGallery = [[command argumentAtIndex:3] boolValue];
strCameraBGColor = [command argumentAtIndex:4];
strCameraPressedBG = [command argumentAtIndex:5];
fQuality = [[command argumentAtIndex:6] intValue];
bOpacity = [[command argumentAtIndex:7] boolValue];
nDefaultFlash = [[command argumentAtIndex:8] intValue];
bSwitchFlash = [[command argumentAtIndex:9] boolValue];
nDefaultCamera = [[command argumentAtIndex:10] intValue];
bSwitchCamera = [[command argumentAtIndex:11] boolValue];
}
return self;
}
@end
+28 -6
View File
@@ -4,14 +4,36 @@
{
CDVInvokedUrlCommand *lastCommand;
NSString *filename;
CGFloat quality;
CGFloat targetWidth;
CGFloat targetHeight;
int nDestType;
int nSourceType;
NSString* strPhotoName;
int nDestType;
NSData *bgImageData;
NSData *bgImageData1;
BOOL miniature;
BOOL saveInGallery;
int nCameraFlashMode;
NSString* clrCameraBG;
NSString* clrCameraPressedBG;
CGFloat quality;
BOOL opacity;
int defaultFlash;
BOOL switchFlash;
int defaultCamera;
BOOL switchCamera;
NSString *filename;
}
- (void)startCamera:(CDVInvokedUrlCommand*)command;
+19 -20
View File
@@ -1,23 +1,26 @@
#import "CustomCamera.h"
#import "AVCamViewController.h"
#import "CameraParameter.h"
@implementation CustomCamera
- (void)startCamera:(CDVInvokedUrlCommand*)command {
lastCommand = command;
NSString *guid = [[NSUUID new] UUIDString];
NSString *uniqueFileName = [NSString stringWithFormat:@"%@.jpg", guid];
filename = uniqueFileName; //[command argumentAtIndex:0];
quality = [[command argumentAtIndex:1] floatValue];
targetWidth = [[command argumentAtIndex:2] floatValue];
targetHeight = [[command argumentAtIndex:3] floatValue];
nDestType = [[command argumentAtIndex:4] intValue];
nSourceType = [[command argumentAtIndex:5] intValue];
filename = uniqueFileName;
nSourceType = 1;
nDestType = 0;
strPhotoName = [command argumentAtIndex:0];
CameraParameter *param = [[CameraParameter alloc] initWithCommand:lastCommand];
// NSString * strPhotoName = @"sample.png";
if(nSourceType == 0)
{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
@@ -32,13 +35,12 @@
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Camera is not accessible"];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
} else {
AVCamViewController *cameraViewController = [[AVCamViewController alloc] initWithPhoto:strPhotoName WithCallback:^(UIImage *image) {
AVCamViewController *cameraViewController = [[AVCamViewController alloc] initWithParams:param WithCallback:^(UIImage *image) {
@autoreleasepool {
if(nDestType == 0)
{
UIImage *scaledImage = [self scaleImage:image toSize:CGSizeMake(targetWidth, targetHeight)];
NSData *scaledImageData = UIImageJPEGRepresentation(scaledImage, quality / 100);
NSString* strEncodeData = [scaledImageData base64EncodedStringWithOptions:0];
NSData *imageData = UIImageJPEGRepresentation(image, quality / 100);
NSString* strEncodeData = [imageData base64EncodedStringWithOptions:0];
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsString:strEncodeData];
[self.viewController dismissViewControllerAnimated:YES completion:nil];
@@ -46,10 +48,9 @@
} else {
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:filename];
UIImage *scaledImage = [self scaleImage:image toSize:CGSizeMake(targetWidth, targetHeight)];
NSData *scaledImageData = UIImageJPEGRepresentation(scaledImage, quality / 100);
NSData *imageData = UIImageJPEGRepresentation(image, quality / 100);
//[self deleteFileWithName:imagePath];
[scaledImageData writeToFile:imagePath atomically:YES];
[imageData writeToFile:imagePath atomically:YES];
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsString:[[NSURL fileURLWithPath:imagePath] absoluteString]];
[self.viewController dismissViewControllerAnimated:YES completion:nil];
@@ -76,10 +77,9 @@
@autoreleasepool {
if(nDestType == 0)
{
UIImage *scaledImage = [self scaleImage:image toSize:CGSizeMake(targetWidth, targetHeight)];
NSData *scaledImageData = UIImageJPEGRepresentation(scaledImage, quality / 100);
NSData *imageData = UIImageJPEGRepresentation(image, quality / 100);
NSString* strEncodeData = [scaledImageData base64EncodedStringWithOptions:0];
NSString* strEncodeData = [imageData base64EncodedStringWithOptions:0];
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsString:strEncodeData];
[self.commandDelegate sendPluginResult:result callbackId:lastCommand.callbackId];
@@ -87,10 +87,9 @@
} else {
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:filename];
UIImage *scaledImage = [self scaleImage:image toSize:CGSizeMake(targetWidth, targetHeight)];
NSData *scaledImageData = UIImageJPEGRepresentation(scaledImage, quality / 100);
NSData *imageData = UIImageJPEGRepresentation(image, quality / 100);
//[self deleteFileWithName:imagePath];
[scaledImageData writeToFile:imagePath atomically:YES];
[imageData writeToFile:imagePath atomically:YES];
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsString:[[NSURL fileURLWithPath:imagePath] absoluteString]];
[self.commandDelegate sendPluginResult:result callbackId:lastCommand.callbackId];