控制循环系列
1.
#include<iostream>
#include <curses.h>#include <stdio.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
#include <linux/input.h>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
int keys_fd;
char ret[2];
struct input_event t;
keys_fd = open("/dev/input/event1", O_RDONLY);
while(1)
{
cout<<"CHENXUN"<<endl;
if(read(keys_fd,&t,sizeof(t))==0)
continue;
else
break;
}
}
2.
#include <iostream>
#include <stdio.h>
#include <pthread.h>
using namespace std;
void * child1(void *arg)
{
char ch = 0;
while ('=' != (ch = getchar()))
{
;
}
}
void *child2(void *arg)
{
while (1)
{
cout << "chenxun";
sleep(3);
// printf("thread 2 get running.\n");
}
}
int main()
{
pthread_t tid1, tid2;
pthread_create(&tid1, NULL, child1, NULL);
pthread_create(&tid2, NULL, child2, NULL);
pthread_join(tid1, NULL);
pthread_cancel(tid2);
return 0;
}
扫描枪
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
#include <linux/input.h>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
int keys_fd;
char ret[2];
struct input_event t;
string s;
char a[58] = { '0', '0',
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '0', '0',
'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '0', '0',
'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ':', '\'', '0', '@', '\\',
'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', '0', '0', '0', '0'
};
//ofstream my_cout;
//my_cout.open ("barcode.txt");
///
keys_fd = open("/dev/input/event3", O_RDONLY);
if (keys_fd <= 0)
{
printf("open /dev/input/event3 device error!\n");
return 0;
}
int k=0;
string url1 = "curl http://192.168.8.162:3000/api/tagByPi?user=steve02\\&key=7b8e5c2031b85b392891ee13cf782652\\&filename=OA1_08476R-2.jpg\\&cus=";
string url2 = "\\&photoId=pictureOffice201411100000003\\&time=2014-09-11%2020:58:10\\&location=Miki&fav=1\\&pay=1&seat=";
// while(1)
while (read(keys_fd, &t, sizeof (t))>0)
{
// if (read(keys_fd, &t, sizeof (t)) == sizeof (t))
{
// if (t.type == EV_KEY)
if (t.value == 0 || t.value == 1)
{
if (t.value==1)
{
if(t.code==42)
{
k=1;
cout<<t.code<<endl;
}
if(t.code!=42&&t.code!=28)
{
if(k==1&&t.code!=39)
{
cout<<t.code<<endl;
char tmp=a[t.code];
char up_tmp=tmp-32;
//char up_tmp=toupper(tmp);
s.push_back(up_tmp);
}
else
{
cout<<t.code<<endl;
s.push_back(a[t.code]);
}
k=0;
}
if(t.code==28)
{
cout<<t.code<<endl;
cout<<s.size()<<endl;
string photoid;
ifstream input("photoid.txt");
input>>photoid;
input.close();
string bar_command = url1 + photoid + s + url2;
cout<<bar_command<<endl;
system(bar_command.c_str());
cout<<s<<endl;
s.clear();
}
// printf("%d\n", t.code);
// my_cout << a[tmp];
// printf("key %d %s\n", t.code, (t.value) ? "Pressed" : "Released");
}
// printf("key %d %s\n", t.code, (t.value) ? "Pressed" : "Released");
// if (t.code == KEY_ESC)
// break;
}
}
// if (t.code == KEY_ESC)
// break;
}
close(keys_fd);
return 0;
}
linux下图片播放代码
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <curl/curl.h>
#include <stdlib.h>
#include <vector>
#include <string>
#include <sstream>
#include </home/pi/raspi_test/myjson/json.h>
#include<cv.h>
#include<highgui.h>
using namespace std;
using namespace cv;
void int2str(const int &int_temp, string &string_temp)
{
stringstream stream;
stream << int_temp;
string_temp = stream.str();
}
//size_t process_data(void *buffer, size_t size, size_t nmemb, void *user_p)
//{
// FILE *fp = (FILE *)user_p;
// size_t return_size = fwrite(buffer, size, nmemb, fp);
// cout << (char *)buffer << endl;
// return return_size;
//}
int main()
{
string pic1_photoId_index("");
string pic2_photoId_index("");
while(1)
{
cout<<endl;
cout<<endl;
cout<<"start load hte picture:"<<endl;
CURL *easy_handle= curl_easy_init();
if (NULL == easy_handle)
{
cerr << "get a easy handle failed." << endl;
curl_global_cleanup();
return -1;
}
FILE *fp = fopen("host_json.txt","w");
curl_easy_setopt(easy_handle, CURLOPT_URL, "http://192.168.8.3:3000/getPhotoWallLogin?user=steve02&key=670b14728ad9902aecba32e22fa4f6bd&screenCode=sc08" );
//curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, &process_data);
curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, fp);
curl_easy_perform(easy_handle);
fclose(fp);
curl_easy_cleanup(easy_handle);
curl_global_cleanup();
///
ifstream file("/home/pi/raspi_test/host_json.txt");
if (!file)
{
cout << "Open file the host_json.txt fail!!!" << endl;
getchar();
return -1;
}
Json::Value root;
Json::Reader reader;
if (!reader.parse(file, root, false))
{
cout << "Plz check your url make sure you can contact your host" << endl;
getchar();
return -1;
}
int len = root["info"].size();
//string s[4];
//for (int i = 0; i < len; ++i)
//{
// s[i] = root["info"][i]["originalInfo"]["url"].asString();
//}
//for (auto s1 : s)
//{
// cout << s1 << endl;
//}
int index[4] = { 0, 1, 2, 3 };
cout<<"---------------------------------------------------"<<endl;
cout << "上次图片1的id--->" << pic1_photoId_index << endl;
cout << "上次图片2的id--->" << pic2_photoId_index << endl;
string pic1 = root["info"][index[0]]["photoId"].asString();
string pic2 = root["info"][index[1]]["photoId"].asString();
cout<<"!!!!!!!!!!!!!!!!!!!!!!!!!!!!"<<endl;
cout << "图片1id--------->" << pic1 << endl;
cout << "图片id2--------->" << pic2 << endl;
cout<<"---------------------------------------------------"<<endl;
if (pic1 != pic1_photoId_index || pic2 != pic2_photoId_index)
{
pic1_photoId_index = pic1;
pic2_photoId_index = pic2;
vector<string> pic_url;
for (int i = 0; i < len; ++i)
{
pic_url.push_back(root["info"][i]["originalInfo"]["url"].asString());
}
//for (auto s : pic_url)
// cout << s << endl;
string http = root["photoServerIp"].asString();
cout << http << endl;
string command1 = "curl -o 1.jpg " + http +"/"+ pic_url[0];
cout<<command1<<endl;
string command2 = "curl -o 2.jpg " + http +"/"+ pic_url[1];
cout<<command2<<endl;
//string command3 = "curl -0 3.jpg " + http + pic_url[2];
//string command4 = "curl -0 4.jpg " + http + pic_url[3];
system(command1.c_str());//下载第一张图片
system(command2.c_str());//下载第二张图片
system("date >loadpic_time.txt ");
cout<<"更新图片成功"<<endl;
//合成图片
IplImage*img1 = cvLoadImage("1.jpg");
IplImage* img2 = cvLoadImage("2.jpg");
IplImage*img = cvLoadImage("black.jpg", 1);
// IplImage* img = cvCreateImage(cvSize(img1->width + img2->width, img1->height), img1->depth, 3);
//cout << img1->widthStep << endl;
//cout << img2->widthStep << endl;
uchar r1, g1, b1;
for (int i = 0; i < img1->height; i++)
{
uchar *ptrImage = (uchar*)(img1->imageData + i * img1->widthStep);
uchar *ptrDst = (uchar*)(img->imageData + (i+450) * img->widthStep);
for (int j = 0; j < img1->width; j++)
{
b1 = ptrImage[3 * j + 0];
g1 = ptrImage[3 * j + 1];
r1 = ptrImage[3 * j + 2];
ptrDst[3 * j + 0] = b1;
ptrDst[3 * j + 1] = g1;
ptrDst[3 * j + 2] = r1;
}
}
uchar r2, g2, b2;
for (int i = 0; i < img2->height; i++)
{
uchar *ptrImage = (uchar*)(img2->imageData + i * img2->widthStep);
uchar *ptrDst = (uchar*)(img->imageData + (i+450) * img->widthStep);
for (int j = img1->width, k = 0; j < (img2->width + img1->width), k < img2->width; k++, j++)
{
b2 = ptrImage[3 * k + 0];
g2 = ptrImage[3 * k + 1];
r2 = ptrImage[3 * k + 2];
ptrDst[3 * j + 0] = b2;
ptrDst[3 * j + 1] = g2;
ptrDst[3 * j + 2] = r2;
}
}
cvSaveImage("result.jpg", img);
cvReleaseImage( &img );
cvReleaseImage( &img1 );
cvReleaseImage( &img2 );
cout<<"you can display the loaded image"<<endl;
// int k = 0;
// string font_set1 = root["setting"]["displayInfo"][k]["screenTextFormat"].asString();
// cout << font_set1 << endl;
// int x = root["setting"]["displayInfo"][k]["screenTextLocation"]["x"].asInt();
// string x_s;
// int2str(x, x_s);
// int y = root["setting"]["displayInfo"][k]["screenTextLocation"]["y"].asInt();
// string y_s;
// int2str(y, y_s);
//string sssss="welcome \%text\%";
// string display_font = "convert -pointsize 45 -fill \"#F9D02F\" -draw 'text " + x_s + "," + y_s + " " + font_set1 + "\' " + "1.jpg out.jpg";
// cout << display_font << endl;
// system(display_font.c_str());
//system("feh 11.jpg -Z");
}
sleep(3);
}
return 0;
}
-----------------------------------------------------------------------------------------------------------------------------------------------
/************************************************************************
* Author : stallman chen
* Date : 2015/01/17
* Address : pciture works shanghai
* Email : 275076730@qq.com
* function: Show 4 images on only one window
*************************************************************************/
#include<iostream>
#include<fstream>
#include<string>
#include"json.h"
//#include "opencv2/opencv.hpp"
#include "highgui.h"
#include "cv.h"
using namespace cv;
using namespace std;
void get_json_txt()
{
system("curl \"http://192.168.8.3:3000/getPhotoWallLogin?user=steve02&key=670b14728ad9902aecba32e22fa4f6bd&screenCode=sc08\" -o json_data.txt");
}
Json::Value get_json_array()
{
ifstream file("json_data.txt", ios::out);
if (!file)
{
cout << "Open file the json_dat.txt fail!!!" << endl;
getchar();
return -1;
}
Json::Value root;
Json::Reader reader;
if (!reader.parse(file, root, false))
{
cout << "Plz check your url make sure that you can contact your host" << endl;
getchar();
return -1;
}
return root;
}
vector<string> get_url(Json::Value root)
{
vector<string> pic_url;
for (int i = 0; i < 4; ++i)
{
pic_url.push_back(root["info"][i]["originalInfo"]["url"].asString());
}
//for (auto s : pic_url)
// cout << s << endl;
return pic_url;
}
Mat show_4_images(const vector<Mat>& src_img)
{
Mat image_display;
size_t sz = src_img.size();
CvSize src_image_size = cvSize(src_img[0].cols, src_img[0].rows);
//CvSize image_display_size = cvSize(src_image_size.width, src_image_size.height);
//******************** Blank setting ********************//
CvSize DispBlank_Edge = cvSize(200, 200);
CvSize DispBlank_Gap = cvSize(200, 200);
image_display.create(Size(6934, 3900), CV_8UC3);
image_display.setTo(0);//Background
if (sz >= 1)
{
Mat imgROI0 = image_display(Rect(250, 100, src_image_size.width, src_image_size.height));
resize(src_img[0], imgROI0, Size(2400, 1800), INTER_CUBIC);
}
if (sz >= 2)
{
Mat imgROI1 = image_display(Rect(3724, 100, src_image_size.width, src_image_size.height));
resize(src_img[1], imgROI1, Size(2400, 1800), INTER_CUBIC);
}
if (sz >= 3)
{
Mat imgROI2 = image_display(Rect(650, 2000, src_image_size.width, src_image_size.height));
resize(src_img[2], imgROI2, Size(2400, 1800), INTER_CUBIC);
}
if (sz >= 4)
{
Mat imgROI3 = image_display(Rect(4300, 2000, src_image_size.width, src_image_size.height));
resize(src_img[3], imgROI3, Size(2400, 1800), INTER_CUBIC);
}
return image_display;
}
int main()
{
string pic1_photoId_index("");
string pic2_photoId_index("");
string pic3_photoId_index("");
string pic4_photoId_index("");
bool flag = false;
int index[4] = { 0, 1, 2, 3 };
while (1)
{
get_json_txt();
Json::Value root;
root = get_json_array();
int info = root["info"].size();
//cout << root.size() << endl;
cout << info << endl;
bool flag = false;
if (info == 4)
{
cout << "-----------------------------------------------------" << endl;
cout << "上次图片1的id--->" << pic1_photoId_index << endl;
cout << "上次图片2的id--->" << pic2_photoId_index << endl;
cout << "上次图片3的id--->" << pic3_photoId_index << endl;
cout << "上次图片4的id--->" << pic4_photoId_index << endl;
string pic1 = root["info"][index[0]]["photoId"].asString();
string pic2 = root["info"][index[1]]["photoId"].asString();
string pic3 = root["info"][index[2]]["photoId"].asString();
string pic4 = root["info"][index[3]]["photoId"].asString();
cout << "----------------------------------------------------" << endl;
cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
cout << "图片id1--------->" << pic1 << endl;
cout << "图片id2--------->" << pic2 << endl;
cout << "图片id3--------->" << pic3 << endl;
cout << "图片id4--------->" << pic4 << endl;
cout << "---------------------------------------------------" << endl;
if (pic1 != pic1_photoId_index)
{
ofstream input("photoid.txt");
input << pic1 << endl;
input.close();
vector<string>pic_url;
pic_url = get_url(root);
string http = root["photoServerIp"].asString();
cout << pic_url.size() << endl;
string command1 = "curl -o 1.jpg " + http + "/" + pic_url[0];
string command2 = "curl -o 2.jpg " + http + "/" + pic_url[1];
string command3 = "curl -o 3.jpg " + http + "/" + pic_url[2];
string command4 = "curl -o 4.jpg " + http + "/" + pic_url[3];
cout << command1 << endl;
cout << command2 << endl;
cout << command3 << endl;
cout << command4 << endl;
cout << "---------------------------------------------------" << endl;
cout << "---------------------------------------------------" << endl;
system(command1.c_str());
system(command2.c_str());
system(command3.c_str());
system(command4.c_str());
pic1_photoId_index = pic1;
pic2_photoId_index = pic2;
pic3_photoId_index = pic3;
pic4_photoId_index = pic4;
vector<Mat> imgs;
imgs.push_back(imread("1.jpg"));
imgs.push_back(imread("2.jpg"));
imgs.push_back(imread("3.jpg"));
imgs.push_back(imread("4.jpg"));
Mat display_img = show_4_images(imgs);
namedWindow("pic_viewer", CV_WINDOW_NORMAL);
setWindowProperty("pic_viewer", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
imshow("pic_viewer", display_img);
waitKey(10);
}
}
if (info == 3)
{
cout << "-----------------------------------------------------" << endl;
cout << "上次图片1的id--->" << pic1_photoId_index << endl;
cout << "上次图片2的id--->" << pic2_photoId_index << endl;
cout << "上次图片3的id--->" << pic3_photoId_index << endl;
cout << "上次图片4的id--->" << pic4_photoId_index << endl;
string pic1 = root["info"][index[0]]["photoId"].asString();
string pic2 = root["info"][index[1]]["photoId"].asString();
string pic3 = root["info"][index[2]]["photoId"].asString();
string pic4 = pic4_photoId_index;
cout << "----------------------------------------------------" << endl;
cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
cout << "图片id1--------->" << pic1 << endl;
cout << "图片id2--------->" << pic2 << endl;
cout << "图片id3--------->" << pic3 << endl;
cout << "图片id4--------->" << pic4 << endl;
cout << "---------------------------------------------------" << endl;
if (pic1 != pic1_photoId_index)
{
ofstream input("photoid.txt");
input << pic1 << endl;
input.close();
vector<string>pic_url;
pic_url = get_url(root);
string http = root["photoServerIp"].asString();
cout << pic_url.size() << endl;
string command1 = "curl -o 1.jpg " + http + "/" + pic_url[0];
string command2 = "curl -o 2.jpg " + http + "/" + pic_url[1];
string command3 = "curl -o 3.jpg " + http + "/" + pic_url[2];
cout << command1 << endl;
cout << command2 << endl;
cout << command3 << endl;
cout << "---------------------------------------------------" << endl;
cout << "---------------------------------------------------" << endl;
system(command1.c_str());
system(command2.c_str());
system(command3.c_str());
pic1_photoId_index = pic1;
pic2_photoId_index = pic2;
pic3_photoId_index = pic3;
vector<Mat> imgs;
imgs.push_back(imread("1.jpg"));
imgs.push_back(imread("2.jpg"));
imgs.push_back(imread("3.jpg"));
Mat display_img = show_4_images(imgs);
namedWindow("pic_viewer", CV_WINDOW_NORMAL);
setWindowProperty("pic_viewer", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
imshow("pic_viewer", display_img);
waitKey(10);
}
}
if (info == 2)
{
cout << "-----------------------------------------------------" << endl;
cout << "上次图片1的id--->" << pic1_photoId_index << endl;
cout << "上次图片2的id--->" << pic2_photoId_index << endl;
cout << "上次图片3的id--->" << pic3_photoId_index << endl;
cout << "上次图片4的id--->" << pic4_photoId_index << endl;
string pic1 = root["info"][index[0]]["photoId"].asString();
string pic2 = root["info"][index[1]]["photoId"].asString();
string pic3 = pic4_photoId_index;
string pic4 = pic4_photoId_index;
cout << "----------------------------------------------------" << endl;
cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
cout << "图片id1--------->" << pic1 << endl;
cout << "图片id2--------->" << pic2 << endl;
cout << "图片id3--------->" << pic3 << endl;
cout << "图片id4--------->" << pic4 << endl;
cout << "---------------------------------------------------" << endl;
if (pic1 != pic1_photoId_index)
{
ofstream input("photoid.txt");
input << pic1 << endl;
input.close();
vector<string>pic_url;
pic_url = get_url(root);
string http = root["photoServerIp"].asString();
cout << pic_url.size() << endl;
string command1 = "curl -o 1.jpg " + http + "/" + pic_url[0];
string command2 = "curl -o 2.jpg " + http + "/" + pic_url[1];
cout << command1 << endl;
cout << command2 << endl;
cout << "---------------------------------------------------" << endl;
cout << "---------------------------------------------------" << endl;
system(command1.c_str());
system(command2.c_str());
pic1_photoId_index = pic1;
pic2_photoId_index = pic2;
vector<Mat> imgs;
imgs.push_back(imread("1.jpg"));
imgs.push_back(imread("2.jpg"));
Mat display_img = show_4_images(imgs);
namedWindow("pic_viewer", CV_WINDOW_NORMAL);
setWindowProperty("pic_viewer", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
imshow("pic_viewer", display_img);
waitKey(10);
}
}
if (info == 1)
{
cout << "-----------------------------------------------------" << endl;
cout << "上次图片1的id--->" << pic1_photoId_index << endl;
cout << "上次图片2的id--->" << pic2_photoId_index << endl;
cout << "上次图片3的id--->" << pic3_photoId_index << endl;
cout << "上次图片4的id--->" << pic4_photoId_index << endl;
string pic1 = root["info"][index[0]]["photoId"].asString();
string pic2 = pic4_photoId_index;
string pic3 = pic4_photoId_index;
string pic4 = pic4_photoId_index;
cout << "----------------------------------------------------" << endl;
cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
cout << "图片id1--------->" << pic1 << endl;
cout << "图片id2--------->" << pic2 << endl;
cout << "图片id3--------->" << pic3 << endl;
cout << "图片id4--------->" << pic4 << endl;
cout << "---------------------------------------------------" << endl;
if (pic1 != pic1_photoId_index)
{
ofstream input("photoid.txt");
input << pic1 << endl;
input.close();
vector<string>pic_url;
pic_url = get_url(root);
string http = root["photoServerIp"].asString();
cout << pic_url.size() << endl;
string command1 = "curl -o 1.jpg " + http + "/" + pic_url[0];
cout << command1 << endl;
cout << "---------------------------------------------------" << endl;
cout << "---------------------------------------------------" << endl;
system(command1.c_str());
pic1_photoId_index = pic1;
vector<Mat> imgs;
imgs.push_back(imread("1.jpg"));
Mat display_img = show_4_images(imgs);
namedWindow("pic_viewer", CV_WINDOW_NORMAL);
setWindowProperty("pic_viewer", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
imshow("pic_viewer", display_img);
waitKey(10);
}
}
if (info == 0)
{
namedWindow("pic_viewer", CV_WINDOW_NORMAL);
setWindowProperty("pic_viewer", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
Mat black = imread("result.jpg");
imshow("pic_viewer", black);
cvWaitKey(2);
}
}
}