如何使用多对等连接将摄像头从一个iOS设备流式传输到另一个设备

如何使用多对等连接将摄像头从一个iOS设备流式传输到另一个设备,第1张

概述我们如何在iOS 7中使用蓝牙或wifi有效地将摄像头源从一个iOS设备传输到另一个iOS设备.下面是获取流缓冲区的代码. - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnect 我们如何在iOS 7中使用蓝牙或wifi有效地将摄像头源从一个iOS设备传输到另一个iOS设备.下面是获取流缓冲区的代码.
- (voID)captureOutput:(AVCaptureOutput *)captureOutput         dIDOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer         fromConnection:(AVCaptureConnection *)connection{    // Create a UIImage from the sample buffer data    UIImage *image = [self imageFromSampleBuffer:sampleBuffer];}    // Create a UIImage from sample buffer data- (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer{    // Get a CMSampleBuffer's Core VIDeo image buffer for the media data    CVImageBufferRef imageBuffer = CMSampleBufferGetimageBuffer(sampleBuffer);    // Lock the base address of the pixel buffer    CVPixelBufferLockBaseAddress(imageBuffer,0);    // Get the number of bytes per row for the pixel buffer    voID *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);    // Get the number of bytes per row for the pixel buffer    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);    // Get the pixel buffer wIDth and height    size_t wIDth = CVPixelBufferGetWIDth(imageBuffer);    size_t height = CVPixelBufferGetHeight(imageBuffer);    // Create a device-dependent RGB color space    CGcolorSpaceRef colorSpace = CGcolorSpaceCreateDeviceRGB();    // Create a bitmap graphics context with the sample buffer data    CGContextRef context = CGBitmapContextCreate(baseAddress,wIDth,height,8,bytesPerRow,colorSpace,kCGBitmapByteOrder32little | kCGImageAlphaPremultiplIEdFirst);    // Create a Quartz image from the pixel data in the bitmap graphics context    CGImageRef quartzImage = CGBitmapContextCreateImage(context);    // Unlock the pixel buffer    CVPixelBufferUnlockBaseAddress(imageBuffer,0);    // Free up the context and color space    CGContextRelease(context);    CGcolorSpaceRelease(colorSpace);    // Create an image object from the Quartz image    UIImage *image = [UIImage imageWithCGImage:quartzImage];    // Release the Quartz image    CGImageRelease(quartzImage);    return (image);}

在这里,我们可以获得iOS相机捕获的图像.

我们是否可以使用多个对等方将样本缓冲区信息直接发送到另一个设备,或者是否有任何有效的方法将数据传输到其他iOS设备?

谢谢.

解决方法 我得到了这样做的方法,我们可以使用多对等连接来流式传输压缩图像,使其看起来像是相机流.

将要发送流的一个对等体将使用此代码.在captureOutput Delegate方法中:

NSData *imageData = UIImageJPEGRepresentation(cgBackedImage,0.2);    // maybe not always the correct input?  just using this to send current FPS...    AVCaptureinputPort* inputPort = connection.inputPorts[0];    AVCaptureDeviceinput* deviceinput = (AVCaptureDeviceinput*) inputPort.input;    CMTime frameDuration = deviceinput.device.activeVIDeoMaxFrameDuration;    NSDictionary* dict = @{                           @"image": imageData,@"timestamp" : timestamp,@"framesPerSecond": @(frameDuration.timescale)                           };    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:dict];    [_session sendData:data topeers:_session.connectedPeers withMode:MCSessionSendDataReliable error:nil];

在接收方:

- (voID)session:(MCSession *)session dIDReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID {//    NSLog(@"(%@) Read %d bytes",peerID.displayname,data.length);    NSDictionary* dict = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:data];    UIImage* image = [UIImage imageWithData:dict[@"image"] scale:2.0];    NSNumber* framesPerSecond = dict[@"framesPerSecond"];}

我们将获得FPS值,因此我们可以设置参数来管理我们的流图像.

希望它会有所帮助.

谢谢.

总结

以上是内存溢出为你收集整理的如何使用多对等连接将摄像头从一个iOS设备流式传输到另一个设备全部内容,希望文章能够帮你解决如何使用多对等连接将摄像头从一个iOS设备流式传输到另一个设备所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1099820.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存