マルチカメラ API

注: このページでは、Camera2 パッケージについて説明します。アプリが Camera2 の特定の低レベルの機能を必要とする場合を除き、CameraX を使用することをおすすめします。CameraX と Camera2 は、どちらも Android 5.0(API レベル 21)以降に対応しています。

マルチカメラは Android 9(API レベル 28)で導入されました。リリース以来、この API をサポートするデバイスが市場に出ています。マルチカメラのユースケースの多くは、特定のハードウェア構成と密接に関連しています。つまり、すべてのユースケースがすべてのデバイスに対応しているわけではないため、マルチカメラ機能は Play Feature Delivery に適しています。

一般的なユースケースは次のとおりです。

  • ズーム: 切り抜き範囲や希望の焦点距離に応じてカメラを切り替えます。
  • Depth: 複数のカメラを使用して奥行きマップを作成します。
  • ボケ表現: 推定される奥行き情報を使用して、デジタル一眼レフカメラのような狭い焦点範囲をシミュレートします。

論理カメラと物理カメラの違い

マルチカメラ API を理解するには、論理カメラと物理カメラの違いを理解する必要があります。参考として、3 つの背面カメラを備えたデバイスについて考えてみましょう。この例では、3 つの背面カメラそれぞれが物理カメラと見なされます。論理カメラは、複数の物理カメラをグループ化したものです。論理カメラの出力は、基盤となる物理カメラの 1 つからのストリーム、または複数の基盤となる物理カメラから同時に発生した融合ストリームです。どちらの場合も、ストリームはカメラの Hardware Abstraction Layer(HAL)によって処理されます。

多くのスマートフォン メーカーが自社のカメラアプリを開発しており、通常は自社のデバイスにプリインストールされています。ハードウェアのすべての機能を使用するために、非公開 API または非公開 API を使用するか、他のアプリがアクセスできないドライバ実装から特別な扱いを受けることがあります。デバイスによっては、異なる物理カメラからのフレームの融合ストリームを提供することで、論理カメラのコンセプトを実装していますが、これは特定の特権アプリに限定されます。多くの場合、物理カメラのうち 1 つのみがフレームワークに公開されています。Android 9 より前のサードパーティ デベロッパーの状況を次の図に示します。

図 1. 通常、特権アプリのみが使用できるカメラ機能

Android 9 以降、Android アプリで非公開 API を使うことはできなくなりました。Android のベスト プラクティスでは、フレームワークにマルチカメラ サポートを組み込むことで、スマートフォン メーカーに対し、同じ方向を向いたすべての物理カメラに対して論理カメラを公開することを強く推奨しています。サードパーティ デベロッパーが Android 9 以降を実行するデバイスでは、次の表示を想定しています。

図 2. Android 9 以降、すべてのカメラデバイスに対する完全なデベロッパー アクセス権

論理カメラが提供する機能は、カメラ HAL の OEM 実装に完全に依存します。たとえば、Pixel 3 のようなデバイスでは、要求された焦点距離と切り抜き領域に基づいて物理カメラのいずれかを選択するように論理カメラが実装されています。

Multi-camera API

新しい API では、次の新しい定数、クラス、メソッドが追加されています。

Android 互換性定義ドキュメント(CDD)の変更により、マルチカメラ API についてもデベロッパーから一定の期待が寄せられています。Android 9 より前にデュアルカメラを搭載したデバイスは存在していましたが、複数のカメラを同時に開くには試行錯誤が必要でした。Android 9 以降では、マルチカメラにより、同じ論理カメラに含まれる物理カメラのペアを開くことができるタイミングを指定する一連のルールが提供されます。

ほとんどの場合、Android 9 以降を搭載したデバイスは、すべての物理カメラ(赤外線などのあまり一般的でないセンサータイプを除く)と、使いやすい論理カメラをエクスポーズします。動作が保証されたストリームの組み合わせごとに、論理カメラに属する 1 つのストリームを、基盤となる物理カメラからの 2 つのストリームに置き換えることができます。

複数のストリーミングの同時進行

複数のカメラ ストリームの同時使用では、1 台のカメラで複数のストリームを同時に使用するためのルールについて説明しています。注意すべき点として、複数のカメラに同じルールが適用されます。CameraMetadata.REQUEST_AVAILABLE_CAPABILITIES_LOGICAL_MULTI_CAMERA は、論理 YUV_420_888 または RAW ストリームを 2 つの物理ストリームに置き換える方法について説明します。つまり、YUV または RAW タイプの各ストリームは、同じタイプとサイズの 2 つのストリームに置き換えることができます。シングルカメラ デバイスの場合、次に示す保証された構成のカメラ ストリームから始めることができます。

  • ストリーム 1: YUV タイプ、論理カメラ id = 0 から MAXIMUM サイズ

マルチカメラ サポートを備えたデバイスでは、論理 YUV ストリームを 2 つの物理ストリームに置き換えるセッションを作成できます。

  • ストリーム 1: YUV タイプ、MAXIMUM サイズ(物理カメラ id = 1
  • ストリーム 2: YUV タイプ、MAXIMUM サイズ(物理カメラ id = 2

YUV または RAW ストリームを 2 つの同等のストリームに置き換えることができるのは、2 つのカメラが CameraCharacteristics.getPhysicalCameraIds() にリストされている論理カメラグループの一部である場合のみです。

フレームワークによって提供される保証は、複数の物理カメラから同時にフレームを取得するために必要な最小限の保証にすぎません。ほとんどのデバイスで追加のストリームがサポートされており、複数の物理カメラデバイスを個別に開けることさえあります。フレームワークで確実に保証されるわけではないため、そのためには試行錯誤しながらデバイスごとにテストと調整を行う必要があります。

複数の物理カメラを使用したセッションの作成

マルチカメラ対応デバイスで物理カメラを使用する場合は、1 つの CameraDevice(論理カメラ)を開いて、1 回のセッションで操作します。API レベル 28 で追加された API CameraDevice.createCaptureSession(SessionConfiguration config) を使用して、単一のセッションを作成します。セッション構成にはいくつかの出力構成があります。各構成には、一連の出力ターゲットがあり、必要に応じて物理カメラ ID を設定できます。

図 3. SessionConfiguration モデルと OutputConfiguration モデル

キャプチャ リクエストには出力ターゲットが関連付けられています。フレームワークは、アタッチされている出力ターゲットに基づいて、リクエストを送信する物理カメラまたは論理カメラを決定します。出力ターゲットが、物理カメラ ID とともに出力構成として送信された出力ターゲットのいずれかに対応している場合、その物理カメラはリクエストを受信して処理します。

2 台の物理カメラを使用する

マルチカメラ用のカメラ API に加え、論理カメラを識別し、その背後にある物理カメラを検出することもできます。論理カメラ ストリームの 1 つを置き換えるために使用できる、物理カメラのペア候補を特定する関数を定義できます。

Kotlin

/**
     * Helper class used to encapsulate a logical camera and two underlying
     * physical cameras
     */
    data class DualCamera(val logicalId: String, val physicalId1: String, val physicalId2: String)

    fun findDualCameras(manager: CameraManager, facing: Int? = null): List {
        val dualCameras = MutableList()

        // Iterate over all the available camera characteristics
        manager.cameraIdList.map {
            Pair(manager.getCameraCharacteristics(it), it)
        }.filter {
            // Filter by cameras facing the requested direction
            facing == null || it.first.get(CameraCharacteristics.LENS_FACING) == facing
        }.filter {
            // Filter by logical cameras
            // CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES_LOGICAL_MULTI_CAMERA requires API >= 28
            it.first.get(CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES)!!.contains(
                CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES_LOGICAL_MULTI_CAMERA)
        }.forEach {
            // All possible pairs from the list of physical cameras are valid results
            // NOTE: There could be N physical cameras as part of a logical camera grouping
            // getPhysicalCameraIds() requires API >= 28
            val physicalCameras = it.first.physicalCameraIds.toTypedArray()
            for (idx1 in 0 until physicalCameras.size) {
                for (idx2 in (idx1 + 1) until physicalCameras.size) {
                    dualCameras.add(DualCamera(
                        it.second, physicalCameras[idx1], physicalCameras[idx2]))
                }
            }
        }

        return dualCameras
    }

Java

/**
     * Helper class used to encapsulate a logical camera and two underlying
     * physical cameras
     */
    final class DualCamera {
        final String logicalId;
        final String physicalId1;
        final String physicalId2;

        DualCamera(String logicalId, String physicalId1, String physicalId2) {
            this.logicalId = logicalId;
            this.physicalId1 = physicalId1;
            this.physicalId2 = physicalId2;
        }
    }
    List findDualCameras(CameraManager manager, Integer facing) {
        List dualCameras = new ArrayList<>();

        List cameraIdList;
        try {
            cameraIdList = Arrays.asList(manager.getCameraIdList());
        } catch (CameraAccessException e) {
            e.printStackTrace();
            cameraIdList = new ArrayList<>();
        }

        // Iterate over all the available camera characteristics
        cameraIdList.stream()
                .map(id -> {
                    try {
                        CameraCharacteristics characteristics = manager.getCameraCharacteristics(id);
                        return new Pair<>(characteristics, id);
                    } catch (CameraAccessException e) {
                        e.printStackTrace();
                        return null;
                    }
                })
                .filter(pair -> {
                    // Filter by cameras facing the requested direction
                    return (pair != null) &&
                            (facing == null || pair.first.get(CameraCharacteristics.LENS_FACING).equals(facing));
                })
                .filter(pair -> {
                    // Filter by logical cameras
                    // CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES_LOGICAL_MULTI_CAMERA requires API >= 28
                    IntPredicate logicalMultiCameraPred =
                            arg -> arg == CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES_LOGICAL_MULTI_CAMERA;
                    return Arrays.stream(pair.first.get(CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES))
                            .anyMatch(logicalMultiCameraPred);
                })
                .forEach(pair -> {
                    // All possible pairs from the list of physical cameras are valid results
                    // NOTE: There could be N physical cameras as part of a logical camera grouping
                    // getPhysicalCameraIds() requires API >= 28
                    String[] physicalCameras = pair.first.getPhysicalCameraIds().toArray(new String[0]);
                    for (int idx1 = 0; idx1 < physicalCameras.length; idx1++) {
                        for (int idx2 = idx1 + 1; idx2 < physicalCameras.length; idx2++) {
                            dualCameras.add(
                                    new DualCamera(pair.second, physicalCameras[idx1], physicalCameras[idx2]));
                        }
                    }
                });
return dualCameras;
}

物理カメラの状態処理は論理カメラによって制御されます。「デュアルカメラ」を開くには、物理カメラに対応する論理カメラを開きます。

Kotlin

fun openDualCamera(cameraManager: CameraManager,
                       dualCamera: DualCamera,
        // AsyncTask is deprecated beginning API 30
                       executor: Executor = AsyncTask.SERIAL_EXECUTOR,
                       callback: (CameraDevice) -> Unit) {

        // openCamera() requires API >= 28
        cameraManager.openCamera(
            dualCamera.logicalId, executor, object : CameraDevice.StateCallback() {
                override fun onOpened(device: CameraDevice) = callback(device)
                // Omitting for brevity...
                override fun onError(device: CameraDevice, error: Int) = onDisconnected(device)
                override fun onDisconnected(device: CameraDevice) = device.close()
            })
    }

Java

void openDualCamera(CameraManager cameraManager,
                        DualCamera dualCamera,
                        Executor executor,
                        CameraDeviceCallback cameraDeviceCallback
    ) {

        // openCamera() requires API >= 28
        cameraManager.openCamera(dualCamera.logicalId, executor, new CameraDevice.StateCallback() {
            @Override
            public void onOpened(@NonNull CameraDevice cameraDevice) {
               cameraDeviceCallback.callback(cameraDevice);
            }

            @Override
            public void onDisconnected(@NonNull CameraDevice cameraDevice) {
                cameraDevice.close();
            }

            @Override
            public void onError(@NonNull CameraDevice cameraDevice, int i) {
                onDisconnected(cameraDevice);
            }
        });
    }

開くカメラを選択する以外は、プロセスは以前の Android バージョンでカメラを開く場合と同じです。新しいセッション構成 API を使用してキャプチャ セッションを作成すると、特定のターゲットを特定の物理カメラ ID に関連付けるようにフレームワークに指示します。

Kotlin

/**
 * Helper type definition that encapsulates 3 sets of output targets:
 *
 *   1. Logical camera
 *   2. First physical camera
 *   3. Second physical camera
 */
typealias DualCameraOutputs =
        Triple?, MutableList?, MutableList?>

fun createDualCameraSession(cameraManager: CameraManager,
                            dualCamera: DualCamera,
                            targets: DualCameraOutputs,
                            // AsyncTask is deprecated beginning API 30
                            executor: Executor = AsyncTask.SERIAL_EXECUTOR,
                            callback: (CameraCaptureSession) -> Unit) {

    // Create 3 sets of output configurations: one for the logical camera, and
    // one for each of the physical cameras.
    val outputConfigsLogical = targets.first?.map { OutputConfiguration(it) }
    val outputConfigsPhysical1 = targets.second?.map {
        OutputConfiguration(it).apply { setPhysicalCameraId(dualCamera.physicalId1) } }
    val outputConfigsPhysical2 = targets.third?.map {
        OutputConfiguration(it).apply { setPhysicalCameraId(dualCamera.physicalId2) } }

    // Put all the output configurations into a single flat array
    val outputConfigsAll = arrayOf(
        outputConfigsLogical, outputConfigsPhysical1, outputConfigsPhysical2)
        .filterNotNull().flatMap { it }

    // Instantiate a session configuration that can be used to create a session
    val sessionConfiguration = SessionConfiguration(
        SessionConfiguration.SESSION_REGULAR,
        outputConfigsAll, executor, object : CameraCaptureSession.StateCallback() {
            override fun onConfigured(session: CameraCaptureSession) = callback(session)
            // Omitting for brevity...
            override fun onConfigureFailed(session: CameraCaptureSession) = session.device.close()
        })

    // Open the logical camera using the previously defined function
    openDualCamera(cameraManager, dualCamera, executor = executor) {

        // Finally create the session and return via callback
        it.createCaptureSession(sessionConfiguration)
    }
}

Java

/**
 * Helper class definition that encapsulates 3 sets of output targets:
 * 

* 1. Logical camera * 2. First physical camera * 3. Second physical camera */ final class DualCameraOutputs { private final List logicalCamera; private final List firstPhysicalCamera; private final List secondPhysicalCamera; public DualCameraOutputs(List logicalCamera, List firstPhysicalCamera, List third) { this.logicalCamera = logicalCamera; this.firstPhysicalCamera = firstPhysicalCamera; this.secondPhysicalCamera = third; } public List getLogicalCamera() { return logicalCamera; } public List getFirstPhysicalCamera() { return firstPhysicalCamera; } public List getSecondPhysicalCamera() { return secondPhysicalCamera; } } interface CameraCaptureSessionCallback { void callback(CameraCaptureSession cameraCaptureSession); } void createDualCameraSession(CameraManager cameraManager, DualCamera dualCamera, DualCameraOutputs targets, Executor executor, CameraCaptureSessionCallback cameraCaptureSessionCallback) { // Create 3 sets of output configurations: one for the logical camera, and // one for each of the physical cameras. List outputConfigsLogical = targets.getLogicalCamera().stream() .map(OutputConfiguration::new) .collect(Collectors.toList()); List outputConfigsPhysical1 = targets.getFirstPhysicalCamera().stream() .map(s -> { OutputConfiguration outputConfiguration = new OutputConfiguration(s); outputConfiguration.setPhysicalCameraId(dualCamera.physicalId1); return outputConfiguration; }) .collect(Collectors.toList()); List outputConfigsPhysical2 = targets.getSecondPhysicalCamera().stream() .map(s -> { OutputConfiguration outputConfiguration = new OutputConfiguration(s); outputConfiguration.setPhysicalCameraId(dualCamera.physicalId2); return outputConfiguration; }) .collect(Collectors.toList()); // Put all the output configurations into a single flat array List outputConfigsAll = Stream.of( outputConfigsLogical, outputConfigsPhysical1, outputConfigsPhysical2 ) .filter(Objects::nonNull) .flatMap(Collection::stream) .collect(Collectors.toList()); // Instantiate a session configuration that can be used to create a session SessionConfiguration sessionConfiguration = new SessionConfiguration( SessionConfiguration.SESSION_REGULAR, outputConfigsAll, executor, new CameraCaptureSession.StateCallback() { @Override public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) { cameraCaptureSessionCallback.callback(cameraCaptureSession); } // Omitting for brevity... @Override public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) { cameraCaptureSession.getDevice().close(); } }); // Open the logical camera using the previously defined function openDualCamera(cameraManager, dualCamera, executor, (CameraDevice c) -> // Finally create the session and return via callback c.createCaptureSession(sessionConfiguration)); }

サポートされているストリームの組み合わせについては、createCaptureSession をご覧ください。ストリームの統合は、1 台の論理カメラ上の複数のストリームに対して行われます。互換性は、同じ構成を使用して、そのうちの 1 つのストリームを、同じ論理カメラに属する 2 台の物理カメラからの 2 つのストリームに置き換える場合にも拡張されます。

カメラ セッションの準備ができたら、必要なキャプチャ リクエストを送信します。キャプチャ リクエストの各ターゲットは、関連する物理カメラ(使用中)からデータを受け取るか、論理カメラにフォールバックします。

Zoom のユースケース例

複数の物理カメラの映像を 1 つのストリームに統合することで、ユーザーは複数の物理カメラを切り替えて異なる画角を体験し、異なる「ズームレベル」を効果的にキャプチャできます。

図 4. ズームレベルのユースケースに合わせてカメラを切り替える例(Pixel 3 の広告)

まず、ユーザーが切り替えられるようにする物理カメラのペアを選択します。最大限の効果を得るために、使用可能な最小焦点と最大焦点距離を提供するカメラのペアを選択できます。

Kotlin

fun findShortLongCameraPair(manager: CameraManager, facing: Int? = null): DualCamera? {

    return findDualCameras(manager, facing).map {
        val characteristics1 = manager.getCameraCharacteristics(it.physicalId1)
        val characteristics2 = manager.getCameraCharacteristics(it.physicalId2)

        // Query the focal lengths advertised by each physical camera
        val focalLengths1 = characteristics1.get(
            CameraCharacteristics.LENS_INFO_AVAILABLE_FOCAL_LENGTHS) ?: floatArrayOf(0F)
        val focalLengths2 = characteristics2.get(
            CameraCharacteristics.LENS_INFO_AVAILABLE_FOCAL_LENGTHS) ?: floatArrayOf(0F)

        // Compute the largest difference between min and max focal lengths between cameras
        val focalLengthsDiff1 = focalLengths2.maxOrNull()!! - focalLengths1.minOrNull()!!
        val focalLengthsDiff2 = focalLengths1.maxOrNull()!! - focalLengths2.minOrNull()!!

        // Return the pair of camera IDs and the difference between min and max focal lengths
        if (focalLengthsDiff1 < focalLengthsDiff2) {
            Pair(DualCamera(it.logicalId, it.physicalId1, it.physicalId2), focalLengthsDiff1)
        } else {
            Pair(DualCamera(it.logicalId, it.physicalId2, it.physicalId1), focalLengthsDiff2)
        }

        // Return only the pair with the largest difference, or null if no pairs are found
    }.maxByOrNull { it.second }?.first
}

Java

// Utility functions to find min/max value in float[]
    float findMax(float[] array) {
        float max = Float.NEGATIVE_INFINITY;
        for(float cur: array)
            max = Math.max(max, cur);
        return max;
    }
    float findMin(float[] array) {
        float min = Float.NEGATIVE_INFINITY;
        for(float cur: array)
            min = Math.min(min, cur);
        return min;
    }

DualCamera findShortLongCameraPair(CameraManager manager, Integer facing) {
        return findDualCameras(manager, facing).stream()
                .map(c -> {
                    CameraCharacteristics characteristics1;
                    CameraCharacteristics characteristics2;
                    try {
                        characteristics1 = manager.getCameraCharacteristics(c.physicalId1);
                        characteristics2 = manager.getCameraCharacteristics(c.physicalId2);
                    } catch (CameraAccessException e) {
                        e.printStackTrace();
                        return null;
                    }

                    // Query the focal lengths advertised by each physical camera
                    float[] focalLengths1 = characteristics1.get(
                            CameraCharacteristics.LENS_INFO_AVAILABLE_FOCAL_LENGTHS);
                    float[] focalLengths2 = characteristics2.get(
                            CameraCharacteristics.LENS_INFO_AVAILABLE_FOCAL_LENGTHS);

                    // Compute the largest difference between min and max focal lengths between cameras
                    Float focalLengthsDiff1 = findMax(focalLengths2) - findMin(focalLengths1);
                    Float focalLengthsDiff2 = findMax(focalLengths1) - findMin(focalLengths2);

                    // Return the pair of camera IDs and the difference between min and max focal lengths
                    if (focalLengthsDiff1 < focalLengthsDiff2) {
                        return new Pair<>(new DualCamera(c.logicalId, c.physicalId1, c.physicalId2), focalLengthsDiff1);
                    } else {
                        return new Pair<>(new DualCamera(c.logicalId, c.physicalId2, c.physicalId1), focalLengthsDiff2);
                    }

                }) // Return only the pair with the largest difference, or null if no pairs are found
                .max(Comparator.comparing(pair -> pair.second)).get().first;
    }

この場合、2 つの SurfaceViews(ストリームごとに 1 つずつ)が必要になります。これらの SurfaceViews は、ユーザー操作に基づいてスワップされ、常に 1 つのみが表示されるようになっています。

次のコードは、論理カメラを開いてカメラ出力を設定し、カメラ セッションを作成して、2 つのプレビュー ストリームを開始する方法を示しています。

Kotlin

val cameraManager: CameraManager = ...

// Get the two output targets from the activity / fragment
val surface1 = ...  // from SurfaceView
val surface2 = ...  // from SurfaceView

val dualCamera = findShortLongCameraPair(manager)!!
val outputTargets = DualCameraOutputs(
    null, mutableListOf(surface1), mutableListOf(surface2))

// Here you open the logical camera, configure the outputs and create a session
createDualCameraSession(manager, dualCamera, targets = outputTargets) { session ->

  // Create a single request which has one target for each physical camera
  // NOTE: Each target receive frames from only its associated physical camera
  val requestTemplate = CameraDevice.TEMPLATE_PREVIEW
  val captureRequest = session.device.createCaptureRequest(requestTemplate).apply {
    arrayOf(surface1, surface2).forEach { addTarget(it) }
  }.build()

  // Set the sticky request for the session and you are done
  session.setRepeatingRequest(captureRequest, null, null)
}

Java

CameraManager manager = ...;

        // Get the two output targets from the activity / fragment
        Surface surface1 = ...;  // from SurfaceView
        Surface surface2 = ...;  // from SurfaceView

        DualCamera dualCamera = findShortLongCameraPair(manager, null);
                DualCameraOutputs outputTargets = new DualCameraOutputs(
                null, Collections.singletonList(surface1), Collections.singletonList(surface2));

        // Here you open the logical camera, configure the outputs and create a session
        createDualCameraSession(manager, dualCamera, outputTargets, null, (session) -> {
            // Create a single request which has one target for each physical camera
            // NOTE: Each target receive frames from only its associated physical camera
            CaptureRequest.Builder captureRequestBuilder;
            try {
                captureRequestBuilder = session.getDevice().createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
                Arrays.asList(surface1, surface2).forEach(captureRequestBuilder::addTarget);

                // Set the sticky request for the session and you are done
                session.setRepeatingRequest(captureRequestBuilder.build(), null, null);
            } catch (CameraAccessException e) {
                e.printStackTrace();
            }
        });

あとは、ボタンや SurfaceView のダブルタップなど、2 つのサーフェスを切り替えるための UI をユーザーに提供するだけです。なんらかの形でシーン分析を実行し、2 つのストリームを自動的に切り替えることもできます。

レンズ歪み

すべてのレンズである程度の歪みが生じます。Android では、CameraCharacteristics.LENS_DISTORTION を使用してレンズによって生成された歪みをクエリできます。これは、サポートが終了した CameraCharacteristics.LENS_RADIAL_DISTORTION に代わるものです。論理カメラの場合、歪みは最小限であり、アプリはカメラからのフレームの使用頻度を増減できます。物理カメラの場合、特に広角レンズの場合は、レンズ構成が大きく異なる可能性があります。

デバイスによっては、CaptureRequest.DISTORTION_CORRECTION_MODE を介して自動歪み補正を実装する場合があります。ほとんどのデバイスで、歪み補正はデフォルトでオンになっています。

Kotlin

val cameraSession: CameraCaptureSession = ...

        // Use still capture template to build the capture request
        val captureRequest = cameraSession.device.createCaptureRequest(
            CameraDevice.TEMPLATE_STILL_CAPTURE
        )

        // Determine if this device supports distortion correction
        val characteristics: CameraCharacteristics = ...
        val supportsDistortionCorrection = characteristics.get(
            CameraCharacteristics.DISTORTION_CORRECTION_AVAILABLE_MODES
        )?.contains(
            CameraMetadata.DISTORTION_CORRECTION_MODE_HIGH_QUALITY
        ) ?: false

        if (supportsDistortionCorrection) {
            captureRequest.set(
                CaptureRequest.DISTORTION_CORRECTION_MODE,
                CameraMetadata.DISTORTION_CORRECTION_MODE_HIGH_QUALITY
            )
        }

        // Add output target, set other capture request parameters...

        // Dispatch the capture request
        cameraSession.capture(captureRequest.build(), ...)

Java

CameraCaptureSession cameraSession = ...;

        // Use still capture template to build the capture request
        CaptureRequest.Builder captureRequestBuilder = null;
        try {
            captureRequestBuilder = cameraSession.getDevice().createCaptureRequest(
                    CameraDevice.TEMPLATE_STILL_CAPTURE
            );
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }

        // Determine if this device supports distortion correction
        CameraCharacteristics characteristics = ...;
        boolean supportsDistortionCorrection = Arrays.stream(
                        characteristics.get(
                                CameraCharacteristics.DISTORTION_CORRECTION_AVAILABLE_MODES
                        ))
                .anyMatch(i -> i == CameraMetadata.DISTORTION_CORRECTION_MODE_HIGH_QUALITY);
        if (supportsDistortionCorrection) {
            captureRequestBuilder.set(
                    CaptureRequest.DISTORTION_CORRECTION_MODE,
                    CameraMetadata.DISTORTION_CORRECTION_MODE_HIGH_QUALITY
            );
        }

        // Add output target, set other capture request parameters...

        // Dispatch the capture request
        cameraSession.capture(captureRequestBuilder.build(), ...);

このモードでキャプチャ リクエストを設定すると、カメラで生成できるフレームレートに影響する可能性があります。静止画像のキャプチャに対してのみ、歪み補正を設定することもできます。