IOS Custom Camera with watermark!

Source: Internet
Author: User
<span id="Label3"></p><p><p>//</p></p><p><p>Hszjlcameraviewc.m</p></p><p><p>Hszjlcamera</p></p><p><p>//</p></p><p><p>Created by Purple Chardonnay on 16/1/16.</p></p><p><p>COPYRIGHT©2016 year Hipal. All Rights Reserved.</p></p><p><p>//</p></p><p><p></p></p><p><p>#import "HSZJLCameraViewC.h"</p></p><p><p>#import <AVFoundation/AVFoundation.h></p></p><p><p>#import "HSSaveViewController.h"</p></p><p><p></p></p><p><p>@interface HSZJLCAMERAVIEWC ()</p></p><p><p>/**</p></p><p><p>* Camera Display Area</p></p><p><p>*/</p></p><p><p>@property (weak, nonatomic) iboutlet UIView *cameraview;</p></p><p><p>/**</p></p><p><p>* Watermark Area</p></p><p><p>*/</p></p><p><p>@property (weak, nonatomic) iboutlet UIView *logoview;</p></p><p><p>/**</p></p><p><p>* Tool bar</p></p><p><p>*/</p></p><p><p>@property (weak, nonatomic) iboutlet UIView *toolview;</p></p><p><p></p></p><p><p>/**</p></p><p><p>* Data transfer between devices first created</p></p><p><p>*/</p></p><p><p>@property (nonatomic, Strong) avcapturesession * session;</p></p><p><p>/**</p></p><p><p>* Input stream, I think the camera</p></p><p><p>*/</p></p><p><p>@property (nonatomic, Strong) avcapturedeviceinput * videoinput;</p></p><p><p>/**</p></p><p><p>* Camera</p></p><p><p>*/</p></p><p><p>@property (nonatomic, Strong) avcapturestillimageoutput * stillimageoutput;</p></p><p><p>/**</p></p><p><p>* Display Layer (photo Frame) to create a second</p></p><p><p>*/</p></p><p><p>@property (nonatomic, Strong) Avcapturevideopreviewlayer * previewlayer;</p></p><p><p>/**</p></p><p><p>* Watermark Photo</p></p><p><p>*/</p></p><p><p>@property (weak, nonatomic) iboutlet uiimageview *logoimageview;</p></p><p><p></p></p><p><p>@end</p></p><p><p></p></p><p><p>@implementation HSZJLCAMERAVIEWC</p></p><p><p></p></p><p><p>-(instancetype) initwithnibname: (nsstring *) nibnameornil bundle: (nsbundle *) Nibbundleornil {</p></p><p><p>Self = [super Initwithnibname:nibnameornil bundle:nibbundleornil];</p></p><p><p>If (self) {</p></p><p><p></p></p><p><p>If (floor (nsfoundationversionnumber) <= nsfoundationversionnumber_ios_7_1) {</p></p><p><p>IOS 7</p></p><p><p>[self initsession];</p></p><p><p>}</p></p><p><p>else {</p></p><p><p>IOS 8</p></p><p><p>Avauthorizationstatus status = [avcapturedevice authorizationstatusformediatype:avmediatypevideo];</p></p><p><p>Switch (status) {</p></p><p><p>Case Avauthorizationstatusauthorized:</p></p><p><p>[self initsession];</p></p><p><p>Break</p></p><p><p>Case Avauthorizationstatusnotdetermined: {</p></p><p><p>User authorization</p></p><p><p>[avcapturedevice requestaccessformediatype:avmediatypevideo completionhandler:^ (BOOL granted) {</p></p><p><p>If (granted) {</p></p><p><p>[self initsession];</p></p><p><p>} else {</p></p><p><p>[self Dismissviewcontrolleranimated:yes completion:^{</p></p><p><p></p></p><p><p>}];</p></p><p><p>}</p></p><p><p>}];</p></p><p><p>}</p></p><p><p>Break</p></p><p><p>Default</p></p><p><p>Break</p></p><p><p>}</p></p><p><p></p></p><p><p>}</p></p><p><p>}</p></p><p><p>Return self;</p></p><p><p>}</p></p><p><p></p></p><p><p></p></p><p><p>-(void) Viewdidload {</p></p><p><p>[super viewdidload];</p></p><p><p></p></p><p><p>Self.view.clipsToBounds = NO;</p></p><p><p>Self.view.backgroundColor = [uicolor blackcolor];</p></p><p><p></p></p><p><p>[self setupcameralayer];</p></p><p><p></p></p><p><p>}</p></p><p><p>/**</p></p><p><p>* Initialization</p></p><p><p>*/</p></p><p><p>-(void) initsession</p></p><p><p>{</p></p><p><p></p></p><p><p>Self.session = [[avcapturesession alloc] init];</p></p><p><p>Self.session.sessionPreset = avcapturesessionpresetphoto;</p></p><p><p>Self.videoinput = [[avcapturedeviceinput alloc] initwithdevice:[self backcamera] error:nil];</p></p><p><p></p></p><p><p>Self.stillimageoutput = [[avcapturestillimageoutput alloc] init];</p></p><p><p>Nsdictionary * outputsettings = [[nsdictionary alloc] initwithobjectsandkeys:avvideocodecjpeg,avvideocodeckey, nil];</p></p><p><p>Output images in JPEG image format</p></p><p><p>[self.stillimageoutput setoutputsettings:outputsettings];</p></p><p><p></p></p><p><p>If ([self.session canAddInput:self.videoInput]) {</p></p><p><p>[self.session addInput:self.videoInput];</p></p><p><p>}</p></p><p><p>If ([self.session canAddOutput:self.stillImageOutput]) {</p></p><p><p>[self.session addOutput:self.stillImageOutput];</p></p><p><p>}</p></p><p><p></p></p><p><p>}</p></p><p><p>/**</p></p><p><p>* Set camera Layer</p></p><p><p>*/</p></p><p><p>-(void) Setupcameralayer</p></p><p><p>{</p></p><p><p></p></p><p><p>if (self.previewlayer = = Nil) {</p></p><p><p>Self.previewlayer = [[avcapturevideopreviewlayer alloc] initWithSession:self.session];</p></p><p><p></p></p><p><p>UIView * view = self.cameraview;</p></p><p><p>Calayer * Viewlayer = [view layer];</p></p><p><p>[viewlayer setmaskstobounds:yes];</p></p><p><p></p></p><p><p>CGRect bounds = [view bounds];</p></p><p><p>Bounds.size.width = [uiscreen mainscreen].bounds.size.width;</p></p><p><p>Bounds.size.height = [uiscreen Mainscreen].bounds.size.height * 0.6;</p></p><p><p>[self.previewlayer setframe:bounds];</p></p><p><p>[self.previewlayer setvideogravity:avlayervideogravityresizeaspectfill];</p></p><p><p></p></p><p><p>[viewlayer InsertSublayer:self.previewLayer Below:[[viewlayer sublayers] objectatindex:0]];</p></p><p><p>}</p></p><p><p>}</p></p><p><p>/**</p></p><p><p>* Photo shoot</p></p><p><p>*/</p></p><p><p>-(void) opencamera{</p></p><p><p>[self shuttercamera];</p></p><p><p>}</p></p><p><p>/**</p></p><p><p>* Methods to get front and rear camera objects</p></p><p><p>*/</p></p><p><p>-(avcapturedevice *) camerawithposition: (avcapturedeviceposition) Position {</p></p><p><p>Nsarray *devices = [avcapturedevice deviceswithmediatype:avmediatypevideo];</p></p><p><p>For (avcapturedevice *device in devices) {</p></p><p><p>If ([device position] = = Position) {</p></p><p><p>Return device;</p></p><p><p>}</p></p><p><p>}</p></p><p><p>Return nil;</p></p><p><p>}</p></p><p><p></p></p><p><p>Front-facing camera</p></p><p><p>-(avcapturedevice *) Frontcamera {</p></p><p><p>return [self camerawithposition:avcapturedevicepositionfront];</p></p><p><p>}</p></p><p><p>Rear-facing camera</p></p><p><p>-(avcapturedevice *) Backcamera {</p></p><p><p>return [self camerawithposition:avcapturedevicepositionback];</p></p><p><p>}</p></p><p><p>/**</p></p><p><p>* Front and rear camera switch</p></p><p><p>*/</p></p><p><p>-(void) Togglecamera {</p></p><p><p></p></p><p><p>Nsuinteger cameracount = [[avcapturedevice deviceswithmediatype:avmediatypevideo] count];</p></p><p><p>If (cameracount > 1) {</p></p><p><p>Nserror *error;</p></p><p><p>Avcapturedeviceinput *newvideoinput;</p></p><p><p>Avcapturedeviceposition position = [[_videoinput device] position];</p></p><p><p></p></p><p><p>if (position = = Avcapturedevicepositionback)</p></p><p><p>Newvideoinput = [[avcapturedeviceinput alloc] initwithdevice:[self frontcamera] error:&error];</p></p><p><p>else if (position = = Avcapturedevicepositionfront)</p></p><p><p>Newvideoinput = [[avcapturedeviceinput alloc] initwithdevice:[self backcamera] error:&error];</p></p><p><p>Else</p></p><p><p>Return</p></p><p><p></p></p><p><p>If (newvideoinput! = Nil) {</p></p><p><p>[self.session beginconfiguration];</p></p><p><p>[self.session removeInput:self.videoInput];</p></p><p><p>If ([self.session canaddinput:newvideoinput]) {</p></p><p><p>[self.session addinput:newvideoinput];</p></p><p><p>[self setvideoinput:newvideoinput];</p></p><p><p>} else {</p></p><p><p>[self.session addInput:self.videoInput];</p></p><p><p>}</p></p><p><p>[self.session commitconfiguration];</p></p><p><p>} else if (error) {</p></p><p><p>NSLog (@ "open failed%@", error);</p></p><p><p>}</p></p><p><p>}</p></p><p><p>}</p></p><p><p></p></p><p><p>#pragma mark--take a picture</p></p><p><p></p></p><p><p>-(void) Shuttercamera</p></p><p><p>{</p></p><p><p>Avcaptureconnection * videoconnection = [self.stillimageoutput connectionwithmediatype:avmediatypevideo];</p></p><p><p>If (!videoconnection) {</p></p><p><p>NSLog (@ "photo failed");</p></p><p><p>Return</p></p><p><p>}</p></p><p><p></p></p><p><p>[self.stillimageoutput capturestillimageasynchronouslyfromconnection:videoconnection completionHandler:^ ( Cmsamplebufferref imagedatasamplebuffer, nserror *error) {</p></p><p><p>if (imagedatasamplebuffer = = NULL) {</p></p><p><p>Return</p></p><p><p>}</p></p><p><p></p></p><p><p>Photo</p></p><p><p>NSData * ImageData = [avcapturestillimageoutput jpegstillimagensdatarepresentation:imagedatasamplebuffer];</p></p><p><p>UIImage * image = [UIImage imagewithdata:imagedata];</p></p><p><p></p></p><p><p>Hssaveviewcontroller *SAVC = [[hssaveviewcontroller alloc] initwithnibname:@ "hssaveviewcontroller" bundle:nil];</p></p><p><p>UIImage *logo = [UIImage imagenamed:@ "picture_logo1"];</p></p><p><p>[self PRESENTVIEWCONTROLLER:SAVC Animated:yes completion:^{</p></p><p><p>[SAVC Setphotoimage:image logoimage:logo];</p></p><p><p>}];</p></p><p><p>}];</p></p><p><p>}</p></p><p><p></p></p><p><p>-(UIImage *) addimage: (UIImage *) image1 toimage: (UIImage *) image2 {</p></p><p><p>Uigraphicsbeginimagecontext (image1.size);</p></p><p><p></p></p><p><p>Draw Image1</p></p><p><p>[image1 drawinrect:cgrectmake (0, 0, image1.size.width, image1.size.height)];</p></p><p><p></p></p><p><p>Draw Image2</p></p><p><p>[image2 drawinrect:cgrectmake (0, 0, image2.size.width, image2.size.height)];</p></p><p><p></p></p><p><p>UIImage *resultingimage = Uigraphicsgetimagefromcurrentimagecontext ();</p></p><p><p></p></p><p><p>Uigraphicsendimagecontext ();</p></p><p><p></p></p><p><p>Return resultingimage;</p></p><p><p>}</p></p><p><p></p></p><p><p>#pragma mark--add Watermark Code</p></p><p><p>-(UIImage *) completeeditwithimage: (uiimage*) Image {</p></p><p><p></p></p><p><p>Cgsize size = image.size;</p></p><p><p>Uigraphicsbeginimagecontextwithoptions (cgsizemake (size.width-2, size.height-2), NO, 1.0);</p></p><p><p>[image drawinrect:cgrectmake (0, 0, size.width, size.height)];</p></p><p><p></p></p><p><p>UIImage *logo = [UIImage imagenamed:@ "picture_logo.png"];</p></p><p><p>[logo Drawatpoint:cgpointmake (100, 100)];</p></p><p><p></p></p><p><p>Image = Uigraphicsgetimagefromcurrentimagecontext ();</p></p><p><p>Uigraphicsendimagecontext ();</p></p><p><p>Return image;</p></p><p><p>}</p></p><p><p></p></p><p><p>/**</p></p><p><p>* Because at all low-end opportunities somehow memory warning startrunning-map to the screen</p></p><p><p>*/</p></p><p><p></p></p><p><p>-(void) Viewdidappear: (BOOL) Animated {</p></p><p><p>[super viewdidappear:yes];</p></p><p><p>If (self.session) {</p></p><p><p>[self.session startrunning];</p></p><p><p>}</p></p><p><p>}</p></p><p><p>-(void) viewdiddisappear: (BOOL) Animated</p></p><p><p>{</p></p><p><p>[super viewdiddisappear:animated];</p></p><p><p>If (self.session) {</p></p><p><p>[self.session stoprunning];</p></p><p><p>}</p></p><p><p>}</p></p><p><p></p></p><p><p>-(void) didreceivememorywarning {</p></p><p><p>[super didreceivememorywarning];</p></p><p><p>NSLog (@ "camera Memory warning");</p></p><p><p>}</p></p><p><p></p></p><p><p></p></p><p><p>-(ibaction) takepictureclick: (id) Sender {</p></p><p><p>[self shuttercamera];</p></p><p><p>}</p></p><p><p></p></p><p><p>-(ibaction) cancelclick: (UIButton *) Sender {</p></p><p><p>If (self.navigationController.viewControllers.count >1) {</p></p><p><p>[self.navigationcontroller popviewcontrolleranimated:yes];</p></p><p><p>}else{</p></p><p><p>[self Dismissviewcontrolleranimated:yes completion:^{</p></p><p><p></p></p><p><p>}];</p></p><p><p>}</p></p><p><p>}</p></p><p><p></p></p><p><p>-(ibaction) changecameraclick: (UIButton *) Sender {</p></p><p><p>[self togglecamera];</p></p><p><p>}</p></p><p><p></p></p><p><p>-(ibaction) choselogo: (uibutton*) Sender {</p></p><p><p>Switch (sender.tag) {</p></p><p><p>Case 0:</p></p><p><p>{</p></p><p><p>Not selected</p></p><p><p>Self.logoImageVIew.image = nil;</p></p><p><p>}</p></p><p><p>Break</p></p><p><p>Case 1:</p></p><p><p>{</p></p><p><p>Self.logoImageVIew.image = [UIImage imagenamed:@ "picture_logo"];</p></p><p><p>}</p></p><p><p>Break</p></p><p><p>Case 2:</p></p><p><p>{</p></p><p><p>Self.logoImageVIew.image = [UIImage imagenamed:@ "picture_logo1"];</p></p><p><p>}</p></p><p><p>Break</p></p><p><p>Case 3:</p></p><p><p>{</p></p><p><p>Self.logoImageVIew.image = [UIImage imagenamed:@ "picture_logo"];</p></p><p><p>}</p></p><p><p>Break</p></p><p><p>Default</p></p><p><p>Break</p></p><p><p>}</p></p><p><p>}</p></p><p><p>@end</p></p><p><p></p></p><p><p>IOS Custom Camera with watermark!</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.