编写一个基于智芯微猎鹰的c语言API接口的摄像头拍照程序
时间: 2024-06-08 10:09:19 浏览: 148
以下是一个基于智芯微猎鹰的C语言API接口的摄像头拍照程序的示例:
```c
#include "hi_comm_sys.h"
#include "mpi_sys.h"
#include "mpi_vb.h"
#include "mpi_vi.h"
#include "mpi_isp.h"
#include "mpi_ae.h"
#include "mpi_awb.h"
#include "mpi_hdr.h"
#include "mpi_venc.h"
#include "mpi_region.h"
#include "hi_math.h"
#include "hi_type.h"
#define CAPTURE_FILE_PATH "/mnt/sdcard/capture.jpg"
int main(int argc, char *argv[])
{
HI_S32 s32Ret = HI_SUCCESS;
VB_CONFIG_S stVbConf = {0};
VI_DEV ViDev = 0;
VI_PIPE ViPipe = 0;
VI_CHN ViChn = 0;
ISP_PUB_ATTR_S stIspPubAttr = {0};
ISP_EXPOSURE_ATTR_S stExpAttr = {0};
ISP_AWB_ATTR_S stAwbAttr = {0};
ISP_HDR_ATTR_S stHdrAttr = {0};
VENC_CHN VencChn = 0;
VENC_CHN_ATTR_S stVencChnAttr = {0};
VIDEO_FRAME_INFO_S stFrame = {0};
REGION_INFO_S stRegionInfo = {0};
HI_U32 u32BlkSize = 0;
HI_U32 u32BlkCnt = 0;
HI_CHAR *pchYUVBuffer = HI_NULL;
HI_CHAR *pchJpegBuffer = HI_NULL;
/* 初始化视频缓存 */
memset(&stVbConf, 0, sizeof(VB_CONFIG_S));
stVbConf.u32MaxPoolCnt = 128;
stVbConf.astCommPool[0].u32BlkSize = 1920 * 1080 * 2;
stVbConf.astCommPool[0].u32BlkCnt = 4;
/* 初始化视频输入设备 */
s32Ret = HI_MPI_SYS_Init();
s32Ret |= HI_MPI_VB_SetConfig(&stVbConf);
s32Ret |= HI_MPI_VB_Init();
s32Ret |= HI_MPI_VI_SetDevAttr(ViDev, &stIspPubAttr);
s32Ret |= HI_MPI_VI_EnableDev(ViDev);
s32Ret |= HI_MPI_VI_SetChnAttr(ViPipe, ViChn, &stIspPubAttr);
s32Ret |= HI_MPI_VI_EnableChn(ViPipe, ViChn);
/* 初始化ISP模块 */
s32Ret |= HI_MPI_ISP_SetPubAttr(ViPipe, &stIspPubAttr);
s32Ret |= HI_MPI_ISP_Init(ViPipe);
/* 初始化自动曝光模块 */
s32Ret |= HI_MPI_AE_Register(ViPipe, &stExpAttr);
s32Ret |= HI_MPI_AE_Init(ViPipe);
/* 初始化自动白平衡模块 */
s32Ret |= HI_MPI_AWB_Register(ViPipe, &stAwbAttr);
s32Ret |= HI_MPI_AWB_Init(ViPipe);
/* 初始化HDR模块 */
s32Ret |= HI_MPI_HDR_Register(ViPipe, &stHdrAttr);
s32Ret |= HI_MPI_HDR_Init(ViPipe);
/* 初始化视频编码器 */
s32Ret |= HI_MPI_VENC_CreateChn(VencChn, &stVencChnAttr);
s32Ret |= HI_MPI_VENC_RegisterChn(VencChn, &stVencChnAttr);
s32Ret |= HI_MPI_VENC_StartRecvPic(VencChn);
/* 分配视频缓存 */
s32Ret |= HI_MPI_VI_GetFrame(ViPipe, ViChn, &stFrame, 0);
/* 将YUV格式转换成JPEG格式 */
s32Ret |= HI_MPI_VENC_SendFrame(VencChn, &stFrame, 1000);
/* 从编码器接收JPEG格式视频帧 */
s32Ret |= HI_MPI_VENC_GetStream(VencChn, &stFrame, 1000);
/* 将JPEG格式视频帧写入文件 */
pchJpegBuffer = (HI_CHAR *)stFrame.pVirAddr[0];
FILE *pFile = fopen(CAPTURE_FILE_PATH, "wb");
fwrite(pchJpegBuffer, 1, stFrame.u32Len, pFile);
fclose(pFile);
/* 释放视频缓存 */
s32Ret |= HI_MPI_VI_ReleaseFrame(ViPipe, ViChn, &stFrame);
/* 释放视频编码器 */
s32Ret |= HI_MPI_VENC_StopRecvPic(VencChn);
s32Ret |= HI_MPI_VENC_UnRegisterChn(VencChn);
s32Ret |= HI_MPI_VENC_DestroyChn(VencChn);
/* 释放HDR模块 */
s32Ret |= HI_MPI_HDR_Exit(ViPipe);
s32Ret |= HI_MPI_HDR_UnRegister(ViPipe);
/* 释放自动白平衡模块 */
s32Ret |= HI_MPI_AWB_Exit(ViPipe);
s32Ret |= HI_MPI_AWB_UnRegister(ViPipe);
/* 释放自动曝光模块 */
s32Ret |= HI_MPI_AE_Exit(ViPipe);
s32Ret |= HI_MPI_AE_UnRegister(ViPipe);
/* 释放ISP模块 */
s32Ret |= HI_MPI_ISP_Exit(ViPipe);
/* 释放视频输入设备 */
s32Ret |= HI_MPI_VI_DisableChn(ViPipe, ViChn);
s32Ret |= HI_MPI_VI_DisableDev(ViDev);
s32Ret |= HI_MPI_VB_Exit();
s32Ret |= HI_MPI_SYS_Exit();
if (HI_SUCCESS != s32Ret)
{
printf("Failed to capture image!\n");
return -1;
}
printf("Image captured successfully!\n");
return 0;
}
```
需要注意的是,该示例程序只是一个框架,具体的参数和配置需要根据实际情况进行调整和修改。
阅读全文