資源簡介
利用opencv打開網絡攝像頭,并可以實時編碼,編碼后的數據保存在本地。
(win7+opencv3.0+海康威視)
代碼片段和文件信息
#include?
#include?
#include?
#include?“Windows.h“
#include?“HCNetSDK.h“
#include?“plaympeg4.h“
#include?
#include?“cv.h“
#include?“highgui.h“
#include??
#include?“stdafx.h“
#include?“stdint.h“?
#include?“opencv2/highgui/highgui.hpp“
#include?“opencv2/imgproc/imgproc.hpp“
#include?
#include?
using?namespace?cv;
using?namespace?std;
extern?“C“
{
#include?“x264.h“
#include?“x264_config.h“
};
LONG?nPort?=?-1;?
char?filename[100];
volatile?int?gbHandling?=?3;
int?ct?=?0;
int?w?=?640;
int?h?=?480;
int?yuv_bufLen?=?w?*?h?*?3?/?2;
unsigned?char*?pYuvBuf?=?new?unsigned?char[yuv_bufLen];
FILE*?pFileOut?=?fopen(“result.yuv“?“w+“);
Mat?dst;
int?MyYuvtoH264(int?width?int?height?string?filename)
{
int?fps?=?15;
size_t?yuv_size?=?width?*?height?*?3?/?2;
x264_t?*encoder;
x264_picture_t?pic_in?pic_out;
uint8_t?*yuv_buffer;
x264_param_t?param;
x264_param_default_preset(¶m?“veryfast“?“zerolatency“);
param.i_threads?=?1;
param.i_width?=?width;
param.i_height?=?height;
param.i_fps_num?=?fps;
param.i_fps_den?=?1;
param.i_keyint_max?=?25;
param.b_intra_refresh?=?1;
param.b_annexb?=?1;
x264_param_apply_profile(¶m?“baseline“);
encoder?=?x264_encoder_open(¶m);
x264_picture_alloc(&pic_in?X264_CSP_I420?width?height);
yuv_buffer?=?(uint8_t*)malloc(yuv_size);
pic_in.img.plane[0]?=?yuv_buffer;
pic_in.img.plane[1]?=?pic_in.img.plane[0]?+?width?*?height;
pic_in.img.plane[2]?=?pic_in.img.plane[1]?+?width?*?height?/?4;
int64_t?i_pts?=?0;
x264_nal_t?*nals;
int?nnal;
FILE?*inf?=?fopen(filename.c_str()?“rb“);
FILE?*outf?=?fopen(“C:/Users/Administrator/Desktop/test.h264“?“wb“);
if?(NULL?==?inf)
{
return?-1;
}
while?(fread(yuv_buffer?1?yuv_size?inf)?>?0)
{
pic_in.i_pts?=?i_pts++;
x264_encoder_encode(encoder?&nals?&nnal?&pic_in?&pic_out);
x264_nal_t?*nal;
for?(nal?=?nals;?nal? {
fwrite(nal->p_payload?1?nal->i_payload?outf);
}
}
x264_encoder_close(encoder);
fclose(inf);
fclose(outf);
free(yuv_buffer);
return?0;
}
//解碼回調?視頻為YUV數據(YV12),音頻為PCM數據
void?CALLBACK?DecCBFun(long?nPort?char?*?pBuf?long?nSize?frame_INFO?*?pframeInfo?long?nReserved1?long?nReserved2)
{
if?(gbHandling)
{
gbHandling--;
return;
}
long?lframeType?=?pframeInfo->nType;
if?(lframeType?==?T_YV12)
{
Mat?pImg(pframeInfo->nHeight?pframeInfo->nWidth?CV_8UC3);
Mat?src(pframeInfo->nHeight?+?pframeInfo->nHeight?/?2?pframeInfo->nWidth?CV_8UC1?pBuf);
cvtColor(src?pImg?CV_YUV2BGR_YV12);
int?resize_height?=?640;
int?resize_width?=?480;
resize(pImg?dst?cv::Size(resize_height?resize_width)?0?0?INTER_LINEAR);
ct++;
cout?<“ct為:“?<
cv::Mat?yuvImg;
cv::cvtColor(dst?yuvImg?CV_BGR2YUV_I420);
memcpy(pYuvBuf?yuvImg.data?yuv_bufLen*sizeof(unsigned?char));
fwrit
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????415744??2017-08-02?15:47??X264-encode?3\Debug\AnalyzeData.dll
?????文件?????428544??2017-08-02?15:47??X264-encode?3\Debug\AudioIntercom.dll
?????文件?????146432??2017-08-02?15:47??X264-encode?3\Debug\AudioRender.dll
?????文件?????463872??2017-08-02?15:57??X264-encode?3\Debug\HCAlarm.dll
?????文件????1144320??2017-08-02?15:55??X264-encode?3\Debug\HCCore.dll
?????文件?????814592??2017-08-02?15:57??X264-encode?3\Debug\HCCoreDevCfg.dll
?????文件?????612864??2017-08-02?15:58??X264-encode?3\Debug\HCDisplay.dll
?????文件????1026560??2017-08-02?15:58??X264-encode?3\Debug\HCGeneralCfgMgr.dll
?????文件?????750592??2017-08-02?15:58??X264-encode?3\Debug\HCIndustry.dll
?????文件?????665600??2017-08-02?15:59??X264-encode?3\Debug\HCNetSDK.dll
?????文件?????415744??2017-08-02?15:47??X264-encode?3\Debug\HCNetSDKCom\AnalyzeData.dll
?????文件?????428544??2017-08-02?15:47??X264-encode?3\Debug\HCNetSDKCom\AudioIntercom.dll
?????文件?????463872??2017-08-02?15:57??X264-encode?3\Debug\HCNetSDKCom\HCAlarm.dll
?????文件??????13908??2017-08-02?15:57??X264-encode?3\Debug\HCNetSDKCom\HCAlarm.lib
?????文件?????814592??2017-08-02?15:57??X264-encode?3\Debug\HCNetSDKCom\HCCoreDevCfg.dll
?????文件?????612864??2017-08-02?15:58??X264-encode?3\Debug\HCNetSDKCom\HCDisplay.dll
?????文件????1026560??2017-08-02?15:58??X264-encode?3\Debug\HCNetSDKCom\HCGeneralCfgMgr.dll
?????文件??????48542??2017-08-02?15:58??X264-encode?3\Debug\HCNetSDKCom\HCGeneralCfgMgr.lib
?????文件?????750592??2017-08-02?15:58??X264-encode?3\Debug\HCNetSDKCom\HCIndustry.dll
?????文件?????418816??2017-08-02?15:58??X264-encode?3\Debug\HCNetSDKCom\HCPlayBack.dll
?????文件?????617472??2017-08-02?15:58??X264-encode?3\Debug\HCNetSDKCom\HCPreview.dll
?????文件??????37192??2017-08-02?15:57??X264-encode?3\Debug\HCNetSDKCom\HCPreview.lib
?????文件?????600576??2017-08-02?15:57??X264-encode?3\Debug\HCNetSDKCom\HCVoiceTalk.dll
?????文件?????978432??2017-08-02?15:47??X264-encode?3\Debug\HCNetSDKCom\libiconv2.dll
?????文件?????655872??2017-08-02?15:47??X264-encode?3\Debug\HCNetSDKCom\msvcr90.dll
?????文件?????427008??2017-08-02?15:47??X264-encode?3\Debug\HCNetSDKCom\OpenAL32.dll
?????文件?????410112??2017-08-02?15:47??X264-encode?3\Debug\HCNetSDKCom\StreamTransClient.dll
?????文件?????653312??2017-08-02?15:47??X264-encode?3\Debug\HCNetSDKCom\SystemTransform.dll
?????文件?????418816??2017-08-02?15:58??X264-encode?3\Debug\HCPlayBack.dll
?????文件?????617472??2017-08-02?15:58??X264-encode?3\Debug\HCPreview.dll
............此處省略270個文件信息
評論
共有 條評論