go 读取BMP文件头二进制读取方式

作者:清明-心若淡定 时间:2024-02-16 03:29:24 

BMP文件头定义:

WORD 两个字节 16bit

DWORD 四个字节 32bit

go 读取BMP文件头二进制读取方式


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

标签:go,BMP,文件头,二进制
0
投稿

猜你喜欢

  • MySQL创建带特殊字符的数据库名称方法示例

    2024-01-26 15:31:30
  • django admin 添加自定义链接方式

    2022-09-22 05:33:14
  • PHP session有效期session.gc_maxlifetime

    2023-11-14 17:01:45
  • 详细讲解Access数据库远程连接的实用方法

    2008-11-28 16:34:00
  • 基于Python获取照片的GPS位置信息

    2021-02-25 03:32:00
  • Go简单实现协程池的实现示例

    2024-02-19 07:35:16
  • Pytorch创建张量的四种方法

    2023-11-20 15:25:36
  • vue keep-alive的简单总结

    2024-04-28 10:55:32
  • JS定时器实例

    2024-04-29 13:25:21
  • PHP设计模式中观察者模式详解

    2023-05-27 04:43:31
  • 深入浅出解析mssql在高频,高并发访问时键查找死锁问题

    2024-01-29 02:32:54
  • javascript中的offsetWidth、clientWidth、innerWidth及相关属性方法

    2024-05-10 14:07:17
  • python中str内置函数用法总结

    2022-06-23 10:22:45
  • Python中获取网页状态码的两个方法

    2023-08-27 22:47:21
  • mysql 5.7.18 zip版安装配置方法图文教程(win7)

    2024-01-26 17:58:18
  • Python输出汉字字库及将文字转换为图片的方法

    2023-01-13 07:06:39
  • vue中将el-switch值true、false改为number类型的1和0

    2024-04-27 15:57:43
  • ASP实例:幻灯片新闻代码

    2008-11-21 17:40:00
  • Python实现基于C/S架构的聊天室功能详解

    2022-06-14 11:43:38
  • 表单系列·出错字段排行榜

    2008-07-01 12:57:00
  • asp之家 网络编程 m.aspxhome.com