Go语言实现AndroidFramebuffer屏幕截图功能

package main

/***********
作者:红猎人
***********/

import (
    "os"
    "syscall"
    "fmt"
    "unsafe"
    /*
    "image/png"
    "bytes"
    */
)

var (
    FBIOGET_VSCREENINFO = 0x4600
)

type fbBitfield struct {
    offset uint32 /* beginning of bitfield */
    length uint32 /* length of bitfield */
    msb_right uint32 /* != 0 : Most significant bit is */
}

type fbVarInfo struct {
    xres uint32 /* visible resolution */
    yres uint32
    xres_virtual uint32 /* virtual resolution */
    yres_virtual uint32
    xoffset uint32 /* offset from virtual to visible */
    yoffset uint32 /* resolution */

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

    nonstd uint32 /* != 0 Non standard pixel format */

    activate uint32 /* see FB_ACTIVATE_* */

    height uint32 /* height of picture in mm */
    width uint32 /* width of picture in mm */

    accel_flags uint32 /* (OBSOLETE) see fb_info.flags */

    /* Timing: All values in pixclocks, except pixclock (of course) */
    pixclock uint32 /* pixel clock in ps (pico seconds) */
    left_margin uint32 /* time from sync to picture */
    right_margin uint32 /* time from picture to sync */
    upper_margin uint32 /* time from sync to picture */
    lower_margin uint32
    hsync_len uint32 /* length of horizontal sync */
    vsync_len uint32 /* length of vertical sync */
    sync uint32 /* see FB_SYNC_* */
    vmode uint32 /* see FB_VMODE_* */
    rotate uint32 /* angle we rotate counter clockwise */
    colorspace uint32 /* colorspace for FOURCC-based modes */
    reserved [4]uint32 /* Reserved for future compatibility */
}

func main() {
    fb, err := os.Open("/dev/graphics/fb0")
    if err != nil {
        panic("can't open file /dev/fb0")
    }
    defer fb.Close()
    fmt.Printf("file open %v \n", fb.Fd())

    var varInfo fbVarInfo
    _,_,errno := syscall.RawSyscall(syscall.SYS_IOCTL, fb.Fd(), uintptr(FBIOGET_VSCREENINFO), uintptr(unsafe.Pointer(&varInfo)))
    if errno != 0 {
        fmt.Println(errno)
        panic("can't ioctl ... ")
    }
    fmt.Printf("width = %d height = %d\n", varInfo.xres, varInfo.yres)
    fmt.Printf("xoffset = %d yoffset = %d\n", varInfo.xoffset, varInfo.yoffset)
    fmt.Printf("depth = %d\n", varInfo.bits_per_pixel/8)

    // func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error)
    bpp := varInfo.bits_per_pixel / 8
    size := varInfo.xres * varInfo.yres * bpp
    offset := varInfo.xoffset * bpp + varInfo.xres * bpp * varInfo.yoffset
    mapsize := size + offset
    fmt.Printf("mapsize = %d\n", mapsize)
    data, err := syscall.Mmap(int(fb.Fd()), 0, int(mapsize) , syscall.PROT_READ, syscall.MAP_PRIVATE)
    if err != nil {
        panic("map failed ...")
    }
    defer syscall.Munmap(data)


    /*
    // save as png image.
    //
    // func Encode(w io.Writer, m image.Image) error
    // m := image.NewRGBA(image.Rect(0, 0, 640, 480))
    screen := data[offset :]
    m, err := png.Decode(bytes.NewBuffer(screen))
    if err != nil {
        panic("decode fail...")
    }
    output, err := os.OpenFile("/sdcard/screenshot.png", os.O_WRONLY | os.O_CREATE, 0666)
    if err != nil {
        panic("can't open file /sdcard/screenshot.png")
    }
    defer output.Close()
    err = png.Encode(output, m)
    if err != nil {
        panic("encoding fail...")
    }
    */
}

转载于:https://my.oschina.net/zengsai/blog/140092

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值