简体   繁体   English

如何在相机应用程序中将原始字节 [] 转换为位图

[英]How to convert raw byte[] into bitmap in camera App

This is the picture taking fuction这是拍照功能

class Camera {
...

void capturePicture() {
    Camera.Size size = mParams.getPictureSize();
    int bitsPerPixel = ImageFormat.getBitsPerPixel(mParams.getPictureFormat());
    int bufferSize = (int) Math.ceil(size.width * size.height * bitsPerPixel / 8d) ;
    Log.d(TAG, "Picture Size : " + size.width + "\t" + size.height);
    Log.d(TAG, "Picture format : " + mParams.getPictureFormat());
    Log.d(TAG, "Bits per Pixel = " + bitsPerPixel);
    Log.d(TAG, "Buffer Size = " + bufferSize);
    byte[] buffer = new byte[1382400];
    addBuffer(buffer);
    Camera.ShutterCallback shutterCallback = () -> mCameraCallbacks.onShutter();
    Camera.PictureCallback pictureCallback = (data, camera) -> {
        mCameraControllerCallbacks.onPicture(data);
        };
    mCamera.takePicture(shutterCallback, pictureCallback, null, null);
}

public interface CameraCallbacks {
    void onPicture(byte[] bytes);
}

The the picture size should be 3264 x 2448 however the bitsPerPixel returns -1 so I can't use it to calculate.图片大小应该是 3264 x 2448 但是 bitsPerPixel 返回 -1 所以我不能用它来计算。 It turn out the minimum buffer size is 1382400 I don't know why.结果证明最小缓冲区大小是 1382400 我不知道为什么。

Here is the Activity receives the callback这是 Activity 接收回调

public class CameraActivity extends AppCompatActivity implements Camera.CameraCallbacks

    @Override
public void onPicture(byte[] bytes) {

    final ByteBuffer buffer = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN);
    final int[] ints = new int[bytes.length / 4];
    buffer.asIntBuffer().put(ints);
    Log.d(TAG,"Creating Bitmap of Size : "+mCameraView.mPictureSize.width +" x "+mCameraView.mPictureSize.height);
    Bitmap bitmap = Bitmap.createBitmap(ints, mCameraView.mPictureSize.width, mCameraView.mPictureSize.height, Bitmap.Config.ARGB_8888);
    Intent intent = new Intent(CameraActivity.this, PicturePreviewActivity.class);
    intent.putExtra("bitmap", bmp);
    startActivityForResult(intent, SAVE_PICTURE_OR_NOT);

}

The code here is obviously wrong and I am having trouble rearrange these byte[] into ints[] the way bitmap accepts because I don't know the data structure inside these bytes.这里的代码显然是错误的,我无法按照位图接受的方式将这些 byte[] 重新排列为 ints[],因为我不知道这些字节中的数据结构。
Also BitmapFactory.decodeByteArray won't work because it can't read raw data. BitmapFactory.decodeByteArray 也不起作用,因为它无法读取原始数据。

Can anybody help me on this one?有人可以帮我解决这个问题吗?

It is not possible to retrieve uncompressed bitmap data from android camera, so the picture call back need to move from RawCallback to JpegCallback and use decodeByteArray() to obtain Bitmap.Also it is unreliable to pass Bitmap through Intent So the simplest way is to write to the receiving Activity directly.The Code became like this: android相机无法获取未压缩的位图数据,所以图片回调需要从RawCallback移动到JpegCallback,使用decodeByteArray()获取Bitmap。另外通过Intent传递Bitmap也是不可靠的,所以最简单的写法直接到接收Activity。代码变成这样:

    @Override
public void onPicture(byte[] bytes) {
        Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, mJpeg.get().length,null);
        PicturePreviewActivity.mBitmap=new WeakReference<>(bitmap);
        Intent intent = new Intent(CameraActivity.this, PicturePreviewActivity.class);
        startActivityForResult(intent, SAVE_PICTURE_OR_NOT);
    }
}

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

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