好,知道了。转换为 bmp 有点神奇,但猜测真的没有直接的方法。
所以我确实初始化了一个bytebuffer,分析了android.media.image的YUV成分,把它们转成Jpeg,然后改成Bitmap,旋转90°来匹配原图。
// get the arFragment
arFragment = (ArFragment) getSupportFragmentManager().findFragmentById(R.id.arFragment);
ArSceneView arSceneView = arFragment.getArSceneView();
// set up a Listener to trigger on every frame
arSceneView.getScene().addOnUpdateListener(frameTime ->
{
try
{
frame = arSceneView.getArFrame();
androidMediaImage = frame.acquireCameraImage();
int imageWidth = androidMediaImage.getWidth();
int imageHeight = androidMediaImage.getHeight();
// select the target Container to display the image in
ImageView secondView = (ImageView) findViewById(R.id.imageView3);
byte[] nv21;
// an Android.Media.Image is a YUV-Image which is made out of 3 planes
ByteBuffer yBuffer = androidMediaImage.getPlanes()[0].getBuffer();
ByteBuffer uBuffer = androidMediaImage.getPlanes()[1].getBuffer();
ByteBuffer vBuffer = androidMediaImage.getPlanes()[2].getBuffer();
// set up a Bytearray with the size of all the planes
int ySize = yBuffer.remaining();
int uSize = uBuffer.remaining();
int vSize = vBuffer.remaining();
nv21 = new byte[ySize + uSize + vSize];
// Fill in the array. This code is directly taken from https://www.programcreek.com
//where it was pointed out that U and V have to be swapped
yBuffer.get(nv21, 0 , ySize);
vBuffer.get(nv21, ySize, vSize);
vBuffer.get(nv21, ySize + vSize, uSize);
// combine the three layers to one nv21 image
YuvImage yuvImage = new YuvImage(nv21, ImageFormat.NV21, imageWidth, imageHeight, null);
// Open a Bytestream to feed the compressor
ByteArrayOutputStream out = new ByteArrayOutputStream();
// compress the yuv image to Jpeg. This is important, because the BitmapFactory can't read a
// yuv-coded image directly (belief me I tried -.-)
yuvImage.compressToJpeg(new Rect(0, 0, imageWidth, imageHeight), 50, out);
// now write down the bytes of the image into an array
byte[] imageBytes = out.toByteArray();
// and build the bitmap using the Factory
Bitmap bitmapImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
// use a Matrix for the rotation
Matrix rotationMatrix = new Matrix();
// the thing is basically a bunch of numbers which then can be used to compute the new location of each pixel
rotationMatrix.postRotate(90);
// the rotatedImage will be our target image
Bitmap rotatedImage = Bitmap.createBitmap(bitmapImage, 0,0, bitmapImage.getWidth(), bitmapImage.getHeight(), rotationMatrix, true);
// it's so easy!!!!
secondView.setImageBitmap(rotatedImage);
} catch (NotYetAviableException e)
{
e.printStackTrace();
}
});
如果我完全错了,您显然可以纠正我,并且有一种更简单的解决方案。但它至少有效,所以我很高兴 <3