示例#1
0
  /**
   * Reads an image from an archived file and return it as ByteBuffer object.
   *
   * @author Mike Butler, Kiet Le
   */
  private ByteBuffer readImage(String filename, Dimension dim) {
    if (dim == null) dim = new Dimension(0, 0);
    ByteBuffer bytes = null;
    try {
      DataInputStream dis =
          new DataInputStream(getClass().getClassLoader().getResourceAsStream(filename));
      dim.width = dis.readInt();
      dim.height = dis.readInt();
      System.out.println("Creating buffer, width: " + dim.width + " height: " + dim.height);
      // byte[] buf = new byte[3 * dim.height * dim.width];
      bytes = BufferUtil.newByteBuffer(3 * dim.width * dim.height);
      for (int i = 0; i < bytes.capacity(); i++) {
        bytes.put(dis.readByte());
      }
      dis.close();

    } catch (Exception e) {
      e.printStackTrace();
    }
    bytes.rewind();
    return bytes;
  }