我们可能想用于
游戏开发的资源文件,例如图片,转换成二进制的文件并且可以在程序中使用。很多专业的
游戏开发都采用了这样的处理。本文将介绍一种功能不算很强大,但是简单可用的工具:BINCompiler。通过具体的实例讲解如何BINCompiler转换
游戏资源文件,并在MIDP应用中使用。
首先从本站下载中心下载BINCompiler,解压后双击运行。BINCompiler的界面非常简单:

BINCompiler不支持子目录,因此你需要把你所有要转换的资源文件放在一个目录下。例如d:/temp/files,我们从WTK的DEMO中任意选择了两个PNG的图片,比如LightHouse-0.png ,jc_frame_46.png(他们位于相册应用程序的目录中)。选择Compilation Folder的Brows按钮,定位到d:/temp/files也就是我们放置文件的目录,然后选择输出的目录,比如F:/,然后点Create按钮。这时候BINCompiler就把这两个文件创建为一个.bin文件了。在输出文件的目录中还有一个重要的文件index.txt,我们需要根据这个文件中的信息从.bin文件中读取图片文件。
FName Index Pos Size
LightHouse-0.png 0 0 3756
jc_frame_46.png 1 3760 4075
我们使用如下两个java方法来读取图像文件:
//读取指定文件并返回字节数组
public byte[] readFile(String binfile, int pos)
{
byte buffer[];
int len;
try {
InputStream is = this.getClass().getResourceAsStream("/" + binfile);
is.skip(pos);
len = (is.read() & 0xFF) << 24;
len |= (is.read() & 0xFF) << 16;
len |= (is.read() & 0xFF) << 8;
len |= (is.read() & 0xFF);
buffer = new byte[len];
is.read(buffer, 0, buffer.length);
is.close();
is = null;
System.gc();
} catch (Exception e) {
buffer = null;
e.printStackTrace();
System.gc();
return null;
}
return buffer;
}
//读取指定文件并返回图片
public Image readImage(String binfile, int pos)
{
byte buffer[];
int len;
try {
InputStream is = this.getClass().getResourceAsStream("/" + binfile);
is.skip(pos);
len = (is.read() & 0xFF) << 24;
len |= (is.read() & 0xFF) << 16;
len |= (is.read() & 0xFF) << 8;
len |= (is.read() & 0xFF);
buffer = new byte[len];
is.read(buffer, 0, buffer.length);
is.close();
is = null;
System.gc();
} catch (Exception e) {
buffer = null;
e.printStackTrace();
System.gc();
return null;
}
return Image.createImage(buffer, 0, buffer.length);
}
使用这两个方法非常简单,我们只需要把输出文件的名字和要读取文件的pos值(从index读取)传递给方法就可以了,例如我们要读取图片LightHouse-0.png ,那么示例代码如下:
display = Display.getDisplay(this);
Form form = new Form("Image");
Image image = this.readImage("map.bin", 0);
form.append(image);
display.setCurrent(form);
读者可以使用eclipse或者netbeans编写一个简单的MIDlet测试一下,这里给出一个简单的MIDlet源码供参考:
/*
* MainMidlet.java
*
* Created on 2005年8月3日, 下午10:45
*/
package com.j2medev.test;
import java.io.InputStream;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
*
* @author Administrator
* @version
*/
public class MainMidlet extends MIDlet {
private Display display = null;
public void startApp() {
display = Display.getDisplay(this);
Form form = new Form("Image");
Image image = this.readImage("map.bin", 0);
form.append(image);
display.setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
/**************************************************************************************
* Reads a file from the BIN file and return data as a byte buffer
**************************************************************************************/
public byte[] readFile(String binfile, int pos)
{
byte buffer[];
int len;
try {
InputStream is = this.getClass().getResourceAsStream("/" + binfile);
is.skip(pos);
len = (is.read() & 0xFF) << 24;
len |= (is.read() & 0xFF) << 16;
len |= (is.read() & 0xFF) << 8;
len |= (is.read() & 0xFF);
buffer = new byte[len];
is.read(buffer, 0, buffer.length);
is.close();
is = null;
System.gc();
} catch (Exception e) {
buffer = null;
e.printStackTrace();
System.gc();
return null;
}
return buffer;
}
/**************************************************************************************
* Reads a file from the BIN file and return data as an Image
**************************************************************************************/
public Image readImage(String binfile, int pos)
{
byte buffer[];
int len;
try {
InputStream is = this.getClass().getResourceAsStream("/" + binfile);
is.skip(pos);
len = (is.read() & 0xFF) << 24;
len |= (is.read() & 0xFF) << 16;
len |= (is.read() & 0xFF) << 8;
len |= (is.read() & 0xFF);
buffer = new byte[len];
is.read(buffer, 0, buffer.length);
is.close();
is = null;
System.gc();
} catch (Exception