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. P2052 [NOI2011]道路修建——树形结构(水题,大佬勿进)

    P2052 [NOI2011]道路修建 这个题其实在dfs里面就可以把事干完的,(我一开始还拿出来求了一把)…… 一条边的贡献就是儿子的大小和n-siz[v]乘上边权: #include<cma ...

  2. 从0开始部署GPU集群-1:k8s部署生态

    1 k8s:nvidia deepops 2  批处理:华为volcano 3 工作流:argo

  3. cpu指令重排序的原理

    目录: 1.重排序场景 2.追根溯源 3.缓存一致性协议 4.重排序原因 一.重排序场景 class ResortDemo { int a = 0; boolean flag = false; pub ...

  4. 堆排序Heapsort的Java和C代码

    Heapsort排序思路 将整个数组看作一个二叉树heap, 下标0为堆顶层, 下标1, 2为次顶层, 然后每层就是"3,4,5,6", "7, 8, 9, 10, 11 ...

  5. libusb: android上集成libusb库

    1. 下载libusb库. 可以到libusb库的官网(https://libusb.info/)或者是其官方的github仓库(https://github.com/libusb/libusb/re ...

  6. Win 10 MSYS2 VS Code 配置 c++ 的编译环境

    博客参考 https://www.cnblogs.com/esllovesn/p/10012653.html 和 https://blog.csdn.net/bat67/article/details ...

  7. 不规则的Ifc构件顶点提取方法

    BIM模型中有很多不规则的构件,在IFC中这些不规则的构件一般用顶点的形式表示,顶点坐标提取路径:  IfcObject->IfcProductDefinitionShape->IfcSh ...

  8. mysql全文索引:fulltext

    fulltext全文索引 要使用全文索引,不仅需要把某个字段的索引类型设置为fulltext,还需要修改mysql配置文件: [mysqld] ft_wordlist_charset #表示词典的字符 ...

  9. matlab学习笔记8 基本绘图命令-三维绘图

    一起来学matlab-matlab学习笔记8 基本绘图命令_6 三维绘图 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考书籍 <matlab 程序设计与综合应用>张德丰等著 ...

  10. vue+element-ui+ajax实现一个表格的实例

    <!DOCTYPE html> <html> <head> <script src="js/jquery-3.2.1.js">< ...