#include "camera.h"
int cam_fd;
rgb_buffers rgb_buf;
extern int send_data(const char *data,const unsigned int length);
extern SHARED *g_data;
static void cap_abilities(__u32 capabilities)
{
printf("capabilities:");
if(capabilities & V4L2_CAP_VIDEO_CAPTURE) fprintf(stdout,"\tV4L2_CAP_VIDEO_CAPTURE");
if(capabilities & V4L2_CAP_VIDEO_OUTPUT) fprintf(stdout,"\tV4L2_CAP_VIDEO_OUTPUT");
if(capabilities & V4L2_CAP_VIDEO_OVERLAY) fprintf(stdout,"\tV4L2_CAP_VIDEO_OVERLAY");
if(capabilities & V4L2_CAP_VBI_CAPTURE) fprintf(stdout,"\tV4L2_CAP_VBI_CAPTURE");
if(capabilities & V4L2_CAP_VBI_OUTPUT) fprintf(stdout,"\tV4L2_CAP_VBI_OUTPUT");
if(capabilities & V4L2_CAP_SLICED_VBI_CAPTURE) fprintf(stdout,"\tV4L2_CAP_SLICED_VBI_CAPTURE");
if(capabilities & V4L2_CAP_SLICED_VBI_OUTPUT) fprintf(stdout,"\tV4L2_CAP_SLICED_VBI_OUTPUT");
if(capabilities & V4L2_CAP_RDS_CAPTURE) fprintf(stdout,"\tV4L2_CAP_RDS_CAPTURE");
if(capabilities & V4L2_CAP_VIDEO_OUTPUT_OVERLAY) fprintf(stdout,"\tV4L2_CAP_VIDEO_OUTPUT_OVERLAY");
if(capabilities & V4L2_CAP_HW_FREQ_SEEK) fprintf(stdout,"\tV4L2_CAP_HW_FREQ_SEEK");
if(capabilities & V4L2_CAP_RDS_OUTPUT) fprintf(stdout,"\tV4L2_CAP_RDS_OUTPUT");
if(capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE) fprintf(stdout,"\tV4L2_CAP_VIDEO_CAPTURE_MPLANE");
if(capabilities & V4L2_CAP_VIDEO_OUTPUT_MPLANE) fprintf(stdout,"\tV4L2_CAP_VIDEO_OUTPUT_MPLANE");
if(capabilities & V4L2_CAP_TUNER ) fprintf(stdout,"\tV4L2_CAP_TUNER ");
if(capabilities & V4L2_CAP_AUDIO) fprintf(stdout,"\tV4L2_CAP_AUDIO");
if(capabilities & V4L2_CAP_RADIO) fprintf(stdout,"\tV4L2_CAP_RADIO");
if(capabilities & V4L2_CAP_MODULATOR) fprintf(stdout,"\tV4L2_CAP_MODULATOR");
if(capabilities & V4L2_CAP_READWRITE) fprintf(stdout,"\tV4L2_CAP_READWRITE");
if(capabilities & V4L2_CAP_ASYNCIO) fprintf(stdout,"\tV4L2_CAP_ASYNCIO");
if(capabilities & V4L2_CAP_STREAMING) fprintf(stdout,"\tV4L2_CAP_STREAMING");
}
int camera_init(const char *devpath, unsigned int *width, unsigned int *height, int *size )
{
F_STR;
cam_fd = open(devpath, O_RDWR);
if(cam_fd == -1){
perror("dev open :\n");
return FAILED;
}
//Show device info
struct v4l2_capability cap;
if(ioctl(cam_fd, VIDIOC_QUERYCAP, &cap) < 0){
perror("ioctl");
return FAILED;
}
printf("\nDriver Info:\nDriverName: %s\nCard Name: %s\nBus info: %s\nDriverVersion: %u.%u.%u\n",
cap.driver, cap.card, cap.bus_info, (cap.version>>16)&0xFF, (cap.version>>8)&0xFF, cap.version&0xFF);
cap_abilities(cap.capabilities);
printf("\n\n");
//Set video capture format
struct v4l2_format fmt;
memset(&fmt, 0, sizeof(fmt));
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmt.fmt.pix.width = *width;
fmt.fmt.pix.height = *height;
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
if(ioctl(cam_fd, VIDIOC_S_FMT, &fmt) == -1){
perror("VIDIOC_S_FMT\n");
return -1;
}
if(ioctl(cam_fd, VIDIOC_G_FMT, &fmt) == -1){
perror("VIDIOC_G_FMT\n");
return -1;
}
printf("Current data format information:\n\twidth:%d\theight:%d\n\n",fmt.fmt.pix.width,fmt.fmt.pix.height);
//Get buffer * 4
struct v4l2_requestbuffers req;
memset(&req, 0, sizeof(req));
req.count = 4;
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
req.memory = V4L2_MEMORY_MMAP;
if(ioctl(cam_fd, VIDIOC_REQBUFS, &req) == -1){
return FAILED;
}
//buffers = calloc(req.count, sizeof(*buffers));
//Get buffer address and length
struct v4l2_buffer buf;
for(int numBufs = 0; numBufs < req.count; numBufs++){
memset(&buf, 0, sizeof(buf));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = numBufs;
//Mapping
if(ioctl(cam_fd, VIDIOC_QUERYBUF, &buf) == -1){
return FAILED;
}
buffers[numBufs].length = buf.length;
buffers[numBufs].start = mmap(NULL, buf.length, PROT_READ | PROT_WRITE,
MAP_SHARED, cam_fd, buf.m.offset);
if(buffers[numBufs].start == MAP_FAILED){
return FAILED;
}
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
//Put in buffer queue
if(ioctl(cam_fd, VIDIOC_QBUF, &buf) == -1){
return FAILED;
}
}
*width = fmt.fmt.pix.width;
*height = fmt.fmt.pix.height;
*size = buffers[0].length;
F_END;
return SECCESS;
}
int camera_start(int fd)
{
int ret;
enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
ret = ioctl(fd, VIDIOC_STREAMON, &type);
if (ret == -1) {
return FAILED;
}
return SECCESS;
}
///Write JPEG buffer to file
int camera_jpeg_gather(char *buffers,int len)
{
FILE *fp;
if((fp=fopen("./capture.jpg","w+"))==NULL)
{
perror("Capture JPEG:");
return -1;
}
fwrite(buffers,len,1,fp);
fclose(fp);
return 0;
}
//YUYV to RGB
void convert_yuv_to_rgb(char *yuv, char *rgb, int width, int height,unsigned int bps)
{
unsigned int i;
int y1, y2, u, v;
unsigned char *src = yuv;
unsigned char *dst = rgb;
unsigned int count = width * height / 2;
switch (bps) {
case 24:
for (i = 0; i < count; i++) {
y1 = *src++;
u = *src++;
y2 = *src++;
v = *src++;
*dst++ = ROUND_0_255(y1 + radj[v]);
*dst++ = ROUND_0_255(y1 - gadj1[u] - gadj2[v]);
*dst++ = ROUND_0_255(y1 + badj[u]);
*dst++ = ROUND_0_255(y2 + radj[v]);
*dst++ = ROUND_0_255(y2 - gadj1[u] - gadj2[v]);
*dst++ = ROUND_0_255(y2 + badj[u]);
}
break;
}
}
//RGB to JPEG
int convert_rgb_to_jpg_work(char *rgb, char *jpeg, unsigned int width, unsigned int height, unsigned int bpp, int quality)
{
// init
memset(&jinfo, 0, sizeof(struct jpeg_mgr_info));
jinfo.cinfo.err = jpeg_std_error(&jinfo.jerr);
jpeg_create_compress(&jinfo.cinfo);
jinfo.written = width * height * bpp / 3;
jpeg_mem_dest(&jinfo.cinfo, (unsigned char **)&jpeg, &jinfo.written);
jinfo.cinfo.image_width = width;
jinfo.cinfo.image_height = height;
jinfo.cinfo.input_components = bpp / 8;
jinfo.cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&jinfo.cinfo);
jpeg_set_quality(&jinfo.cinfo, quality, TRUE);
jpeg_start_compress(&jinfo.cinfo, TRUE);
while(jinfo.cinfo.next_scanline < height) {
jinfo.row_pointer[0] = rgb + jinfo.cinfo.next_scanline * width * bpp / 8;
jpeg_write_scanlines(&jinfo.cinfo, jinfo.row_pointer, 1);
// jinfo.cinfo.next_scanline++;
}
jpeg_finish_compress(&jinfo.cinfo);
return (jinfo.written);
}
//RGB to BMP and save
void savebmp(char * pdata, char * bmp_file, int width, int height )
{
int size = width*height*3*sizeof(char); //3 bytes per pixel
//Bitmap part 1, file information
BMPFILEHEADER_T bfh;
bfh.bfType = (WORD)0x4d42; //bm
bfh.bfSize = size // data size
+ sizeof( BMPFILEHEADER_T ) // first section size
+ sizeof( BMPINFOHEADER_T ) // second section size
;
bfh.bfReserved1 = 0; // reserved
bfh.bfReserved2 = 0; // reserved
bfh.bfOffBits = sizeof( BMPFILEHEADER_T )+ sizeof( BMPINFOHEADER_T ); //Real data start index
//Bitmap part 2, data
BMPINFOHEADER_T bih;
bih.biSize = sizeof(BMPINFOHEADER_T);
bih.biWidth = width;
bih.biHeight = -height;//BMP image is scanned from the last point.When displayed,the image is upside down,so use -height
bih.biPlanes = 1;
bih.biBitCount = 24;
bih.biCompression = 0; //No compression
bih.biSizeImage = size;
bih.biXPelsPerMeter = 2835 ;
bih.biYPelsPerMeter = 2835 ;
bih.biClrUsed = 0;
bih.biClrImportant = 0;
FILE * fp = fopen( bmp_file,"wb" );
if( !fp ){
perror("bmp_file");
return ;
}
fwrite( &bfh, 8, 1, fp );
fwrite(&bfh.bfReserved2, sizeof(bfh.bfReserved2), 1, fp);
fwrite(&bfh.bfOffBits, sizeof(bfh.bfOffBits), 1, fp);
fwrite( &bih, sizeof(BMPINFOHEADER_T),1,fp );
fwrite(pdata,size,1,fp);
fclose( fp );
}
/*
//Video output settings
struct v412_input input;
ioctl(cam_fd, V
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
【文件介绍】 Linux 目录存放服务器(树莓派)的代码 QT 目录存放客户端(PC)的代码 【物理连接】 树莓派 GPIO_1 连接 DHT11(使用 WiringPi库) 树莓派 GPIO_4 连接 红外 树莓派 GPIO_5 连接 蜂鸣器 树莓派 USB2.0 连接 摄像头 客户端 通过网线或无线连接树莓派(服务器端) 【软件依赖】 wiringpi(2.52) libjpeg-dev(1.5.2) 【功能介绍】 火灾报警、拜访提示音、闯入报警 读取dht11 温湿度值校验 多连接支持,pthread & mutex QT基本界面 视频 日志 本项目实现了家庭监控系统的基本功能,包括温湿度显示、视频显示、有人提醒、火灾报警、日志功能
资源推荐
资源详情
资源评论
















收起资源包目录











































共 34 条
- 1
资源评论

- 云隙焰火2023-12-21资源质量不错,和资源描述一致,内容详细,对我很有用。
- caorenyiyindao2023-11-21资源太好了,解决了我当下遇到的难题,抱紧大佬的大腿~manylinux2023-12-14嗯嗯,感谢您的支持啊

manylinux
- 粉丝: 5092
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 物联网发展与技术研究.docx
- 基于微信小程序的党建系统的设计与开发.docx
- 健康医疗大数据分析-资本市场趋于成熟-集中趋势明显.docx
- C程序设计期末上机考试经典题.doc
- VB期末考试题B卷.doc
- 基于PLC电梯控制系统毕业设计.doc
- 智慧城市公厕建设与管理研究.docx
- 电子商务中消费者隐私保护问题研究毕业论文.doc
- 加强计算机网络应用安全性的对策.docx
- PID算法的烤箱温控制系统设计.doc
- 网络营销推广宝典.doc
- 信息与物理世界的本质探讨
- 欢聚集团推出的 VNN:高性能轻量神经网络部署框架,赋能多场景 AI 应用
- 基于MATLAB的信号与系统实验仿真系统的设计连续信号分析模块.doc
- 物联网与人工智能关键技术.docx
- 计算机仿真技术在数控专业教学的应用.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
