go 读取BMP文件头二进制读取方式
作者:清明-心若淡定 发布时间:2024-02-16 03:29:24
标签: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}
补充:golang(Go语言) byte/[]byte 与 二进制形式字符串 互转
效果
把某个字节或字节数组转换成字符串01的形式,一个字节用8个”0”或”1”字符表示。
比如:
byte(3) –> “00000011”
[]byte{1,2,3} –> “[00000001 00000010 00000011]”
“[00000011 10000000]” –> []byte{0x3, 0x80}
开源库 biu
实际上我已经将其封装到一个开源库了(biu),其中的一个功能就能达到上述效果:
//byte/[]byte -> string
bs := []byte{1, 2, 3}
s := biu.BytesToBinaryString(bs)
fmt.Println(s) //[00000001 00000010 00000011]
fmt.Println(biu.ByteToBinaryString(byte(3))) //00000011
//string -> []byte
s := "[00000011 10000000]"
bs := biu.BinaryStringToBytes(s)
fmt.Printf("%#v\n", bs) //[]byte{0x3, 0x80}
代码实现
const (
zero = byte('0')
one = byte('1')
lsb = byte('[') // left square brackets
rsb = byte(']') // right square brackets
space = byte(' ')
)
var uint8arr [8]uint8
// ErrBadStringFormat represents a error of input string's format is illegal .
var ErrBadStringFormat = errors.New("bad string format")
// ErrEmptyString represents a error of empty input string.
var ErrEmptyString = errors.New("empty string")
func init() {
uint8arr[0] = 128
uint8arr[1] = 64
uint8arr[2] = 32
uint8arr[3] = 16
uint8arr[4] = 8
uint8arr[5] = 4
uint8arr[6] = 2
uint8arr[7] = 1
}
// append bytes of string in binary format.
func appendBinaryString(bs []byte, b byte) []byte {
var a byte
for i := 0; i < 8; i++ {
a = b
b <<= 1
b >>= 1
switch a {
case b:
bs = append(bs, zero)
default:
bs = append(bs, one)
}
b <<= 1
}
return bs
}
// ByteToBinaryString get the string in binary format of a byte or uint8.
func ByteToBinaryString(b byte) string {
buf := make([]byte, 0, 8)
buf = appendBinaryString(buf, b)
return string(buf)
}
// BytesToBinaryString get the string in binary format of a []byte or []int8.
func BytesToBinaryString(bs []byte) string {
l := len(bs)
bl := l*8 + l + 1
buf := make([]byte, 0, bl)
buf = append(buf, lsb)
for _, b := range bs {
buf = appendBinaryString(buf, b)
buf = append(buf, space)
}
buf[bl-1] = rsb
return string(buf)
}
// regex for delete useless string which is going to be in binary format.
var rbDel = regexp.MustCompile(`[^01]`)
// BinaryStringToBytes get the binary bytes according to the
// input string which is in binary format.
func BinaryStringToBytes(s string) (bs []byte) {
if len(s) == 0 {
panic(ErrEmptyString)
}
s = rbDel.ReplaceAllString(s, "")
l := len(s)
if l == 0 {
panic(ErrBadStringFormat)
}
mo := l % 8
l /= 8
if mo != 0 {
l++
}
bs = make([]byte, 0, l)
mo = 8 - mo
var n uint8
for i, b := range []byte(s) {
m := (i + mo) % 8
switch b {
case one:
n += uint8arr[m]
}
if m == 7 {
bs = append(bs, n)
n = 0
}
}
return
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://www.cnblogs.com/saryli/p/11064837.html


猜你喜欢
- 一: 安装命令jupyter: pip install jupyter如果缺少依赖,缺啥装啥二: 运行 jupyter notebook首先
- 前言django是python语言的一个web框架,功能强大。配合一些插件可为web网站很方便地添加搜索功能。搜索引擎使用whoosh,是一
- 很多文章都有提到关于使用phpExcel实现Excel数据的导入导出,大部分文章都差不多,或者就是转载的,都会出现一些问题,下面是本人研究p
- 本文实例讲述了Python设置默认编码为utf8的方法。分享给大家供大家参考,具体如下:这是Python的编码问题,设置python的默认编
- 程序设计中我们时常需要检测用户输入是否正确,特别是姓名,地址等等是不是输入的汉字。那么,如何判断一个字符是不是汉字呢?其实在asp中至少有两
- 本文介绍的是Golang使用 os/exec 来执行 Linux 命令,分享出来供大家参考学习,下面来看看详细的介绍:下面是一个简单的示例:
- 一、前言前两篇博客讲解了爬虫解析网页数据的两种常用方法,re正则表达解析和beautifulsoup标签解析,所以今天的博客将围绕另外一种数
- 一、handlers是什么?logging模块中包含的类用来自定义日志对象的规则(比如:设置日志输出格式、等级等)常用3个子类:Stream
- 前言工作中经常会使用到将宽表变成窄表,例如这样的形式编号编码单位1单位2单位3单位4.................. &nbs
- 以前装过sql server,后来删掉。现在重装,却出现“以前的某个程序安装已在安装计算机上创建挂起的文件操作。运行安装程序之前必须重新启动
- 本文实例讲述了symfony2.4的twig中date用法。分享给大家供大家参考,具体如下:获得当前时间:{{ "now"
- 首先我们需要导入random模块 1. random.random(): 返回随机生成的一个浮点数,范围在[0,1)之间impor
- 从那起,我已经对这些方法做了大量的研究,并且已经在很多场合使用他们。在很多任务中,他们被证明是非常有用的(特别关于结构的抽象 DOM 选择器
- 正文 curl,用于发送请求的命令行工具,一个 HTTP 请求客户端(实际上它也可以做 FTP/SCP/TELNET 协议的事情,
- 在这篇文章中,我们将分析一个网络爬虫。网络爬虫是一个扫描网络内容并记录其有用信息的工具。它能打开一大堆网页,分析每个页面的内容以便寻找所有感
- Plotly Express是对 Plotly.py 的高级封装,内置了大量实用、现代的绘图模板,用户只需调用简单的API函数,即可快速生成
- 本文介绍了一些JavaScript常用到得表单验证函数,方便大家使用。 判断是否为整数,是则返回true,否则返回falsefun
- 为了保障数据的安全,需要定期对数据进行备份。备份的方式有很多种,效果也不一样。一旦数据库中的数据出现了错误,就需要使用备份好的数据进行还原恢
- Git 服务器搭建上一章节中我们远程仓库使用了 Github,Github 公开的项目是免费的,但是如果你不想让其他人看到你的项目就需要收费
- 其中用到urllib2模块和正则表达式模块。下面直接上代码:[/code]#!/usr/bin/env python#-*- coding: