Java读取BMP格式图片(源代码,转)

Basic Objective
   A windows BMP file is a common image format that Java does not handle. While BMP images are used only on windows machines, they are reasonably common. Reading these shows how to read complex structures in Java and
how to alter they byte order from the big endian order used by Java to the little endian order used by the windows and the intel processor.
--------------------------------------------------------
//
//This code was taken and cleaned up from a
//Javaworld tips and tricks column
//

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.MemoryImageSource;
import java.io.FileInputStream;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;

//
//really just a collection of methods to read a BMP file
//

public class BMPLoader

{

    // build an int from a byte array - convert little to big endian
    public static int constructInt( byte[] in, int offset) {

        int ret = (( int) in[offset + 3] & 0xff);

        ret = (ret << 8) | (( int) in[offset + 2] & 0xff);

        ret = (ret << 8) | (( int) in[offset + 1] & 0xff);

        ret = (ret << 8) | (( int) in[offset + 0] & 0xff);

        return (ret);

    }

    // build an int from a byte array - convert little to big endian
    // set high order bytes to 0xfff
    public static int constructInt3( byte[] in, int offset) {

        int ret = 0xff;

        ret = (ret << 8) | (( int) in[offset + 2] & 0xff);

        ret = (ret << 8) | (( int) in[offset + 1] & 0xff);

        ret = (ret << 8) | (( int) in[offset + 0] & 0xff);

        return (ret);

    }

    // build an int from a byte array - convert little to big endian
    public static long constructLong( byte[] in, int offset) {

        long ret = (( long) in[offset + 7] & 0xff);

        ret |= (ret << 8) | (( long) in[offset + 6] & 0xff);

        ret |= (ret << 8) | (( long) in[offset + 5] & 0xff);

        ret |= (ret << 8) | (( long) in[offset + 4] & 0xff);

        ret |= (ret << 8) | (( long) in[offset + 3] & 0xff);

        ret |= (ret << 8) | (( long) in[offset + 2] & 0xff);

        ret |= (ret << 8) | (( long) in[offset + 1] & 0xff);

        ret |= (ret << 8) | (( long) in[offset + 0] & 0xff);

        return (ret);

    }

    // build an double from a byte array - convert little to big endian
    public static double constructDouble( byte[] in, int offset) {

        long ret = constructLong(in, offset);

        return</
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值