| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2500 人关注过本帖
标题:谁能给我一段用Java 加载和显示BMP(位图)的代码?
只看楼主 加入收藏
changyawei
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2005-11-3
收藏
 问题点数:0 回复次数:24 
谁能给我一段用Java 加载和显示BMP(位图)的代码?
本人最近想用java做一些关于图像处理的程序,不知道怎样在Java中加载位图.希望大家帮个忙....
搜索更多相关主题的帖子: 位图 BMP 加载 Java 代码 
2007-03-21 08:21
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
JAVA不认识BMP

可惜不是你,陪我到最后
2007-03-21 09:54
风月_无边
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:60
帖 子:2960
专家分:652
注 册:2007-1-19
收藏
得分:0 
有其他的办法没有!!

我的网站 http://www.
2007-03-21 10:03
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
当然有,可以把BMP转成JAVA认识的格式

可惜不是你,陪我到最后
2007-03-21 10:04
angeloc
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:36
帖 子:1353
专家分:0
注 册:2006-11-21
收藏
得分:0 

你可以用JIMI
http://java.sun.com/products/jimi/

当然你要想自己写代码也可以,比较麻烦。我在网上给你COPY了一个:
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;

public class BMPLoader

{
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);

}
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);

}
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);

}
public static double constructDouble(byte[] in, int offset) {

long ret = constructLong(in, offset);

return (Double.longBitsToDouble(ret));

}
public static short constructShort(byte[] in, int offset) {

short ret = (short) ((short) in[offset + 1] & 0xff);

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

return (ret);

}
static class BitmapHeader {

public int nsize;

public int nbisize;

public int nwidth;

public int nheight;

public int nplanes;

public int nbitcount;

public int ncompression;

public int nsizeimage;

public int nxpm;

public int nypm;

public int nclrused;

public int nclrimp;

// read in the bitmap header
public void read(FileInputStream fs) throws IOException

{

final int bflen = 14; // 14 byte BITMAPFILEHEADER

byte bf[] = new byte[bflen];

fs.read(bf, 0, bflen);

final int bilen = 40; // 40-byte BITMAPINFOHEADER

byte bi[] = new byte[bilen];

fs.read(bi, 0, bilen);
nsize = constructInt(bf, 2);

nbisize = constructInt(bi, 2);

nwidth = constructInt(bi, 4);

nheight = constructInt(bi, 8);

nplanes = constructShort(bi, 12); //(((int)bi[13]&0xff)<<8) |

nbitcount = constructShort(bi, 14); //(((int)bi[15]&0xff)<<8) |

ncompression = constructInt(bi, 16);

nsizeimage = constructInt(bi, 20);

nxpm = constructInt(bi, 24);

nypm = constructInt(bi, 28);

nclrused = constructInt(bi, 32);

nclrimp = constructInt(bi, 36);

}

}

public static Image read(FileInputStream fs)

{

try {

BitmapHeader bh = new BitmapHeader();

bh.read(fs);

if (bh.nbitcount == 24)

return (readMap24(fs, bh));

if (bh.nbitcount == 32)

return (readMap32(fs, bh));

if (bh.nbitcount == 8)

return (readMap8(fs, bh));

fs.close();

} catch (IOException e) {

// System.out.println("Caught exception in loadbitmap!");
}

return (null);

}
protected static Image readMap32(FileInputStream fs, BitmapHeader bh)
throws IOException

{

Image image;

int xwidth = bh.nsizeimage / bh.nheight;

int ndata[] = new int[bh.nheight * bh.nwidth];

byte brgb[] = new byte[bh.nwidth * 4 * bh.nheight];

fs.read(brgb, 0, bh.nwidth * 4 * bh.nheight);

int nindex = 0;

for (int j = 0; j < bh.nheight; j++)

{

for (int i = 0; i < bh.nwidth; i++)

{

ndata[bh.nwidth * (bh.nheight - j - 1) + i] = constructInt3(
brgb, nindex);

nindex += 4;

}

}

image = Toolkit.getDefaultToolkit().createImage

(new MemoryImageSource(bh.nwidth, bh.nheight,

ndata, 0, bh.nwidth));

fs.close();

return (image);

}

protected static Image readMap24(FileInputStream fs, BitmapHeader bh)
throws IOException

{

Image image;

int npad = (bh.nsizeimage / bh.nheight) - bh.nwidth * 3;

int ndata[] = new int[bh.nheight * bh.nwidth];

byte brgb[] = new byte[(bh.nwidth + npad) * 3 * bh.nheight];

fs.read(brgb, 0, (bh.nwidth + npad) * 3 * bh.nheight);

int nindex = 0;

for (int j = 0; j < bh.nheight; j++)

{

for (int i = 0; i < bh.nwidth; i++)

{

ndata[bh.nwidth * (bh.nheight - j - 1) + i] = constructInt3(
brgb, nindex);

nindex += 3;

}

nindex += npad;

}

image = Toolkit.getDefaultToolkit().createImage

(new MemoryImageSource(bh.nwidth, bh.nheight,

ndata, 0, bh.nwidth));

fs.close();

return (image);

}

protected static Image readMap8(FileInputStream fs, BitmapHeader bh)
throws IOException

{

Image image;

int nNumColors = 0;

if (bh.nclrused > 0)

{

nNumColors = bh.nclrused;

}

else

{

nNumColors = (1 & 0xff) << bh.nbitcount;

}

if (bh.nsizeimage == 0)

{

bh.nsizeimage = ((((bh.nwidth * bh.nbitcount) + 31) & ~31) >> 3);

bh.nsizeimage *= bh.nheight;

// System.out.println("nsizeimage (backup) is"+nsizeimage);

}

// Read the palatte colors.

int npalette[] = new int[nNumColors];

byte bpalette[] = new byte[nNumColors * 4];

fs.read(bpalette, 0, nNumColors * 4);

int nindex8 = 0;

for (int n = 0; n < nNumColors; n++)

{

npalette[n] = constructInt3(bpalette, nindex8);

nindex8 += 4;

}

int npad8 = (bh.nsizeimage / bh.nheight) - bh.nwidth;

// System.out.println("nPad is:"+npad8);

int ndata8[] = new int[bh.nwidth * bh.nheight];

byte bdata[] = new byte[(bh.nwidth + npad8) * bh.nheight];

fs.read(bdata, 0, (bh.nwidth + npad8) * bh.nheight);

nindex8 = 0;

for (int j8 = 0; j8 < bh.nheight; j8++)

{

for (int i8 = 0; i8 < bh.nwidth; i8++)

{

ndata8[bh.nwidth * (bh.nheight - j8 - 1) + i8] =

npalette[((int) bdata[nindex8] & 0xff)];

nindex8++;

}

nindex8 += npad8;

}

image = Toolkit.getDefaultToolkit().createImage

(new MemoryImageSource(bh.nwidth, bh.nheight,

ndata8, 0, bh.nwidth));

return (image);

}

public static Image load(String sdir, String sfile) {

return (load(sdir + sfile));

}
public static Image load(String sdir)

{

try

{

FileInputStream fs = new FileInputStream(sdir);

return (read(fs));

}

catch (IOException ex) {

return (null);

}

}

public static void main(String[] args) throws IOException

{

if (args.length == 0) {

System.out.println("Usage >java BMPLoader ImageFile.bmp");

System.exit(0);

}

FileInputStream in = new FileInputStream(args[0]);

Image TheImage = read(in);

JFrame TheFrame = new JFrame(args[0]);

JLabel TheLabel = new JLabel(new ImageIcon(TheImage));

TheFrame.getContentPane().add(new JScrollPane(TheLabel));

TheFrame.setSize(300, 300);

TheFrame.setVisible(true);

}
}


老牛明知夕阳晚,不用扬鞭自奋蹄; Angelo\'s BLOG
2007-03-21 10:26
purana
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:广东-广州
等 级:版主
威 望:66
帖 子:6039
专家分:0
注 册:2005-6-17
收藏
得分:0 
如此的复杂.还不如先把它转成jpg或png

我的msn: myfend@
2007-03-21 10:29
风月_无边
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:60
帖 子:2960
专家分:652
注 册:2007-1-19
收藏
得分:0 
确实够复杂.

我的网站 http://www.
2007-03-21 10:34
angeloc
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:36
帖 子:1353
专家分:0
注 册:2006-11-21
收藏
得分:0 

呵呵,对于C来说是很普通了,JAVA是简单,但最好还是看看比较地层的代码吧,不能光玩表面对象那。


老牛明知夕阳晚,不用扬鞭自奋蹄; Angelo\'s BLOG
2007-03-21 10:37
神vLinux飘飘
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:浙江杭州
等 级:贵宾
威 望:91
帖 子:6140
专家分:217
注 册:2004-7-17
收藏
得分:0 
C都能很轻松的显示BMP,JAVA肯定也没问题的,关键就看你了解不了解BMP的格式了

淘宝杜琨
2007-03-21 12:00
changyawei
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2005-11-3
收藏
得分:0 
在Sun公司网站上,能不能下载支持BMP格式的扩展类库?
2007-03-21 15:40
快速回复:谁能给我一段用Java 加载和显示BMP(位图)的代码?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.070685 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved