将Java的位图的字节数组位图、数组、字节、Java

由网友(深绊久碍)分享简介:Bitmap bmp = intent.getExtras().get("data");int size = bmp.getRowBytes() * bmp.getHeight();ByteBuffer b = ByteBuffer.allocate(size);bmp.copyPixelsToBuff...
  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 ...位图从相机返回的是不可变的...但是这不应该无所谓,因为它做一个副本。

When I look at the buffer after the call to copyPixelsToBuffer the bytes are all 0... The bitmap returned from the camera is immutable... but that shouldn't matter since it's doing a copy.

可能是什么不对的code?

What could be wrong with this code?

推荐答案

尝试是这样的:

Bitmap bmp = intent.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
阅读全文

相关推荐

最新文章