OpenCV camera rotation in iOS - opencv

I used openCV camera in my application. The OpenCV camera is working fine either in landscape left or landscape right. I set the orientation as below
-(void)initOpenCVCamera:(UIImageView*)imgView{
VideoCamera *vcamera = [[VideoCamera alloc] initWithParentView:imgView];
vcamera.defaultAVCaptureVideoOrientation = AVCaptureVideoOrientationLandscapeRight;
vcamera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionBack;
vcamera.defaultAVCaptureSessionPreset = AVCaptureSessionPreset640x480;
vcamera.delegate = self;
vcamera.recordVideo = NO;
vcamera.grayscaleMode = NO;
vcamera.rotateVideo = YES;
vcamera.defaultFPS = 30;
}
The default orientation set it to Landscape Right in the above code. But i wanted to support both landscape left and landscape right orientation. So i implemented the delegate methods of OpenCV camera as below
#interface VideoCamera : CvVideoCamera
- (void)updateOrientation;
- (void)layoutPreviewLayer;
#end
#implementation VideoCamera
- (void)updateOrientation
{
self->customPreviewLayer.bounds = CGRectMake(0, 0, self.parentView.frame.size.width, self.parentView.frame.size.height);
[self layoutPreviewLayer];
}
- (void)layoutPreviewLayer
{
if (self.parentView != nil)
{
CALayer* layer = self->customPreviewLayer;
CGRect bounds = self->customPreviewLayer.bounds;
int rotation_angle = 0;
switch (self.defaultAVCaptureVideoOrientation) {
case AVCaptureVideoOrientationLandscapeRight:
rotation_angle = 0;
break;
case AVCaptureVideoOrientationLandscapeLeft:
rotation_angle = 180;
break;
default:
break;
}
layer.position = CGPointMake(self.parentView.frame.size.width/2., self.parentView.frame.size.height/2.);
layer.affineTransform = CGAffineTransformMakeRotation( DEGREES_RADIANS(rotation_angle) );
layer.bounds = bounds;
}
}
#end
But it is not working. I don't know what is the "rotation_angle" i suppose to set.

Related

How to make the RTCEAGLVideoView no deform full screen display

I'm displaying a RTCVideoTrack with RTCEAGLVideoView, and want it to be full screen, but it always deforms.I have tried to use AVMakeRectWithAspectRatioInsideRect method to display this view, there's no distortion but no full screen either, here is my code.
RTCEAGLVideoView *remoteVideoView = [[RTCEAGLVideoView alloc] initWithFrame:self.backView.bounds];
remoteVideoView.contentMode = UIViewContentModeScaleAspectFill;
_remoteVideoView = remoteVideoView;
[remoteVideoView renderFrame:nil];
[remoteVideoTrack addRenderer:remoteVideoView];
remoteVideoView.delegate = self;
self.remoteVideoTrack = remoteVideoTrack;
[self.backView addSubview:remoteVideoView];
when didChangeVideoSize delegate called:
- (void)videoView:(RTCEAGLVideoView*)videoView didChangeVideoSize:(CGSize)size {
if (videoView == _remoteVideoView) {
_remoteVideoSize = size;
}
CGRect bounds = self.view.bounds;
if (_remoteVideoSize.width > 0 && _remoteVideoSize.height > 0) {
// Aspect fill remote video into bounds.
CGRect remoteVideoFrame =
AVMakeRectWithAspectRatioInsideRect(_remoteVideoSize, bounds);
CGFloat scale = 1;
if (remoteVideoFrame.size.width > remoteVideoFrame.size.height) {
// Scale by height.
scale = bounds.size.height / remoteVideoFrame.size.height;
} else {
// Scale by width.
scale = bounds.size.width / remoteVideoFrame.size.width;
}
remoteVideoFrame.size.height *= scale;
remoteVideoFrame.size.width *= scale;
_remoteVideoView.frame = remoteVideoFrame;
_remoteVideoView.center =
CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
} else {
_remoteVideoView.frame = bounds;
}
}
I am wondering if the method of renderFrame can do something helpful, but i can't find how to create a RTCI420Frame property.
You can use RTCMTLVideoView to set contentMode(full screen).
Import:
#import <WebRTC/RTCMTLVideoView.h>
Use this code here:
#if defined(RTC_SUPPORTS_METAL)
RTCMTLVideoView *mtlVideoView = [[RTCMTLVideoView alloc] initWithFrame:CGRectZero];
mtlVideoView.videoContentMode = UIViewContentModeScaleAspectFill;
#else
//Here you can create RTCEAGLVideoView

SetFrame Method doesn't work in ios8

I am using table view with dynamic height.
-(void)ShowTblRoomType
{
// [self.tableView_roomType setTranslatesAutoresizingMaskIntoConstraints:YES];
[self.tableView_roomType setFrame:CGRectMake(self.tableView_roomType.frame.origin.x, self.tableView_roomType.frame.origin.y, self.tableView_roomType.frame.size.width, self.tableView_roomType.rowHeight*self.roomtypeDetails.count)];
self.tableView_roomType.hidden = NO;
//set shadow to uitableview........
self.tableView_roomType.layer.masksToBounds = NO;
self.tableView_roomType.layer.cornerRadius = 8; // if you like rounded corners
self.tableView_roomType.layer.shadowOffset = CGSizeMake(-15, 20);
self.tableView_roomType.layer.shadowRadius = 3;
self.tableView_roomType.layer.shadowOpacity = 0.5;
}
From here I am calling this method
- (IBAction)showRoomTypeDetails:(id)sender
{
if(self.tableView_roomType.hidden == NO){
self.tableView_roomType.hidden = YES;
}else
{
[self ShowTblRoomType];
}
// [self.view_reservation bringSubviewToFront:self.tableView_roomType];
}
it works on below ios 8 but dosn't work on ios8.
on ios8 it shows only one cell.
Appreciate for help

Particle system looks different on iOS 6 and ios7

I have a couple of particle systems on my app that I checked originally on an iPad 3 with ios7. The particles looked and behaved like I expected them, but when I test on an iPad2 with ios6. The particles are displaced up and are smaller. I have a subclass for the particles. Here's the code for one of my particle systems. I was wandering if the CGREctMake is positioning the particle in a different area on a non retina display, but that doesn't happen when I position other things on the display.
Code to call the particles.
- (void)ShowSteamEffect
{
//Create view for Steam particle effect.
CGRect SteamFrame = CGRectMake(790, 380, 100, 100);
//Show Steam effect
ShowSteam = [[SteamEffect alloc]initWithFrame:SteamFrame];
ShowSteam.hidden = NO;
[self.view insertSubview:ShowSteam aboveSubview:imgComputerLights];
}
Steam subclass.
#import "SteamEffect.h"
#implementation SteamEffect
{
CAEmitterLayer* SteamEmitter;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
SteamEmitter = (CAEmitterLayer*) self.layer;
//SteamEmitter.emitterPosition= CGPointMake(0, 0);
//SteamEmitter.emitterSize = CGSizeMake(10,10);
CAEmitterCell* Steam = [CAEmitterCell emitterCell];
Steam.birthRate = 50;
Steam.spin = .6;
Steam.lifetime = 1.7;
Steam.alphaRange = 0.2;
Steam.alphaSpeed = 0.2;
Steam.contents = (id)[[UIImage imageNamed:#"Steam1.png"]CGImage];
Steam.velocity = 30;
Steam.velocityRange = 50;
Steam.emissionLongitude = -60;
Steam.emissionRange = M_1_PI;
Steam.scale = .2;
Steam.scaleSpeed = .5;
Steam.yAcceleration = -200;
SteamEmitter.renderMode = kCAEmitterLayerBackToFront;
SteamEmitter.emitterShape = kCAEmitterLayerCircle;
SteamEmitter.emitterCells = #[Steam];
}
return self;
}
-(void)didMoveToSuperview
{
[super didMoveToSuperview];
if (self.superview==nil) return;
[self performSelector:#selector(disableEmitterCell) withObject:nil afterDelay:0.5];
}
-(void)disableEmitterCell
{
[SteamEmitter setValue:#0 forKeyPath:#"birthRate"];
}
+ (Class) layerClass
{
//tell UIView to use the CAEmitterLayer root class
return [CAEmitterLayer class];
}
- (void) setEmitterPosition:(CGPoint)pos
{
SteamEmitter.emitterPosition = pos;
}
- (void) toggleOn:(bool)on
{
//SteamEmitter.birthRate = on? 300 : 0;
}

Animating Orientation Switch

I created a simple video player that when portrait it shows the video in a small window on top of the view but when switched to landscape it automatically switches to full screen.
A lot like the youTube app does.
My issue is that it doesn't transition well to the full screen mode. The navigation/tabbar controllers transition but my video just basically pops from one to the other.
Here's a little video of what's happening:
http://screencast.com/t/jaOoPOHLh
Here's the code I'm using to play the video and switch the orientation.
self.controller = [[LBYouTubePlayerController alloc] initWithYouTubeURL:[NSURL URLWithString:#"http://www.youtube.com/watch?v=1fTIhC1WSew&list=FLEYfH4kbq85W_CiOTuSjf8w&feature=mh_lolz"] quality:LBYouTubeVideoQualityLarge];
self.controller.delegate = self;
self.controller.view.frame = CGRectMake(0.0f, 0.0f, 320.0f, 180.0f);
- (void) orientationChanged:(NSNotification *)note
{
//self.controller.view.hidden = YES;
UIDevice * device = note.object;
switch(device.orientation)
{
case UIDeviceOrientationPortrait:
/* start special animation */
self.controller.fullscreen = NO;
//self.controller.view.hidden = NO;
break;
case UIDeviceOrientationPortraitUpsideDown:
/* start special animation */
break;
case UIDeviceOrientationLandscapeLeft:
// do something
//self.controller.fullscreen = YES;
//self.controller.scalingMode = MPMovieScalingModeAspectFit;
//self.controller.view.hidden = NO;
break;
case UIDeviceOrientationLandscapeRight:
self.controller.fullscreen = YES;
self.controller.scalingMode = MPMovieScalingModeAspectFit;
self.controller.view.hidden = NO;
break;
default:
break;
};
}
Is there something I need to add or custom transition I'm suppose to do to make the video transition and match the navigation/tabbar?

transform image when it hits the edge of the screen in iOS

I have 10 fireflies that I make "fly" around the screen using the code below.
The code also serves to keep the fireflies on the screen.
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *blueArray = [NSArray array];
blueArray = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"blue1.png"],
[UIImage imageNamed:#"blue2.png"], nil];
blue.animationImages = blueArray;
blue.animationDuration = 0.20;
blue.animationRepeatCount = -1;
[blue startAnimating];
bluepos =CGPointMake(2.0, 1.5);
}
-(void) someMethod {
endingAnimationTimer = [NSTimer scheduledTimerWithTimeInterval:(0.03) target:self selector:#selector(makeFly) userInfo:nil repeats:YES];
}
-(void) makeFly {
blue.center = CGPointMake(blue.center.x+bluepos.x, blue.center.y+bluepos.y);{
if(blue.center.x > 480 || blue.center.x <0)
bluepos.x = -bluepos.x;
if(blue.center.y > 320 || blue.center.y <0)
bluepos.y = -bluepos.y;
}
}
The "flying" works great except that when the fireflies hit the edge of the screen and reverse direction to keep them on the screen the firefly image itself is still "facing" the othe direction so it looks like they are flying backwards half the time.
I want to set it up so that when the fireflies hit the edge of the screen they reverse direction AND the image itself is reversed.
I tried this:
In .h
#property (nonatomic, assign) BOOL blueIsFacingRight;
In .m
#synthesize blueIsFacingRight;
-(void) makeFly {
blue.center = CGPointMake(blue.center.x+bluepos.x, blue.center.y+bluepos.y); {
if(blue.center.x > 480 ) {
if (blueIsFacingRight == YES) {
blue.transform = CGAffineTransformMakeScale(-1, 1);
blueIsFacingRight = NO;
}
bluepos.x = -bluepos.x;
}
if(blue.center.x <0) {
bluepos.x = -bluepos.x;
if (blueIsFacingRight == NO) {
blue.transform = CGAffineTransformMakeScale(1, 1);
blueIsFacingRight = YES;
}
}
if(blue.center.y > 320 )
bluepos.y = -bluepos.y;
if( blue.center.y <0)
bluepos.y = -bluepos.y;
}
}
I thought this would work but the image does not reverse when it hits the "wall"
Can anyone explain why this does not work and if there is a better why to accomplish the effect im looking for?
You are setting the transform X value to -1 in both cases. When its facing right you should set transform like this CGAffineTransformMakeScale(1, 1);
blue.center = CGPointMake(blue.center.x+bluepos.x, blue.center.y+bluepos.y); {
if(blue.center.x > 480 ) {
if (blueIsFacingRight == YES) {
blue.transform = CGAffineTransformMakeScale(-1, 1);
blueIsFacingRight = NO;
}
bluepos.x = -bluepos.x;
}
if(blue.center.x <0) {
bluepos.x = -bluepos.x;
if (blueIsFacingRight == NO) {
blue.transform = CGAffineTransformMakeScale(1, 1);
blueIsFacingRight = YES;
}
}
**blue.transform =CGAffineTransformIdentity;**
THe above was in my makeFly method with a bunch of other stuff above where I was reversing the image so I was undoing the change the very next time the method was called ( 0.03 later)
Man I feel stupid

Resources