歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux教程

使用imlib2函數庫實現截屏功能

本文演示使用imlib2函數庫實現截屏功能
程序運行後,截屏圖像保存為jpeg格式文件到當前目錄下,起名為“screen.jpeg”

在Debian 5.02系統下,安裝libimlib2-dev軟件包
imlib2的API文檔在 /usr/share/doc/libimlib2-dev/html/index.html

用cvs獲取筆者本機screen項目的源代碼如下:
$ cvs co -p screen
cvs checkout: Updating screen
===================================================================
Checking out screen/Makefile
RCS:  /home/c-aries/cvs/screen/Makefile,v
VERS: 1.2
***************
CC = gcc
CFLAGS = -Wall -Werror -Wcast-align -g
LDFLAGS =

SCREENOBJECT = screen.o

GIBLIBCFLAGS = `giblib-config --cflags`
GIBLIBLIBS = `giblib-config --libs`

all: screen

screen: $(SCREENOBJECT)
    $(CC) $(LDFLAGS) $(GIBLIBLIBS) $(SCREENOBJECT) -o $@

screen.o: screen.c screen.h
    $(CC) $(CFLAGS) $(GIBLIBCFLAGS) -c $<

clean:
    rm -f screen *~ *.o a.out

clean-media:
    rm -f *~ *.jpeg

.PHONY: all clean
===================================================================
Checking out screen/screen.c
RCS:  /home/c-aries/cvs/screen/screen.c,v
VERS: 1.2
***************
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <giblib/giblib.h>
#include "screen.h"

void
screen_init(struct screen *screen)
{
  screen->display = XOpenDisplay(NULL);
  if (!screen->display) {
    perror("XOpenDisplay");
    exit(EXIT_FAILURE);
  }
  screen->screen = ScreenOfDisplay(screen->display,
                  DefaultScreen(screen->display));
  screen->visual = DefaultVisual(screen->display,
                DefaultScreen(screen->display));
  screen->colormap = DefaultColormap(screen->display,
                DefaultScreen(screen->display));
  screen->root = RootWindow(screen->display,
                DefaultScreen(screen->display));
  imlib_context_set_display(screen->display);
  imlib_context_set_visual(screen->visual);
  imlib_context_set_colormap(screen->colormap);
}

int
main(int argc, char *argv[])
{
  struct screen screen;
  Imlib_Image image;

  screen_init(&screen);
  image = gib_imlib_create_image_from_drawable(screen.root,
                           0,
                           0,
                           0,
                           screen.screen->width,
                           screen.screen->height,
                           1);
  imlib_context_set_image(image);
  imlib_image_set_format("jpeg");
  imlib_save_image("screen.jpeg");
  imlib_free_image();
  exit(EXIT_SUCCESS);
}
===================================================================
Checking out screen/screen.h
RCS:  /home/c-aries/cvs/screen/screen.h,v
VERS: 1.1.1.1
***************
#ifndef SCREEN_H
#define SCREEN_H

#include <X11/Xlib.h>

struct screen {
  Display *display;
  Screen *screen;
  Visual *visual;
  Colormap colormap;
  Window root;
};

void screen_init(struct screen *screen);

#endif
$
 

該程序參考了scrot項目的源代碼和imlib2的編程文檔

上周的文章 video4linux2編程實例 使用video4linux2編程接口獲得攝像頭數據流,再將YUYV數據流轉換成RGB數據流。
然後想添加截取圖片功能,將攝像頭的RGB數據流保存成JPEG圖片。
找來平時一直在使用的自由軟件scrot,閱讀其源代碼,即找到將RGB保存成JPEG圖片的方法:
“將用於SDL的像素格式RGBA轉換成用於imlib2的像素格式ARGB,再用imlib2的imlib_create_image_using_data()函數保存成JPEG圖片”
這部分的代碼下周再介紹吧:)

實現截取圖片功能的期間,學會了截屏功能……
於是順便把實現截屏的代碼貼出來,作為編程實例。

Copyright © Linux教程網 All Rights Reserved