// Base64ToImg Function
cv::Mat Base64ToImg(string receive_data)
{
	// data1에 들어온 image의 헤더부분 나눠주기	nodejs에서 오는 이미지 데이터는 data:image/png;base64, 라는 header가 붙는다.
	istringstream iss(receive_data);
	string img_header = "";
	string img_base64 = "";
	getline(iss, img_header, ',');
	getline(iss, img_base64, ',');

	// base64 to image
	string dec_jpg = base64_decode(img_base64);
	vector<uchar> data(dec_jpg.begin(), dec_jpg.end());
	cv::Mat img = cv::imdecode(cv::Mat(data), 1);

	return img;
}


// ImgToBase64 Function
string ImgToBase64(cv::Mat img)
{
	// image to base64
	vector<uchar> buf;
	cv::imencode(".png", img, buf);
	auto *enc_msg = reinterpret_cast<unsigned char*>(buf.data());
	string encoded = base64_encode(enc_msg, buf.size());

	// Header 붙여주기
	string base64 = "data:image/png;base64," + encoded;

	return base64;
}

'Language > C++' 카테고리의 다른 글

C++ 시간 재기  (0) 2022.03.14
Mat 형식 Binary file로 저장 및 Binary file 불러오기  (0) 2022.02.22
opencv vector to Mat  (0) 2021.12.23
파일 랜덤 추출  (0) 2021.10.14
C++ 폴더 존재유무 및 생성  (0) 2021.10.08

+ Recent posts