go 读取BMP文件头二进制读取
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文件头二进制读取的更多相关文章
- 在linux下读取bmp文件头的完整代码。
呵呵,贴在这里记录一下. [cpp] view plaincopy #include<stdio.h> #include<string.h> #include<sys/t ...
- DCMTK读取DICOM文件头信息的三种方法
Howto: Load File Meta-Header Here's an example that shows how to load the File Meta Information Head ...
- WinCE的C#中使用StreamReader 来读取TXT文档,读取文本文档。
using System.IO; private void button1_Click(object sender, EventArgs e) { string strFilePath = " ...
- 读取bmp图片数据
public void getBMPImage(String source) throws Exception { clearNData(); //清除数据保存区 FileInputStream fs ...
- bmp格式图片文件读取
C++读取bmp图片 #include <windows.h> #include <stdio.h> #include <stdlib.h> #include &l ...
- Linux下BMP文件不能正常读取问题的解决办法
今天将之前在win下编好的读取BMP图像功能移植到UNIX下. 碰到的第一个问题是,Linux下的BMP文件头的结构体需要自己重新定义一遍. 第二个问题是,需要搞清楚Linux是32位的还是64位的. ...
- bmp图片格式及读取
C++读取bmp图片的例子 #include <windows.h> #include <stdio.h> #include <stdlib.h> #include ...
- 我与python3擦肩而过(二)—— csv文件头哪去啦?
在看Python Data Visualization Cookbook 这本书(基于python2),开始时读取csv文件头的时候出现问题.查了资料,又是python3的问题,从这个链接找到答案. ...
- (三)pdf的构成之文件头综述
引自:https://blog.csdn.net/steve_cui/article/details/81981943 一般情况下,文件头,即,PDF文件的第一行,它用来定义PDF的版本,从而确定该P ...
随机推荐
- docker_基础用法
1. docker architecture 2. 命令
- Redis4.0新特性(一)-Memory Command
Redis4.0版本增加了很多诱人的新特性,在redis精细化运营管理中都非常有用(猜想和antirez加入redislabs有很大关系):此系列几篇水文主要介绍以下几个新特性的使用和效果. Redi ...
- Spring Boot-intellij idea导入方式搭建SpringBoot
Spring Boot概念 从最根本上来讲,Spring Boot就是一些库的集合,它能够被任意项目的构建系统所使用.简便起见,该框架也提供了命令行界面,它可以用来运行和测试Boot应用.框架的发布版 ...
- Redis慢日志查询
Redis slowlog 是个什么 redis的slow log记录了那些执行时间超过规定时长的请求.执行时间不包括I/O操作(比如与客户端进行网络通信等),只是命令的实际执行时间(期间线程会被阻塞 ...
- vue---数据列表循环
使用vue进行数据循环是非常常见的操作,下面是用利用forEach和map来进行数据循环: 最常见的 forEach 循环: tbody.forEach((item,key) => { .... ...
- Android 编译 product 分区
https://source.android.google.cn/devices/bootloader/product-partitions 编译 product 分区 Android 9 支持使用 ...
- (1)PyCharm开发工具安装Flask并创建helloworld程序
一.环境描述 1.操作系统:windows7 2.编程语言:python3.6 下载地址:https://www.python.org/downloads/windows/ 3.虚拟化环境:virtu ...
- 自定义Shell分隔符
在shell中使用for循环语句时,参数列表有时候需要将空格纳入参数当中,这时就不好使用空格作为分隔符.如下例中,我实际想要输出的是a1.a2.b1.b2以及hello world,但却输出了如下内容 ...
- unittest中的TestLoader使用
一:unittest中的TestLoader使用说明 第一步:unittest增加TestSuit() suite=unittest.TestSuite() 第二步:unittest增加Testloa ...
- mybatis自动生成后无法获取主键id问题
使用mybatis的自动生成工具生成的主键id: <selectKey resultType="java.lang.Integer" keyProperty="id ...