1.Linux时间戳
#include <time.h>
#include <sys/time.h>
std::string get_time()
{
time_t tt = time(NULL);//这句返回的只是一个时间cuo
tm* t= localtime(&tt);
char ti[128];
sprintf(ti,"%d-%02d-%02d_%02d-%02d-%02d",t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
std::string ttt = ti;
printf("time: %s\n",ti);
return ttt;
}
2.字符串分割(非常实用)头文件不列,win,linux均测试
void SplitString(const string& s, vector<string>& v, const string& c)
{
string::size_type pos1, pos2;
pos2 = s.find(c);
pos1 = 0;
while (string::npos != pos2)
{
v.push_back(s.substr(pos1, pos2 - pos1));
pos1 = pos2 + c.size();
pos2 = s.find(c, pos1);
}
if (pos1 != s.length())
v.push_back(s.substr(pos1));
}
3.string2*,win linux均测试
int str2int(const string str)
{
int temp;
stringstream stream(str);
stream>>temp;
return temp;
}
如果要2其他(float double等),直接将int换成其他
4.opencv Rect按中心点外扩Size
Rect rectCenterScale(Rect rect, Size size)
{
rect = rect + size;
Point pt;
pt.x = cvRound(size.width/2.0);
pt.y = cvRound(size.height/2.0);
return (rect-pt);
}
5.创建文件夹(Linux 系统命令创建文件)
#include <stdio.h>
#include <mutex>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/stat.h>
int creat_dir(std::string dir)
{
if (access(dir.c_str(), 0) == -1)
{
std::cout << dir << " is not existing" << std::endl;
cout << "now make it" << endl;
#ifdef WIN32
int flag=mkdir(dir.c_str());
#endif
#ifdef LINUX
int flag=mkdir(dir.c_str(), 0777);
#endif
if (flag == 0)
{
std::cout << "make successfully" << std::endl;
} else {
std::cout << "make errorly" << std::endl;
}
}
/*如果dir为文件名*/
if (access(dir.c_str(), 0) == -1)
system("touch license.lic");
return 0;
}
这个头文件还真忘了,应该就是其中几个(Linux)。win加预处理器定义或者修改mkdir为_mkdir,头文件为direct.h
6.base642Mat(base64码转opencv Mat)
static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
std::string base64_decode(std::string const &encoded_string) {
int in_len = encoded_string.size();
int i = 0;
int j = 0;
int in_ = 0;
unsigned char char_array_4[4], char_array_3[3];
std::string ret;
while (in_len-- && (encoded_string[in_] != '=')) {
char_array_4[i++] = encoded_string[in_]; in_++;
if (i == 4) {
for (i = 0; i < 4; i++)
char_array_4[i] = base64_chars.find(char_array_4[i]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (i = 0; (i < 3); i++)
ret += char_array_3[i];
i = 0;
}
}
if (i) {
for (j = i; j < 4; j++)
char_array_4[j] = 0;
for (j = 0; j < 4; j++)
char_array_4[j] = base64_chars.find(char_array_4[j]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
}
return ret;
}
cv::Mat base64ToMat(string &base64) {
string decoded_string = base64_decode(base64);
std::vector<uchar> data(decoded_string.begin(), decoded_string.end());
cv::Mat image = cv::imdecode(data, CV_LOAD_IMAGE_UNCHANGED);
return image;
}
7.Mat2Base64(opencv Mat 转base64码)
static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
std::string base64_encode(const char* bytes_to_encode, int in_len) {
std::string ret;
int i = 0;
int j = 0;
unsigned char char_array_3[3];
unsigned char char_array_4[4];
while (in_len--) {
char_array_3[i++] = *(bytes_to_encode++);
if (i == 3) {
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for (i = 0; (i <4); i++)
ret += base64_chars[char_array_4[i]];
i = 0;
}
}
if (i)
{
for (j = i; j < 3; j++)
char_array_3[j] = '\0';
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for (j = 0; (j < i + 1); j++)
ret += base64_chars[char_array_4[j]];
while ((i++ < 3))
ret += '=';
}
return ret;
}
std::string matToBase64(cv::Mat img)
{
std::vector<uchar> data_encode;
cv::imencode(".jpg", img, data_encode);
std::string str_encode(data_encode.begin(), data_encode.end());
string base64String = base64_encode(str_encode.c_str(), str_encode.length());
return base64String;
}
8.json读和取,需要jsoncpp.cpp和json.h。网上一搜一大把
写
std::string getJsonString(parameter *P) {
Json::FastWriter writer;
Json::Value root;
root["flage"] = P->flage;
//root["ID_base64"] = P->ID_base64;
//root["img_base64"] = P->img_base64;
root["up_img_base64"] = P->up_img;
root["db"] = P->db;
//root["Compare_image"] = P->Compare_image;
//root["Feature"] = P->Feature;
//root["Score"] = P->Score;
std::string jsonString = writer.write(root);
return jsonString;
}
读
Json::Reader reader;
Json::Value root;
string ID_base64;
string base_image_base64;
if (reader.parse(postResponseStr, root)) {
ID_base64 = root["ID_Base64"].asString();
base_image_base64 = root["Compare_image"].asString();
}
未完待续
9.Linux 下UDP发送接收图片
/*
clinet.c
by cwf
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define FINISH_FLAG "FILE_TRANSPORT_FINISH"
#define MAXLINE 1024
int Fun_send(char *ip, char *port, char *jpg_path, char *jpg_name)
{
FILE *fp;
struct sockaddr_in serv_addr;
char buf[MAXLINE];
int sock_id;
int read_len;
int send_len;
int serv_addr_len;
int i_ret;
char file_name[256];
struct linger m_sLinger;
int n = 0;
/* 建立udp套接字*/
memset(&serv_addr,0,sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(atoi(port));
inet_pton(AF_INET, ip, &serv_addr.sin_addr);
serv_addr_len = sizeof(serv_addr);
if ((sock_id = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("Create socket failed");
close(sock_id);
return -1;
}
m_sLinger.l_onoff=1;
m_sLinger.l_linger=5;//(容许逗留的时间为5秒)
setsockopt(sock_id,SOL_SOCKET,SO_LINGER,(const char*)&m_sLinger,sizeof(m_sLinger));
i_ret = connect(sock_id, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr));
if (-1 == i_ret) {
printf("Connect socket failed!\n");
close(sock_id);
return -1;
}
/*打开要发送的图片文件*/
sprintf(file_name, "%s%s", jpg_path, jpg_name);
printf("file_name = %s\n", file_name);
if ((fp = fopen(file_name,"r")) == NULL) {
printf("Open file failed\n");
fclose(fp);
close(sock_id);
return -1;
}
/*报文头*/
bzero(buf, MAXLINE);
sprintf(buf, "name=%s", jpg_name);
buf[strlen(buf)] = '\0';
send_len = send(sock_id, buf, strlen(buf)+1, 0);
if ( send_len < 0 )
{
printf("Finish send the end string\n");
fclose(fp);
close(sock_id);
return -1;
}
/*报文体*/
bzero(buf, MAXLINE);
while ( (read_len = fread(buf, sizeof(char), MAXLINE, fp)) > 0 )
{
send_len = send(sock_id, buf, read_len, 0);
if ( send_len < 0 ) {
printf("Send data failed\n");
fclose(fp);
close(sock_id);
return -1;
}
bzero(buf, MAXLINE);
n++;
if(n == 5)
{
n = 0;
usleep(1);
}
}
fclose(fp);
/*报文结束标志*/
bzero(buf, MAXLINE);
strcpy(buf, FINISH_FLAG);
buf[strlen(buf)] = '\0';
send_len = send(sock_id, buf, strlen(buf)+1, 0);
if ( send_len < 0 ) {
printf("Finish send the end string\n");
fclose(fp);
close(sock_id);
return -1;
}
close(sock_id);
printf("Send finish\n");
return 0;
}
//./client 192.168.1.61 8000 /opt/renlian/103/ 3_192.168.1.103.jpg
int main(int argc,char **argv)
{
int i;
if (argc != 5) {
printf("usage :%s ipaddr portnum filename\n", argv[0]);
exit(0);
}
i = Fun_send(argv[1], argv[2], argv[3], argv[4]);
return 0;
}
/*
server.c
by cwf
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define FINISH_FLAG "FILE_TRANSPORT_FINISH"
#define MAXLINE 1024
void Fun_recv(char *jpg_path, char *port)
{
struct sockaddr_in serv_addr;
struct sockaddr_in clie_addr;
char buf[MAXLINE];
int sock_id;
int recv_len;
int clie_addr_len;
FILE *fp;
int nRecvBuf = 32*1024;
char file_name[256];
char file_path[256];
int Port = atoi(port);
char Jpg_path[256];
strcpy(Jpg_path, jpg_path);
while(1)
{
//创建套接字
if ((sock_id = socket(AF_INET,SOCK_DGRAM,0)) < 0) {
printf("Create socket failed\n");
close(sock_id);
exit(0);
}
setsockopt(sock_id,SOL_SOCKET,SO_RCVBUF,(const char*)&nRecvBuf,sizeof(int));
/*绑定套接字*/
memset(&serv_addr,0,sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(Port);
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sock_id,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0 ) {
printf("Bind socket faild\n");
close(sock_id);
exit(0);
}
/* 接收一张图片*/
clie_addr_len = sizeof(clie_addr);
bzero(buf, MAXLINE);
while (recv_len = recvfrom(sock_id, buf, MAXLINE, 0,(struct sockaddr *)&clie_addr, &clie_addr_len)) {
if(recv_len < 0) {
printf("Recieve data from client failed!\n");
break;
}
//printf("#");
/*报文结束标志*/
if ( strstr(buf, FINISH_FLAG) != NULL ) {
printf("\nFinish receiver finish_flag\n");
break;
}
/*解析报文头*/
if ( strstr(buf, "name=") != NULL ) {
printf("\n%s \n",buf);
bzero(file_name, 256);
sscanf(buf, "name=%s", file_name);
printf("file_name = %s\n", file_name);
sprintf(file_path, "%s%s", Jpg_path, file_name);
printf("file_path = %s\n", file_path);
/* 创建收到名字的图片文件*/
if ((fp = fopen(file_path, "w")) == NULL)
{
printf("Creat file failed");
fclose(fp);
close(sock_id);
exit(0);
}
continue;
}
/*报文体*/
int write_length = fwrite(buf, sizeof(char), recv_len, fp);
if (write_length < recv_len) {
printf("File write failed\n");
break;
}
bzero(buf, MAXLINE);
}
printf("Finish recieve\n");
fclose(fp);
close(sock_id);
}
return;
}
//./server /opt/renlian/recv/ 8000
int main(int argc,char **argv)
{
if (argc != 3) {
printf("usage :%s portnum filename\n", argv[0]);
exit(0);
}
Fun_recv(argv[1], argv[2]);
return 0;
}
10.Linux 下获取IP及Mac地址
#include <net/if.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <string>
#define ETH_NAME "eth0" //如果要获取其他网卡的地址,将这个换为其他网卡名称,比如eth0
std::string get_ip()
{
int sockfd;
struct sockaddr_in sin;
struct ifreq ifr;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd == -1) {
perror("socket error");
exit(1);
}
strncpy(ifr.ifr_name, ETH_NAME, IFNAMSIZ); //Interface name
if (ioctl(sockfd, SIOCGIFADDR, &ifr) == 0) { //SIOCGIFADDR 获取interface address
memcpy(&sin, &ifr.ifr_addr, sizeof(ifr.ifr_addr));
return inet_ntoa(sin.sin_addr);
}
}
void get_mac(char * mac_a)
{
int sockfd;
struct ifreq ifr;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd == -1) {
perror("socket error");
exit(1);
}
strncpy(ifr.ifr_name, ETH_NAME, IFNAMSIZ); //Interface name
if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) == 0) { //SIOCGIFHWADDR 获取hardware address
memcpy(mac_a, ifr.ifr_hwaddr.sa_data, 6);
}
}