Chinaunix首页 | 论坛 | 博客
  • 博客访问: 413937
  • 博文数量: 73
  • 博客积分: 3120
  • 博客等级: 中校
  • 技术积分: 785
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-20 12:26
文章分类

全部博文(73)

文章存档

2013年(4)

2012年(10)

2011年(32)

2010年(27)

分类: LINUX

2012-06-18 17:25:17

X11 截图

点击(此处)折叠或打开

  1. /***************************************************************************

  2. Written by WuXiao 2010.05.23
  3. Compile this code using:
  4. gcc -o output main.c -lX11 -ljpeg

  5. ***************************************************************************/
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <sys/time.h>

  10. #include <X11/Xlib.h>

  11. #include <jpeglib.h>



  12. /***************************************************************
  13. Write XImage data to a JPEG file

  14. Must include <jpeglib.h>
  15. Return value:
  16. 0 - failed
  17. 1 - success
  18. ****************************************************************/
  19. static char err_str[256];
  20. int JpegWriteFileFromImage(char *filename, XImage* img)
  21. {
  22. FILE* fp;
  23. struct jpeg_compress_struct cinfo;
  24. struct jpeg_error_mgr jerr;

  25. fp = fopen(filename,"wb");
  26. if(fp==NULL)
  27. {    
  28. memset(err_str, 0, sizeof(err_str));    
  29. sprintf(err_str,"cannot write file %s",filename);
  30. printf("JpegWriteFileFromImage %s",err_str);
  31. return 0;
  32. }



  33. fwrite(img->data,1,img->width*img->height*img->bits_per_pixel/8,fp);

  34. printf("img.width=%d,img.height=%d,img.bits_per_pixel=%d depth=%d\n",img->width,img->height,img->bits_per_pixel,img->depth);

  35. cinfo.err = jpeg_std_error(&jerr);
  36. jpeg_create_compress(&cinfo);

  37. jpeg_stdio_dest(&cinfo,fp);
  38. cinfo.image_width = img->width;
  39. cinfo.image_height = img->height;
  40. cinfo.input_components = 3;
  41. cinfo.in_color_space = JCS_RGB;

  42. jpeg_set_defaults(&cinfo);
  43. jpeg_start_compress(&cinfo,TRUE);

  44. JSAMPROW row_pointer[1];
  45. unsigned char* pBuf = (unsigned char*)malloc(3*img->width);
  46. row_pointer[0] = pBuf;
  47. printf("img.width=%d,img.height=%d,img.bits_per_pixel=%d depth=%d\n",img->width,img->height,img->bits_per_pixel,img->depth);
  48. int i=0;
  49. while(cinfo.next_scanline < cinfo.image_height)
  50. {
  51. //printf("cinfo.next_scanline=%d ",cinfo.next_scanline);
  52. int j=0;
  53. for(i=0;i<img->width;i++)
  54. {
  55. //memcpy(pBuf+j,img->data+cinfo.next_scanline*img->bytes_per_line+i*4,3);
  56. *(pBuf+j) = *(img->data+cinfo.next_scanline*img->bytes_per_line+i*4+2);
  57. *(pBuf+j+1) = *(img->data+cinfo.next_scanline*img->bytes_per_line+i*4+1);
  58. *(pBuf+j+2) = *(img->data+cinfo.next_scanline*img->bytes_per_line+i*4);
  59. j+=3;
  60. }
  61. jpeg_write_scanlines(&cinfo,row_pointer,1);
  62. }

  63. free(pBuf);
  64. jpeg_finish_compress(&cinfo);
  65. jpeg_destroy_compress(&cinfo);

  66. fclose(fp);
  67. return 1;
  68. }

  69. /*****************************************************************
  70. Capture a local screenshot of the desktop,
  71. saving to the file specified by filename.
  72. If type = 0, then write a bitmap file, else
  73. write a JPEG file.

  74. Return Value:
  75. 0 - fail
  76. 1 - success
  77. ******************************************************************/
  78. int CaptureDesktop(char* filename, int type)
  79. {
  80. Window desktop;
  81. Display* dsp;
  82. XImage* img;

  83. int screen_width;
  84. int screen_height;
  85.   struct timeval tv;
  86.   long long L1,L2,L3;
  87.   int tv1=0,tv2=0;
  88. dsp = XOpenDisplay(NULL);/* Connect to a local display */
  89. if(NULL==dsp)
  90. {
  91. //sprintf(err_str,"Error:Cannot connect to local display\n");
  92. printf("CaptureDesktop","Cannot connect to local display");
  93. return 0;
  94. }
  95. desktop = RootWindow(dsp,0);/* Refer to the root window */
  96. if(0==desktop)
  97. {
  98. //printf("cannot get root window\n");
  99. printf("CaptureDesktop","cannot get root window");
  100. return 0;
  101. }

  102. /* Retrive the width and the height of the screen */
  103. screen_width = DisplayWidth(dsp,0);
  104. screen_height = DisplayHeight(dsp,0);
  105. printf("%d %d\n",screen_width,screen_height);
  106. /* Get the Image of the root window */
  107.  gettimeofday(&tv,NULL);
  108.                 L1=tv.tv_sec*1000*1000+tv.tv_usec;

  109. img = XGetImage(dsp,desktop,0,0,screen_width,screen_height,~0,ZPixmap);
  110. if(type==0){
  111. printf("type=%d\n",type);
  112. }
  113. //BmpWriteFileFromImage(filename,img);
  114. else
  115. JpegWriteFileFromImage(filename,img);

  116. gettimeofday(&tv,NULL);
  117.                 L2=tv.tv_sec*1000*1000+tv.tv_usec;
  118.                 tv1=(L2-L1);
  119.                 printf("cap time=%d\n",tv1);

  120. XDestroyImage(img);
  121. XCloseDisplay(dsp);
  122. return 1;
  123. }

  124. main()
  125. {
  126. //CaptureDesktop("./screenshot.bmp",0);
  127. CaptureDesktop("./screenshot.jpg",1);
  128. printf("Done.\n");
  129. }
鼠标事件:

点击(此处)折叠或打开

  1. /* ref: http://www.ishiboo.com/~danny/Projects/xwarppointer/ */
  2. #include <stdio.h>
  3. #include <string.h>
  4. //头文件
  5. #include <unistd.h>
  6. #include <X11/X.h>
  7. #include <X11/Xlib.h>
  8. #include <X11/Xutil.h>
  9. //全局变量
  10. Display *display;
  11. Window root;
  12. //初始化
  13. void init()
  14. {
  15.     if ((display = XOpenDisplay(NULL)) == NULL) {
  16.     fprintf(stderr, "Cannot open local X-display.\n");
  17.     return;
  18.     }
  19.     root = DefaultRootWindow(display);
  20. }
  21. void GetCursorPos(int *x,int *y)
  22. {
  23. int tmp;unsigned int tmp2;
  24. Window fromroot, tmpwin;
  25. XQueryPointer(display, root, &fromroot, &tmpwin, x, y, &tmp, &tmp, &tmp2);
  26. }
  27. //设置坐标
  28. void SetCursorPos(int x,int y)
  29. {
  30. int tmp;
  31. XWarpPointer(display, None, root, 0, 0, 0, 0, x, y);
  32. XFlush(display);
  33. }
  34. //模拟点击
  35. /* http://www.linuxquestions.org/questions/programming-9/simulating-a-mouse-click-594576/ */
  36. void mouseClick(int button)
  37. {
  38. Display *display = XOpenDisplay(NULL);

  39. XEvent event;

  40. if(display == NULL)
  41. {
  42. printf("Errore nell'apertura del Display !!!\n");
  43. return;
  44. }

  45. memset(&event, 0x00, sizeof(event));

  46. event.type = ButtonPress;
  47. event.xbutton.button = button;
  48. event.xbutton.same_screen = True;

  49. XQueryPointer(display, RootWindow(display, DefaultScreen(display)), &event.xbutton.root, &event.xbutton.window, &event.xbutton.x_root, &event.xbutton.y_root, &event.xbutton.x, &event.xbutton.y, &event.xbutton.state);

  50. event.xbutton.subwindow = event.xbutton.window;

  51. while(event.xbutton.subwindow)
  52. {
  53. event.xbutton.window = event.xbutton.subwindow;

  54. XQueryPointer(display, event.xbutton.window, &event.xbutton.root, &event.xbutton.subwindow, &event.xbutton.x_root, &event.xbutton.y_root, &event.xbutton.x, &event.xbutton.y, &event.xbutton.state);
  55. }

  56. if(XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0) printf("Errore nell'invio dell'evento !!!\n");

  57. XFlush(display);


  58. sleep(1);

  59. event.type = ButtonRelease;
  60. event.xbutton.state = 0x100;

  61. if(XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0) printf("Errore nell'invio dell'evento !!!\n");

  62. XFlush(display);

  63. XCloseDisplay(display);
  64. }
  65. int main()
  66. {
  67. //init();
  68. //int x,y;
  69. //GetCursorPos(&x,&y);
  70. //printf("%d %d\n",x,y);
  71. //SetCursorPos(500,522);
  72. //XCloseDisplay(display);
  73. //mouseClick(Button1);
  74. //sleep(5);
  75. mouseClick(Button3);


  76. return 0;
  77. }

阅读(3930) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~