zoukankan      html  css  js  c++  java
  • 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}

  • 相关阅读:
    IDEA 的默认tomcat日志位置以及默认编译后的classes位置
    在linux环境下tomcat 指定 jdk或jre版本
    在Linux环境如何在不解压情况下搜索多个zip包中匹配的字符串内容
    说一下最近找的工作所遇到的一个巨坑,跟各位同行分享一下。(与技术无关)
    Jmeter 深入性能测试进阶-01
    英语
    python 01
    fiddler,ADB, Monkey
    http 协议,SSL证书,http头信息,tcp/http区别,支付功能测试
    扎马步-计算机网络和系统基础知识
  • 原文地址:https://www.cnblogs.com/saryli/p/11064837.html
Copyright © 2011-2022 走看看