屏幕分享

窗口缩略图

下载pdf
更新时间:2019-09-27 10:09

1 简介

窗口缩略图主要是辅助窗口分享和屏幕分享选择相应的窗口或屏幕。

本文主要讲述如何设置窗口缩略图分享 详情见示例Demo

2 缩略图调用流程

检测应用的辅助功能

由于MacOS的限制。需要开启辅助功能才能检测到窗口状态。如果没有开启不影响采集,但是窗口状态的通知可能失效。

  //如果需要使用thumbnail功能,建议检测是否开启了辅助功能
    CFDictionaryRef options = (__bridge CFDictionaryRef)@{(__bridge NSString *)kAXTrustedCheckOptionPrompt: @YES};
    if (!AXIsProcessTrustedWithOptions(options)) {
        // Ask for permission
        NSAlert *alert = [[NSAlert alloc] init];
        alert.messageText = @"打开辅助功能";
        alert.informativeText = @"你需要打开辅助功能,开启后重启应用!";
        [alert runModal];
    }else{
        ZegoShareDeskWindow * _mainWindow = [[ZegoShareDeskWindow alloc]initWithWindowNibName:@"ZegoShareDeskWindow"];
        [[_mainWindow window] center];
        [_mainWindow.window orderFront:nil];
    }

1. 初始化

/*
设置日志路径和级别
*/
NSString* logpath = @"/Users/**/ZegoScreenCapture/Mac_OSX/ScreenCaptureDemo";

zego_screencapture_set_log_level(kZegoLogLevelDebug, [logpath cStringUsingEncoding:NSASCIIStringEncoding]);
/*
初始化
*/
zego_screencapture_init();

/*
设置窗口状态变化回调
*/
zego_screencapture_reg_captured_window_status_change_notify(zego_screencapture_captured_window_status_change_notify,NULL);

2. 获取窗口列表

/*
获取可以建立缩略图的窗口和屏幕
*/
const struct ZegoThumbnailWindowInfo * windowList = zego_windowthumbnail_find_windows(&windowListLen);

3. 注册缩略图

/*
传入需要展示缩略图的窗口或者屏幕ID。将会在窗口状态变化回调里返回图片数据。交由客户端自己绘制
*/
zego_windowthumbnail_register((CGWindowID)windowList[i].handle);

4. 绘制窗口标题和窗口图标

/*
zego_screencapture_reg_captured_window_status_change_notify回调类型为kZegoThumbnailWindowStatusThumbnail里返回缩略图和图标数据
*/

struct ZegoThumbnailIconBits
{
    int width;                 ///< 窗口对应图标的宽(像素)
    int height;                ///< 采集画面高(像素)
    int format;                ///< (未使用) 默认BGRA32
    unsigned int len;          ///< 窗口对应图标画面数据大小
    unsigned char* bits;       ///< 窗口对应图标画面数据
};

struct ZegoThumbnailWindowInfo
{
    int thumbnail_id;                             ///< 缩略图ID
    ZegoWindowHandle handle;                      ///< 窗口句柄
    char window_title[ZEGO_MAX_TITLE];            ///< 窗口标题(windows当isScreen为true时 格式为 屏幕1----\\\\.\\desk 1)
    ZegoThumbnailIconBits *icon_bits;             ///< 源窗口进程图标数据
#ifndef WIN32
    ZegoThumbnailIconBits *image_bits;            ///< 缩略图数据
#endif
    bool isScreen;                                ///< 是否是屏幕
};

5. 点击显示源窗口

/*
客户端选中其中窗口进行分享时,可以调用zego_windowthumbnail_show_source_window方法将源窗口进行置顶显示
*/
zego_windowthumbnail_show_source_window(windowItem.windowID,kZegoThumbnailShowWindowCmdShow);

6. 窗口状态变化回调(包含缩略图和图标的回调)

/*
注册缩略图的窗口后,当窗口标题发生改变或者窗口被关闭将产生回调。注意:新增窗口不会实时获取回调。需要自己主动调用。每注册一次窗口(zego_windowthumbnail_register)回调对应的窗口缩略图和图标一次
*/
void window_status_change_notify(enum ZegoThumbnailWindowStatus status_code, struct ZegoThumbnailWindowInfo* window_info ,void *user_data){

    switch (status_code) {
        //窗口被销毁 
        case kZegoThumbnailWindowStatusDestroy:{
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                NSAlert *alert = [[NSAlert alloc] init];
                alert.messageText = @"被关闭";
                alert.informativeText = @"停止吧";
                [alert runModal];
            });
        }
            break;
        case kZegoThumbnailWindowStatusInvalid:{

            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                NSAlert *alert = [[NSAlert alloc] init];
                alert.messageText = @"异常";
                alert.informativeText = @"停止吧";
                [alert runModal];
            });

        }
            break;
        //窗口状态变化 
        case kZegoThumbnailWindowStatusCaptureChange:{


        }
            break;
        //缩略图数据回调
        case kZegoThumbnailWindowStatusThumbnail:{

            NSString * str = [NSString stringWithUTF8String:(window_info->window_title)];
            NSImage * image = nil;
            NSImage * icon = nil;

            ZegoThumbnailIconBits *imageBits = window_info->image_bits;
            if (imageBits -> len > 0 && imageBits->bits && imageBits != NULL) {
                image = [object imageFromBRGABytes:imageBits->bits imageSize:CGSizeMake(imageBits->width, imageBits->height)];
            }

            ZegoThumbnailIconBits *iconBits = window_info->icon_bits;
            if(iconBits != NULL){
                if (iconBits -> len > 0 && iconBits->bits && iconBits != NULL) {
                    NSData * data = [[NSData alloc]initWithBytes:iconBits->bits length:iconBits->len];
                    icon = [[NSImage alloc]initWithData:data];
                    icon = [object imageFromBRGABytes:iconBits->bits imageSize:CGSizeMake(iconBits->width, iconBits->height)];
                }
            }

            ZegoNSView * view = [object.viewDic objectForKey:@(window_info->handle)];
            ZegoNSView * iconView = [object.iconViewDic objectForKey:@(window_info->handle)];

            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                NSImageView * imageView = [[NSImageView alloc]initWithFrame:view.bounds];
                [view addSubview:imageView];

                imageView.image = image;

                NSImageView * iconImageView = [[NSImageView alloc]initWithFrame:iconView.bounds];
                [iconView addSubview:iconImageView];
                iconImageView.image = icon;
            });


        }
            break;

        default:
            break;
    }
}

3 缩略图效果图