BMP文件头定义:

WORD 两个字节 16bit

DWORD 四个字节 32bit

package main

import (
"encoding/binary"
"fmt"
"os"
) func main() {
file, err := os.Open("tim.bmp")
if err != nil {
fmt.Println(err)
return
} defer file.Close() //type拆成两个byte来读
var headA, headB byte
//Read第二个参数字节序一般windows/linux大部分都是LittleEndian,苹果系统用BigEndian
binary.Read(file, binary.LittleEndian, &headA)
binary.Read(file, binary.LittleEndian, &headB) //文件大小
var size uint32
binary.Read(file, binary.LittleEndian, &size) //预留字节
var reservedA, reservedB uint16
binary.Read(file, binary.LittleEndian, &reservedA)
binary.Read(file, binary.LittleEndian, &reservedB) //偏移字节
var offbits uint32
binary.Read(file, binary.LittleEndian, &offbits) fmt.Println(headA, headB, size, reservedA, reservedB, offbits) }

  执行结果

66 77 196662 0 0 54

使用结构体方式

package main

import (
"encoding/binary"
"fmt"
"os"
) type BitmapInfoHeader struct {
Size uint32
Width int32
Height int32
Places uint16
BitCount uint16
Compression uint32
SizeImage uint32
XperlsPerMeter int32
YperlsPerMeter int32
ClsrUsed uint32
ClrImportant uint32
} func main() {
file, err := os.Open("tim.bmp")
if err != nil {
fmt.Println(err)
return
} defer file.Close() //type拆成两个byte来读
var headA, headB byte
//Read第二个参数字节序一般windows/linux大部分都是LittleEndian,苹果系统用BigEndian
binary.Read(file, binary.LittleEndian, &headA)
binary.Read(file, binary.LittleEndian, &headB) //文件大小
var size uint32
binary.Read(file, binary.LittleEndian, &size) //预留字节
var reservedA, reservedB uint16
binary.Read(file, binary.LittleEndian, &reservedA)
binary.Read(file, binary.LittleEndian, &reservedB) //偏移字节
var offbits uint32
binary.Read(file, binary.LittleEndian, &offbits) fmt.Println(headA, headB, size, reservedA, reservedB, offbits) infoHeader := new(BitmapInfoHeader)
binary.Read(file, binary.LittleEndian, infoHeader)
fmt.Println(infoHeader) }

  执行结果:

66 77 196662 0 0 54

&{40 256 256 1 24 0 196608 3100 3100 0 0}

go 读取BMP文件头二进制读取的更多相关文章

  1. 在linux下读取bmp文件头的完整代码。

    呵呵,贴在这里记录一下. [cpp] view plaincopy #include<stdio.h> #include<string.h> #include<sys/t ...

  2. DCMTK读取DICOM文件头信息的三种方法

    Howto: Load File Meta-Header Here's an example that shows how to load the File Meta Information Head ...

  3. WinCE的C#中使用StreamReader 来读取TXT文档,读取文本文档。

    using System.IO; private void button1_Click(object sender, EventArgs e) { string strFilePath = " ...

  4. 读取bmp图片数据

    public void getBMPImage(String source) throws Exception { clearNData(); //清除数据保存区 FileInputStream fs ...

  5. bmp格式图片文件读取

    C++读取bmp图片 #include <windows.h> #include <stdio.h> #include <stdlib.h> #include &l ...

  6. Linux下BMP文件不能正常读取问题的解决办法

    今天将之前在win下编好的读取BMP图像功能移植到UNIX下. 碰到的第一个问题是,Linux下的BMP文件头的结构体需要自己重新定义一遍. 第二个问题是,需要搞清楚Linux是32位的还是64位的. ...

  7. bmp图片格式及读取

    C++读取bmp图片的例子 #include <windows.h> #include <stdio.h> #include <stdlib.h> #include ...

  8. 我与python3擦肩而过(二)—— csv文件头哪去啦?

    在看Python Data Visualization Cookbook 这本书(基于python2),开始时读取csv文件头的时候出现问题.查了资料,又是python3的问题,从这个链接找到答案. ...

  9. (三)pdf的构成之文件头综述

    引自:https://blog.csdn.net/steve_cui/article/details/81981943 一般情况下,文件头,即,PDF文件的第一行,它用来定义PDF的版本,从而确定该P ...

随机推荐

  1. Python各种扩展名(py, pyc, pyw, pyo, pyd)区别

    扩展名 在写Python程序时我们常见的扩展名是py, pyc,其实还有其他几种扩展名.下面是几种扩展名的用法. py py就是最基本的源码扩展名 pyw pyw是另一种源码扩展名,跟py唯一的区别是 ...

  2. [Vue warn]: You are using the runtime-only build of Vue where the template compiler is not available

    原文链接https://blog.csdn.net/xiaomajia029/article/details/88320233 问题描述: 原因分析:在项目配置的时候,默认 npm 包导出的是运行时构 ...

  3. SDN-based Network Management Solution

    SDN-based Network Management Solution 摘要: 在此项目中,我们开发了一种网络管理应用程序,以监视和控制由支持OpenFlow的交换机和支持SNMP的设备组成的企业 ...

  4. 如何使用Salt Pillar

    作者言 Salt的网站上有两篇关于Pillar的文档(一,二),其中一篇内容很少,我觉得写成一篇文章更合适.本文的逻辑结构没有参照官方文档,而是根据我自己对Pillar的理解组织内容,希望能够把这个概 ...

  5. linux学习(3):linux常用命令大全

    Linux常用命令大全(非常全!!!) 最近都在和Linux打交道,感觉还不错.我觉得Linux相比windows比较麻烦的就是很多东西都要用命令来控制,当然,这也是很多人喜欢linux的原因,比较短 ...

  6. c#怎么解决System.UnauthorizedAccessException异常

    https://blog.csdn.net/qq_38061677/article/details/81157116 代码: using System;namespace Project2048{ c ...

  7. (转)SQLAlchemy入门和进阶

    URL:https://zhuanlan.zhihu.com/p/27400862 https://www.cnblogs.com/mrchige/p/6389588.html---SQLAlchem ...

  8. C++实现委托机制(三)——lambda表达式封装

    C++.引言:              其实原本没打算写这一章的,不过最后想了想,嗯还是把lambda表达式也一并封装进去,让这个委托也适应lambda表达式的注册.不过在之前还是需要先了解lamb ...

  9. CefSharp 提示 flash player is out of date 运行此插件 等问题解决办法

    CefSharp 提示 flash player is out of date 或者 需要手动右键点 运行此插件 脚本 等问题解决办法 因为中国版FlashPlayer变得Ad模式之后,只好用旧版本的 ...

  10. Nginx记录-Proxy_pass多个应用配置(转载)

    1. 在http节点下,加入upstream节点. upstream linuxidc {       server 10.0.6.108:7080;       server 10.0.0.85:8 ...