js读取屏幕分辨率,根据屏幕分辨率修改css

下午在尝试做类似豆瓣的弹出登陆窗口,发现豆瓣登陆窗口会更加分辨率的不同自动调节其left跟top的参数,于是联想到是否有个通过js获取屏幕分辨率的方法,答案是有滴, 有一个对象参数screen.width  screen.height  可以返回屏幕分辨率参数。哈 有个这个参数,剩下的工作就方便多了。

转载于:https://www.cnblogs.com/nidilzhang/archive/2010/01/10/1643545.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在C++中使用FFmpeg进行屏幕录制和推流获取屏幕分辨率,可以使用FFmpeg的C API。以下是一个简单的示例代码: ```c++ extern "C" { #include <libavutil/avutil.h> #include <libavutil/opt.h> #include <libavformat/avformat.h> #include <libavcodec/avcodec.h> #include <libswscale/swscale.h> #include <libavutil/error.h> } int main() { AVFormatContext *pFormatCtx; AVOutputFormat *pOutputFmt; AVStream *pStream; AVCodecContext *pCodecCtx; AVCodec *pCodec; AVFrame *pFrame; AVPacket pkt; int ret; int width, height; // 初始化FFmpeg av_register_all(); avformat_network_init(); // 打开输出文件 avformat_alloc_output_context2(&pFormatCtx, NULL, NULL, "output.mp4"); if (!pFormatCtx) { printf("Error allocating output context\n"); return -1; } // 添加视频流 pOutputFmt = pFormatCtx->oformat; pStream = avformat_new_stream(pFormatCtx, NULL); if (!pStream) { printf("Error creating new stream\n"); avformat_free_context(pFormatCtx); return -1; } // 设置编码器参数 pCodecCtx = pStream->codec; pCodecCtx->codec_id = pOutputFmt->video_codec; pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO; pCodecCtx->width = 1920; pCodecCtx->height = 1080; pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P; pCodecCtx->time_base = (AVRational){1, 30}; pCodecCtx->bit_rate = 400000; // 查找编码器 pCodec = avcodec_find_encoder(pCodecCtx->codec_id); if (!pCodec) { printf("Codec not found\n"); avformat_free_context(pFormatCtx); return -1; } // 打开编码器 if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) { printf("Error opening codec\n"); avformat_free_context(pFormatCtx); return -1; } // 创建帧 pFrame = av_frame_alloc(); if (!pFrame) { printf("Error allocating frame\n"); avcodec_close(pCodecCtx); avformat_free_context(pFormatCtx); return -1; } pFrame->format = pCodecCtx->pix_fmt; pFrame->width = pCodecCtx->width; pFrame->height = pCodecCtx->height; ret = av_frame_get_buffer(pFrame, 32); if (ret < 0) { printf("Error allocating frame buffer\n"); avcodec_close(pCodecCtx); avformat_free_context(pFormatCtx); return -1; } // 打开屏幕录制设备 AVInputFormat *pInputFmt = av_find_input_format("gdigrab"); AVDictionary *pOptions = NULL; av_dict_set(&pOptions, "framerate", "30", 0); av_dict_set(&pOptions, "draw_mouse", "0", 0); av_dict_set(&pOptions, "offset_x", "0", 0); av_dict_set(&pOptions, "offset_y", "0", 0); av_dict_set(&pOptions, "video_size", "1920x1080", 0); ret = avformat_open_input(&pFormatCtx, "desktop", pInputFmt, &pOptions); if (ret < 0) { char errbuf[AV_ERROR_MAX_STRING_SIZE]; av_strerror(ret, errbuf, AV_ERROR_MAX_STRING_SIZE); printf("Error opening input: %s\n", errbuf); avcodec_close(pCodecCtx); avformat_free_context(pFormatCtx); return -1; } // 读取屏幕分辨率 width = pCodecCtx->width; height = pCodecCtx->height; // 开始录制 avformat_write_header(pFormatCtx, NULL); while (1) { AVFrame *pFrameRaw = av_frame_alloc(); ret = av_read_frame(pFormatCtx, &pkt); if (ret < 0) { if (ret == AVERROR_EOF) { printf("End of file\n"); } else { printf("Error reading frame\n"); } break; } ret = avcodec_send_packet(pCodecCtx, &pkt); if (ret < 0) { printf("Error sending packet\n"); break; } while (ret >= 0) { ret = avcodec_receive_frame(pCodecCtx, pFrameRaw); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { break; } if (ret < 0) { printf("Error receiving frame\n"); break; } sws_scale(sws_getContext(width, height, AV_PIX_FMT_BGRA, pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, SWS_BILINEAR, NULL, NULL, NULL), (const uint8_t *const *)pFrameRaw->data, pFrameRaw->linesize, 0, height, pFrame->data, pFrame->linesize); pFrame->pts = av_gettime(); ret = avcodec_send_frame(pCodecCtx, pFrame); if (ret < 0) { printf("Error sending frame\n"); break; } } av_packet_unref(&pkt); } av_write_trailer(pFormatCtx); avcodec_close(pCodecCtx); avformat_free_context(pFormatCtx); return 0; } ``` 这个示例代码可以录制整个桌面,并将录制的视频输出为MP4格式的文件。其中,`avformat_open_input()`函数打开gdigrab设备,`avcodec_send_frame()`函数将录制的帧发送给编码器进行压缩,`av_write_frame()`函数将压缩后的帧写入输出文件。在代码中,我们也获取了屏幕的分辨率信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值