FFMPEG -- Save streaming data as image in iOS
来源:互联网 发布:答题软件 编辑:程序博客网 时间:2024/05/18 09:05
近期需要在 iPhone 的串流應用程式加上擷取圖片的功能,將自己的實作經驗作一整理。
Step 1. 使用FFMPEG取得串流內的影像資料,並進行解碼
Step 2. 將影像轉成 iOS 定義的 UIImage
Step 3. 將 UIImage 存檔
Step 1. 使用FFMPEG取得串流內的影像資料,並進行解碼
av_read_frame(pFormatCtx, &packet);avcodec_decode_video2(videoCodecCtx, DecodedFrame, &frameFinished, & packet);
Step 2. 將影像轉成 iOS 定義的 UIImage
先將 YUV格式的 AVFrame 轉換為 RGB格式的 AVPicture
img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, outputWidth, outputHeight, PIX_FMT_RGB24, sws_flags, NULL, NULL, NULL);
再將 AVPicture 轉換為 iOS 的 UIImage
-(UIImage *)imageFromAVPicture:(AVPicture)pict width:(int)width height:(int)height { CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault; CFDataRef data = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, pict.data[0], pict.linesize[0]*height,kCFAllocatorNull); CGDataProviderRef provider = CGDataProviderCreateWithCFData(data); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGImageRef cgImage = CGImageCreate(width, height, 8, 24, pict.linesize[0], colorSpace, bitmapInfo, provider, NULL, NO, kCGRenderingIntentDefault); CGColorSpaceRelease(colorSpace); UIImage *image = [UIImage imageWithCGImage:cgImage]; CGImageRelease(cgImage); CGDataProviderRelease(provider); CFRelease(data); return image;}
Step 3. 將 UIImage 存檔
這邊整理三種方法
1. 將檔案存入指定的目錄 (PNG or JPEG)
[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES];[UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];
2. 將影像直接存入相簿 (JPEG)
void UIImageWriteToSavedPhotosAlbum ( UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo);
3. 將檔案存入相簿中的特定群組iOS預設並沒有提供此種功能。
因此可以參考 www.touch-code-magazine.com 的文章,使用下列客製化的API完成此功能。
-(void)saveImage:(UIImage*)image toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock;
參考資料:
- http://www.touch-code-magazine.com/ios5-saving-photos-in-custom-photo-album-category-for-download/
- http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIKitFunctionReference/Reference/reference.html
- http://albert-oma.blogspot.com/2013/05/ffmpeg-save-streaming-data-as-image-in.html
- FFMPEG -- Save streaming data as image in iOS
- Save+as+Image+using+DrawingImage()+in+WPF
- Save an userform as an image in EXCEL
- save image in Database
- Streaming single image with ffmpeg
- ios save image to album
- Save image in SQL Server (delphi)
- Learnning Dlib(三) Dlib load ios image and save image
- ffmpeg streaming
- System.Drawing.Image.Save, A generic error occurred in GDI+.
- Core Data in IOS
- Saving Data in iOS
- How to do take snapshot image as binary data?
- save the file in unicode format to prevent data loss.
- QT14 how to save data in sqlite database with pushbutton
- iOS Core Data: 存储自定义对象 Save Custom NSObject
- QUICKREPORT SAVE AS
- How to save SQL Server data as Insert INTO SQL Script
- linux sed 用法
- [HEVC/H265] 另一个开源项目openHEVC
- Java中的HashMap和ConcurrentHashMap的并发性能测试
- [完美解决Dede关键字交叉(重叠)替换问题]dede5.6 关键词自动添加链接
- 智能指针学习
- FFMPEG -- Save streaming data as image in iOS
- python , 一个简单的单线程的C/S模型示例
- 【转】live555—VS2010 下live555编译、使用及测试
- [ solr入门 ] - 在eclipse中发布solr
- 摘录:Liz Murray 在 DePauw University的演讲
- 四两拨千斤的工作小工具分享!
- js的Boolean对象初始值
- 用nodepad++将代码文件导出为带行号的html文件
- DSS Source Code Analyse (07) - TaskThread::Entry