Как я могу сделать скриншот ArFragment? - PullRequest
0 голосов
/ 23 июня 2019

После нескольких часов поиска я наконец-то смог сохранить скриншот ArFragment. но проблема в том, что он сохраняет только текущее изображение с камеры, за исключением размещенного 3D-объекта.

как я могу получить полный скриншот (текущее изображение камеры + 3D-объект, который находится)?

коды, которые я использовал, здесь ниже.

ImageButton btn3 = (ImageButton)findViewById(R.id.camera_btn);
btn3.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        onSceneUpdate((FrameTime) frameTime);

        Toast.makeText(AR_Activity.this, "스크린샷이 저장되었습니다.", Toast.LENGTH_SHORT).show();
    }
});

private void onSceneUpdate(FrameTime frameTime) {
    try {
        Date now = new Date();
        android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
        String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
        Frame currentFrame = arFragment.getArSceneView().getArFrame();
        Image currentImage = currentFrame.acquireCameraImage();
        int imageFormat = currentImage.getFormat();
        if (imageFormat == ImageFormat.YUV_420_888) {
            Log.d("ImageFormat", "Image format is YUV_420_888");
        }

        WriteImageInformation((Image) currentImage, (String) mPath);
    } catch (Exception e) {

    }
}

private static byte[] NV21toJPEG(byte[] nv21, int width, int height) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    YuvImage yuv = new YuvImage(nv21, ImageFormat.NV21, width, height, null);
    yuv.compressToJpeg(new Rect(0, 0, width, height), 100, out);
    return out.toByteArray();
}

public static void WriteImageInformation(Image image, String path) {

    byte[] data = null;
    data = NV21toJPEG(YUV_420_888toNV21(image),
            image.getWidth(), image.getHeight());
    BufferedOutputStream bos = null;
    try {
        bos = new BufferedOutputStream(new FileOutputStream(path));
        bos.write(data);
        bos.flush();
        bos.close();

    } catch (Throwable e) {
        e.printStackTrace();
    }
}

private static byte[] YUV_420_888toNV21(Image image) {
    byte[] nv21;
    ByteBuffer yBuffer = image.getPlanes()[0].getBuffer();
    ByteBuffer uBuffer = image.getPlanes()[1].getBuffer();
    ByteBuffer vBuffer = image.getPlanes()[2].getBuffer();

    int ySize = yBuffer.remaining();
    int uSize = uBuffer.remaining();
    int vSize = vBuffer.remaining();

    nv21 = new byte[ySize + uSize + vSize];

    //U and V are swapped
    yBuffer.get(nv21, 0, ySize);
    vBuffer.get(nv21, ySize, vSize);
    uBuffer.get(nv21, ySize + vSize, uSize);

    return nv21;
}
...