Tag: bytebuffer

将Java位图转换为字节数组

Bitmap bmp = intent.getExtras().get(“data”); int size = bmp.getRowBytes() * bmp.getHeight(); ByteBuffer b = ByteBuffer.allocate(size); bmp.copyPixelsToBuffer(b); byte[] bytes = new byte[size]; try { b.get(bytes, 0, bytes.length); } catch (BufferUnderflowException e) { // always happens } // do something with byte[] 在调用copyPixelsToBuffer之后,当我查看缓冲区时,字节全部为0 …从相机返回的位图是不可变的,但这不应该因为它正在进行复制。 这段代码有什么问题?

如何将Short / Int写入1字节缓冲区

我有这些功能: fun asByteArray(value: Short): ByteArray { val buffer: ByteBuffer = ByteBuffer.allocate(2) buffer.order(ByteOrder.BIG_ENDIAN) buffer.putShort(value) buffer.flip() return buffer.array() } fun asByteArray(value: Int): ByteArray { val buffer: ByteBuffer = ByteBuffer.allocate(4) buffer.order(ByteOrder.BIG_ENDIAN) buffer.putInt(value) buffer.flip() return buffer.array() } 如果值是255,那么我想写入1个字节的缓冲区。 我该怎么做? 如果我做ByteBuffer.allocate(1)并尝试写入short / int值,则发生BufferOverflowException。