iOS開發- 相機(網路攝影機)擷取到的圖片自動旋轉90度解決辦法,ios90度

來源:互聯網
上載者:User

iOS開發- 相機(網路攝影機)擷取到的圖片自動旋轉90度解決辦法,ios90度

今天寫demo的時候發現, 如果把通過相機擷取到的圖片,直接進行操作, 比如裁剪, 縮放, 則會把原圖片向又旋轉90度。

剛開始覺得莫名其妙, 不知所措。 後來百度了一下,找到瞭解決辦法。


ps: 尋找過程中, 碰到了一種說法:

//get original photo from iOS photos //如果該圖片大於2M,會自動旋轉90度;否則不旋轉UIImage* originalImg=[dict objectForKey:UIImagePickerControllerOriginalImage];

至於是否正確, 還沒確定。 先Mark。


下面的解決辦法親測可行。 原文:http://www.cnblogs.com/jiangyazhou/archive/2012/03/22/2412343.html

用相機拍攝出來的照片含有EXIF資訊,UIImage的imageOrientation屬性指的就是EXIF中的orientation資訊。
如果我們忽略orientation資訊,而直接對照片進行像素處理或者drawInRect等操作,得到的結果是翻轉或者旋轉90之後的樣子。這是因為我們執行像素處理或者drawInRect等操作之後,imageOrientaion資訊被刪除了,imageOrientaion被重設為0,造成照片內容和imageOrientaion不匹配。
所以,在對照片進行處理之前,先將照片旋轉到正確的方向,並且返回的imageOrientaion為0。
下面這個方法就是一個UIImage category中的方法,用它可以達到以上目的。

- (UIImage *)fixOrientation:(UIImage *)aImage {        // No-op if the orientation is already correct    if (aImage.imageOrientation == UIImageOrientationUp)         return aImage;        // We need to calculate the proper transformation to make the image upright.    // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.    CGAffineTransform transform = CGAffineTransformIdentity;        switch (aImage.imageOrientation) {        case UIImageOrientationDown:        case UIImageOrientationDownMirrored:            transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);            transform = CGAffineTransformRotate(transform, M_PI);            break;                    case UIImageOrientationLeft:        case UIImageOrientationLeftMirrored:            transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);            transform = CGAffineTransformRotate(transform, M_PI_2);            break;                    case UIImageOrientationRight:        case UIImageOrientationRightMirrored:            transform = CGAffineTransformTranslate(transform, 0, aImage.size.height);            transform = CGAffineTransformRotate(transform, -M_PI_2);            break;        default:            break;    }        switch (aImage.imageOrientation) {        case UIImageOrientationUpMirrored:        case UIImageOrientationDownMirrored:            transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);            transform = CGAffineTransformScale(transform, -1, 1);            break;                    case UIImageOrientationLeftMirrored:        case UIImageOrientationRightMirrored:            transform = CGAffineTransformTranslate(transform, aImage.size.height, 0);            transform = CGAffineTransformScale(transform, -1, 1);            break;        default:            break;    }        // Now we draw the underlying CGImage into a new context, applying the transform    // calculated above.    CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height,                                             CGImageGetBitsPerComponent(aImage.CGImage), 0,                                             CGImageGetColorSpace(aImage.CGImage),                                             CGImageGetBitmapInfo(aImage.CGImage));    CGContextConcatCTM(ctx, transform);    switch (aImage.imageOrientation) {        case UIImageOrientationLeft:        case UIImageOrientationLeftMirrored:        case UIImageOrientationRight:        case UIImageOrientationRightMirrored:            // Grr...            CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage);            break;                    default:            CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage);            break;    }        // And now we just create a new UIImage from the drawing context    CGImageRef cgimg = CGBitmapContextCreateImage(ctx);    UIImage *img = [UIImage imageWithCGImage:cgimg];    CGContextRelease(ctx);    CGImageRelease(cgimg);    return img;}



現在 做了個 android 視頻錄製的程式 ,後置網路攝影機總是旋轉90度,怎用代碼實現正常

MediaRecorder 中一個方法setCamera();所以可以先設定好網路攝影機的參數,然後再設定到MediaRecorder 中(但是錄製前需要解鎖)。
部分關鍵的代碼如果下:
//初始化相機資訊
Camera mCamera = Camera.open();
Camera.Parameters params = mCamera.getParameters();
mCamera.setDisplayOrientation(90);//旋轉了90度,最好先判斷下JDK的版本號碼,再決定旋轉不
mCamera.setParameters(params);
mCamera.stopPreview();
mCamera.unlock();//解鎖
videoMediaRecorder.setCamera(mCamera);
 
給推薦個可以使映像旋轉90度的網路攝影機攝像軟體

很多的網路攝影機在連通視頻後,可以在功能設定中調試的,甚至可以反轉和鏡像視頻顯示。比如中興晶片的網路攝影機就可以的。誘惑之光Z803也可以。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.