码迷,mamicode.com
首页 > 其他好文 > 详细

利用framebuffer,命令行显示图片

时间:2015-12-05 12:50:19      阅读:344      评论:0      收藏:0      [点我收藏+]

标签:

上代码

import fcntl
import struct
import mmap
import contextlib
import os
import time
import numpy as np
import cv2


src = cv2.imread(./1.bmp, cv2.IMREAD_COLOR)
img = src#[0:100, 0:400]
print img.shape



with open(/dev/fb0, r+b) as fbfd:    
    fb_fix_screeninfo = struct.pack(16sLIIIIIIIILIIHHH,
                                    0000000000000000,
                                    0L, 0,0,0,0,0,0,0,0, 
                                    0L, 0, 0, 0, 0, 0)
    ‘‘‘
    struct fb_fix_screeninfo {
            char id[16];            /* identification string eg "TT Builtin" */
            unsigned long smem_start;    /* Start of frame buffer mem */
                                            /* (physical address) */
            __u32 smem_len;            /* Length of frame buffer mem */
            __u32 type;            /* see FB_TYPE_*        */
            __u32 type_aux;            /* Interleave for interleaved Planes */
            __u32 visual;            /* see FB_VISUAL_*        */ 
            __u16 xpanstep;            /* zero if no hardware panning  */
            __u16 ypanstep;            /* zero if no hardware panning  */
            __u16 ywrapstep;        /* zero if no hardware ywrap    */
            __u32 line_length;        /* length of a line in bytes    */
            unsigned long mmio_start;    /* Start of Memory Mapped I/O   */
                                            /* (physical address) */
            __u32 mmio_len;            /* Length of Memory Mapped I/O  */
            __u32 accel;            /* Indicate to driver which    */
                                            /*  specific chip/card we have    */
            __u16 capabilities;        /* see FB_CAP_*            */
            __u16 reserved[2];        /* Reserved for future compatibility */
    };
    ‘‘‘
    fb_fix_screeninfo = fcntl.ioctl(fbfd, 0x4602, fb_fix_screeninfo)
    fb_fix_screeninfo = struct.unpack(16sLIIIIIIIILIIHHH, fb_fix_screeninfo)
    lineLength = fb_fix_screeninfo[8]

    fb_var_screeninfo = struct.pack(IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII,
                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
    ‘‘‘
    struct fb_bitfield {
            __u32 offset;            /* beginning of bitfield    */
            __u32 length;            /* length of bitfield        */
            __u32 msb_right;        /* != 0 : Most significant bit is */ 
                                            /* right */ 
    };
    struct fb_var_screeninfo {
            __u32 xres;            /* visible resolution        */
            __u32 yres;
            __u32 xres_virtual;        /* virtual resolution        */
            __u32 yres_virtual;
            __u32 xoffset;            /* offset from virtual to visible */
            __u32 yoffset;            /* resolution            */

            __u32 bits_per_pixel;        /* guess what            */
            __u32 grayscale;        /* 0 = color, 1 = grayscale,    */
                                            /* >1 = FOURCC            */
            struct fb_bitfield red;        /* bitfield in fb mem if true color, */
            struct fb_bitfield green;    /* else only length is significant */
            struct fb_bitfield blue;
            struct fb_bitfield transp;    /* transparency            */    

            __u32 nonstd;            /* != 0 Non standard pixel format */

            __u32 activate;            /* see FB_ACTIVATE_*        */

            __u32 height;            /* height of picture in mm    */
            __u32 width;            /* width of picture in mm     */

            __u32 accel_flags;        /* (OBSOLETE) see fb_info.flags */

            /* Timing: All values in pixclocks, except pixclock (of course) */
            __u32 pixclock;            /* pixel clock in ps (pico seconds) */
            __u32 left_margin;        /* time from sync to picture    */
            __u32 right_margin;        /* time from picture to sync    */
            __u32 upper_margin;        /* time from sync to picture    */
            __u32 lower_margin;
            __u32 hsync_len;        /* length of horizontal sync    */
            __u32 vsync_len;        /* length of vertical sync    */
            __u32 sync;            /* see FB_SYNC_*        */
            __u32 vmode;            /* see FB_VMODE_*        */
            __u32 rotate;            /* angle we rotate counter clockwise */
            __u32 colorspace;        /* colorspace for FOURCC-based modes */
            __u32 reserved[4];        /* Reserved for future compatibility */
    };‘‘‘
    fb_var_screeninfo = fcntl.ioctl(fbfd, 0x4600, fb_var_screeninfo)
    fb_var_screeninfo = struct.unpack(IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII, fb_var_screeninfo)
    
    screen_x = fb_var_screeninfo[0]
    screen_y = fb_var_screeninfo[1]
    bitPerPix = fb_var_screeninfo[6]
    print("%d*%d, %dbpp" %(fb_var_screeninfo[0], fb_var_screeninfo[1], fb_var_screeninfo[6]))
    
    screensize = fb_var_screeninfo[0] * fb_var_screeninfo[1] * fb_var_screeninfo[6] / 8
    print screensize= + str(screensize)
    
    with contextlib.closing(mmap.mmap(fbfd.fileno(), screensize, flags=mmap.MAP_SHARED,
                                       access=mmap.ACCESS_WRITE)) as fbp:

##        print fbp.size()
        loc = 0
        linex = 0
        for y in range(img.shape[0]):
            for x in range(img.shape[1]):
                
                loc = (y*lineLength) + (x*bitPerPix/8)
                fbp[loc] = chr(img[y][x][0])
                fbp[loc+1] = chr(img[y][x][1])
                fbp[loc+2] = chr(img[y][x][2])
                fbp[loc+3] = chr(255)
##
##        r = fbp.flush()

##        time.sleep(3)

 

利用framebuffer,命令行显示图片

标签:

原文地址:http://www.cnblogs.com/lulu147/p/5021349.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!