您的位置:首页 >> 编程开发 >> Java >> 正文
RSS
 

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

http://www.rdxx.com 04年07月21日 12:12 Blog 我要投稿

关键词: 源代码 , 图片 , 读取 , 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
共9页  1 2 3 4 5 6 7 8 9

 
 
标签: 源代码 , 图片 , 读取 , Java , 格式 , BMP , 代码 打印本文
 
 
  热点搜索
 
 
 


Copyright ©2005 - 2008 Rdxx.Com,All Rights Reserved
收藏本页
收藏本站