简体   繁体   English

从ByteBuffer创建一个DataBufferUShort

[英]Create a DataBufferUShort from a ByteBuffer

(Oracle Java 1.7, Eclipse Kepler, Ubuntu 12.04 64 bit) (Oracle Java 1.7,Eclipse Kepler,Ubuntu 12.04 64位)

I need to create a DataBufferUShort from a ByteBuffer but all my attempts have failed at run time.. 我需要从ByteBuffer创建一个DataBufferUShort ,但是我的所有尝试都在运行时失败。

How is this done, por favor ? 如何做到这一点, 陈健波青睐

Here is one failure: ( java.lang.UnsupportedOperationException ) 这是一个失败:(java.lang.UnsupportedOperationException)

        ShortBuffer sBuf = imageData.asShortBuffer(); // ByteBuffer
    DataBufferUShort dataBufferUShort = new DataBufferUShort(sBuf.array(),
            nPixels);

Here's the stack trace. 这是堆栈跟踪。 Line 156 is the second line in the code snippet above. 第156行是上方代码段中的第二行。

 Exception in thread "main" java.lang.UnsupportedOperationException
at java.nio.ShortBuffer.array(ShortBuffer.java:959)
at psw.iu.CreateImage.createGrayscaleImage(CreateImage.java:156)
at psw.iu.TestCreateImage.<init>(TestCreateImage.java:48)
at psw.iu.TestCreateImage.main(TestCreateImage.java:16)

Here's the method that creates the ByteBuffer 这是创建ByteBuffer的方法

    public static ByteBuffer createScaledData(WorkingByteBuffer rawWBB,
        ByteBuffer scaledByteBuffer) {

    rawWBB.byteBuffer.rewind(); // byteBuffer is a ByteBuffer
    scaledByteBuffer.rewind();
    ShortBuffer inBuf = rawWBB.byteBuffer.asShortBuffer();
    ShortBuffer outBuf = scaledByteBuffer.asShortBuffer();
    inBuf.rewind();
    outBuf.rewind();
    int maxDN = MAX_USHORT;
    double scaleFactor = (double) maxDN / (double) range;

    while (inBuf.hasRemaining()) {
        int value = 0xffff & inBuf.get();
        int scaledValue = (int) ((double) (value - min) * scaleFactor + 0.5);
        if (scaledValue < 0)
            scaledValue = 0;
        if (scaledValue > maxDN)
            scaledValue = maxDN;
        outBuf.put((short) scaledValue);
    }

    return scaledByteBuffer;

}

Edit This gives no errors but it's not the solution I was looking for. 编辑这没有给出任何错误,但这不是我正在寻找的解决方案。

        imageData.rewind();
    ShortBuffer shortBuffer = imageData.asShortBuffer();
    shortBuffer.rewind();
    DataBufferUShort dataBufferUShort = new DataBufferUShort( nLines * nPixPerLine);
    int index = 0;
    while(shortBuffer.hasRemaining()){
        dataBufferUShort.setElem(index++, 0xffff & shortBuffer.get());
    }

The ShortBuffer you are creating is only a view of the ByteBuffer, and view buffers don't support array(). 您正在创建的ShortBuffer只是ByteBuffer的视图,而视图缓冲区不支持array()。 You need to create a 'real' ShortBuffer with ShortBuffer.allocate(), and copy the data into it via sbuff.get() from the ByteBuffer (flipping the latter first). 您需要使用ShortBuffer.allocate()创建一个“真实的” ShortBuffer,然后通过sbuff.get()从ByteBuffer中将数据复制到其中(首先将后者翻转)。

The ShortBuffer.array() method will only return an array if the original ShortBuffer was created with a short[] . 如果原始的ShortBuffer是使用short[]创建的,则ShortBuffer.array()方法将仅返回一个数组。 in your case, your ShortBuffer is actually built on top of a byte[] (held by the ByteBuffer), so this won't work. 在您的情况下,您的ShortBuffer实际上是在byte[] (由ByteBuffer持有)的顶部构建的,因此这将无法工作。

if you really need a short[], then you will need to copy the contents of the ShortBuffer to a short[]. 如果确实需要short [],则需要将ShortBuffer的内容复制到short []。 something like: 就像是:

short[] shorts = new short[sBuf.remaining()];
sBuff.get(shorts);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM