如何从ARCore [Android Studio]中的session.update()获取位图

问题描述 投票:2回答:2

我试图从我的ARSession的当前帧获得一个位图与ARCore。但它总是等于null。我已经在网上搜索了一段时间,但无法弄清楚我做错了什么。

try {
    capturedImage = mFrame.acquireCameraImage();

    ByteBuffer buffer = capturedImage.getPlanes()[0].getBuffer();

    byte[] bytes = new byte[buffer.capacity()];

    Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length,null);

    if (bitmap == null) 
        Log.e(TAG,"Bitmap was NOT initialized!");

} catch(Exception e){

}

我从我的qazxsw poi的qazxsw poi获得qazxsw poi,我用它来显示相机图像。除了我的Bitmap等于null之外,一切正常。

我正在使用Button,因此只使用一个Frame,如下所示:

mFrame

onDrawFrameGLSurfaceViewscanButton = (Button) findViewById(R.id.scanButton); scanButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { checkbox = false; if (capturedImage!=null) capturedImage.close(); BitmapMethod(); } }); 都不等于null。

capturedImage可能有问题吗?

非常感谢

java android bitmap bitmapfactory arcore
2个回答
2
投票

mFrame.acquireCameraImage()可能有问题吗?

不,buffer按预期工作。

但它总是等于null

由于位图工厂不理解传递给它的图像数据,因此位图将始终等于null。

bytes方法用类型为mFrame.acquireCameraImage()的对象作为YUV格式或YCbCr进行响应。这些类型的图像有3个平面,非常好地解释mFrame.acquireCameraImage()。包含在这些平面中的mFrame.acquireCameraImage()可以由Image代码中的CPU / GPU直接读取。 here无法读取此类数据。因此,您需要将此YUV图像转换为其他内容。

为此,您需要使用ByteArray类创建YUV的实例,然后使用native方法将其转换为JPEG。一旦你拥有了这个byteArray,你就可以完成上面你正在做的事情。使用BitmapFactory将其转换为Bitmap并将其添加到YuvImage

注意:YUV有3架飞机。从所有平面创建单个bytearray,然后将其传递给YUV构造函数。虽然不详细,但它看起来应该与此类似:

compressToJpeg

那只是一个粗略的代码。它可能有改进的余地。另请参阅BitmapFactory


-1
投票

我不知道是否还有人在寻找答案,但这是我的代码。

ImageView
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.