OpenCV Call Camera Photo code

Source: Internet
Author: User

Recently in the study of OpenCV's call to the camera. Now put the code out for everyone to criticize.

1. Affirm

#include "./opencv2/opencv.hpp" #ifdef _debug#pragma comment (lib, "Opencv_core249d.lib") #pragma comment (lib, "opencv_ Highgui249d.lib ") #else #pragma comment (lib," Opencv_core249.lib ") #pragma comment (lib," Opencv_highgui249.lib ") # Endifusing namespace Cv;class Copencvcameractrl:p ublic ccamerabase{public:videocapture Vc;public:copencvcameractrl ( void), ~copencvcameractrl (void);p Ublic:bool Opencamera (HWND win, CString Strvid, CString strpid);//Open Camera BOOL photopic ( CString strsavefile); void Closecamera ();};


2. Turn on the camera

Turn on webcam bool Copencvcameractrl::opencamera (HWND win, CString Strvid, CString strpid) {Initcamerawin (win); vid = Strvid; PID = Strpid;nindex = Getcameraindexinos (vid, PID); if (NIndex < 0) {return FALSE;} if (!vc.open (NIndex)) {return FALSE;} Vc.set (Cv_cap_prop_frame_width, nwidth); Vc.set (Cv_cap_prop_frame_height, nhight); if (!vc.isopened ()) {BOpen = false; return FALSE;} bopen = True;return true;}


3. Turn off the camera

Turn off camera void Copencvcameractrl::closecamera () {if (vc.isopened ()) {Vc.release ()}}


4. Take photos and save to local

camera bool Copencvcameractrl::P hotopic (CString strsavefile) {Mat frame;vc >> frame;vc >> frame;if (! Frame.data) {return FALSE;} Set image quality Vector<int> compression_params;compression_params.push_back (imwrite_jpeg_quality); compression_ Params.push_back (Imwrite); Strsavefile.getbuffer (0), frame, compression_params); Strsavefile.releasebuffer (); return TRUE;}


Note: Every time you get data from the camera to the mat, you need to do it twice.

Mat FRAME;VC >> frame;vc >> frame;if (!frame.data) {return FALSE;}

If executed only once, there are two problems:

A, if you turn on the camera immediately call photo save, the first time will fail, get no data

B, each shot saved picture is the last picture

OpenCV Call Camera Photo code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.