Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I faced the same problem as you in the preview when using the front camera, because the preview is mirrored and I could not find any good information to scale. </p> <p>The following code is the closest I got. Please note that I have an image defined as property that is called <code>heartImage</code> and that I assume you are using the Apple Sample called SquareCam. </p> <p>In the method <code>- (void)drawFaceBoxesForFeatures:(NSArray *)features forVideoBox:(CGRect)clap orientation:(UIDeviceOrientation)orientation</code></p> <pre><code> if(ff.hasLeftEyePosition) { //swap coordinates CGFloat leftEyeRectOriginX = ff.leftEyePosition.y ; CGFloat leftEyeRectOriginY = ff.leftEyePosition.x ; CGFloat leftEyeRectWidth = faceRect.size.width*0.3; CGFloat leftEyeRectHeight = faceRect.size.width*0.3; //adjust scale leftEyeRectOriginX *= widthScaleBy; leftEyeRectOriginY *= heightScaleBy; NSLog(@"LeftEyePosition: %@", NSStringFromCGPoint(ff.leftEyePosition)); CGRect r = CGRectMake(leftEyeRectOriginX - (leftEyeRectWidth/2) , leftEyeRectOriginY - (leftEyeRectHeight/2), leftEyeRectWidth, leftEyeRectHeight); if ( isMirrored ){ r = CGRectOffset(r, previewBox.origin.x + previewBox.size.width - (rightEyeRectOriginX*2) - rightEyeRectWidth+ faceRect.origin.x, previewBox.origin.y); NSLog(@"LeftEyeRect mirrored: %@", NSStringFromCGRect(r)); } else{ r = CGRectOffset(r, previewBox.origin.x, previewBox.origin.y); } while ( !leftEyeEyeLayer &amp;&amp; (currentSublayer &lt; sublayersCount) ) { CALayer *currentLayer = [sublayers objectAtIndex:currentSublayer++]; if ( [[currentLayer name] isEqualToString:@"LeftEyeLayer"] ) { leftEyeEyeLayer = currentLayer; [currentLayer setHidden:NO]; } } // create a new one if necessary if ( !leftEyeEyeLayer ) { leftEyeEyeLayer = [CALayer new]; [leftEyeEyeLayer setContents:(id)[heartImage CGImage]]; [leftEyeEyeLayer setName:@"LeftEyeLayer"]; [previewLayer addSublayer:leftEyeEyeLayer]; [leftEyeEyeLayer release]; } [leftEyeEyeLayer setFrame:r]; } </code></pre> <p>The same applies for the Right Eye, with the exception that I use this in case it's mirrored: <code>r = CGRectOffset(r, previewBox.origin.x + previewBox.size.width - (rightEyeRectOriginX*2) - rightEyeRectWidth+ faceRect.origin.x, previewBox.origin.y); </code>. The only difference with the sample code is that you first want to remove all the featureLayers, so some lines above my code looks like this:</p> <pre><code> // hide all the face layers for ( CALayer *layer in sublayers ) { if ( [[layer name] isEqualToString:@"FaceLayer"] || [[layer name] isEqualToString:@"LeftEyeLayer"] || [[layer name] isEqualToString:@"RightEyeLayer"] ) [layer setHidden:YES]; } </code></pre> <p>To be precise, I have troubles only in the live camera preview. When using the method to save picture in library (<code>- (CGImageRef)newSquareOverlayedImageForFeatures:(NSArray *)features inCGImage:(CGImageRef)backgroundImage withOrientation:(UIDeviceOrientation)orientation frontFacing:(BOOL)isFrontFacing </code>) it works correctly by using:</p> <pre><code> if(ff.hasLeftEyePosition) { CGRect r = CGRectMake(ff.leftEyePosition.x-faceWidth*0.15, ff.leftEyePosition.y-faceWidth*0.15, faceWidth*0.3, faceWidth*0.3); CGContextDrawImage(bitmapContext, r, [rotatedHeartImage CGImage]); } </code></pre> <p>Please let me know if and how I should improve my answer.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload