Utilizzare più stream della videocamera contemporaneamente

Nota: questa pagina fa riferimento al pacchetto Fotocamera2. A meno che la tua app non richieda funzionalità specifiche di basso livello di Fotocamera2, ti consigliamo di utilizzare FotocameraX. Sia CameraX che Camera2 supportano Android 5.0 (livello API 21) e versioni successive.

Un'applicazione fotocamera può utilizzare più di un flusso di fotogrammi contemporaneamente. In alcuni casi, stream diversi richiedono anche una risoluzione dei fotogrammi o un formato pixel diversi. Ecco alcuni casi d'uso tipici:

  • Registrazione video: uno stream per l'anteprima, un altro codificato e salvato in un file.
  • Scansione di codici a barre: uno stream per l'anteprima e un altro per il rilevamento dei codici a barre.
  • Fotografia computazionale: uno stream per l'anteprima, un altro per il rilevamento di volti/scena.

Le prestazioni non sono banali per l'elaborazione dei frame, mentre i costi vengono moltiplicati per l'elaborazione di flussi o pipeline paralleli.

Risorse come CPU, GPU e DSP potrebbero essere in grado di sfruttare le funzionalità di rielaborazione del framework, ma risorse come la memoria cresceranno in modo lineare.

Più target per richiesta

È possibile combinare più stream della videocamera in un'unica CameraCaptureRequest. Il seguente snippet di codice illustra come configurare una sessione della videocamera con uno stream per l'anteprima della videocamera e un altro per l'elaborazione delle immagini:

Kotlin

val session: CameraCaptureSession = ...  // from CameraCaptureSession.StateCallback

// You will use the preview capture template for the combined streams
// because it is optimized for low latency; for high-quality images, use
// TEMPLATE_STILL_CAPTURE, and for a steady frame rate use TEMPLATE_RECORD
val requestTemplate = CameraDevice.TEMPLATE_PREVIEW
val combinedRequest = session.device.createCaptureRequest(requestTemplate)

// Link the Surface targets with the combined request
combinedRequest.addTarget(previewSurface)
combinedRequest.addTarget(imReaderSurface)

// In this simple case, the SurfaceView gets updated automatically. ImageReader
// has its own callback that you have to listen to in order to retrieve the
// frames so there is no need to set up a callback for the capture request
session.setRepeatingRequest(combinedRequest.build(), null, null)

Java

CameraCaptureSession session = …;  // from CameraCaptureSession.StateCallback

// You will use the preview capture template for the combined streams
// because it is optimized for low latency; for high-quality images, use
// TEMPLATE_STILL_CAPTURE, and for a steady frame rate use TEMPLATE_RECORD
        CaptureRequest.Builder combinedRequest = session.getDevice().createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);

// Link the Surface targets with the combined request
        combinedRequest.addTarget(previewSurface);
        combinedRequest.addTarget(imReaderSurface);

// In this simple case, the SurfaceView gets updated automatically. ImageReader
// has its own callback that you have to listen to in order to retrieve the
// frames so there is no need to set up a callback for the capture request
        session.setRepeatingRequest(combinedRequest.build(), null, null);

Se configuri le piattaforme di destinazione correttamente, questo codice produrrà solo flussi che soddisfano il numero di FPS minimo determinato da StreamComfigurationMap.GetOutputMinFrameDuration(int, Size) e StreamComfigurationMap.GetOutputStallDuration(int, Size). Le prestazioni effettive variano da dispositivo a dispositivo, sebbene Android fornisca alcune garanzie per il supporto di combinazioni specifiche in base a tre variabili: tipo di output, dimensione output e livello hardware.

L'utilizzo di una combinazione di variabili non supportata potrebbe funzionare a una frequenza fotogrammi bassa; in caso contrario, verrà attivato uno dei callback di errore. La documentazione relativa a createCaptureSession descrive il funzionamento garantito.

Tipo di output

Il tipo di output fa riferimento al formato in cui vengono codificati i frame. I valori possibili sono PRIV, YUV, JPEG e RAW. Sono descritti nella documentazione relativa a createCaptureSession.

Quando scegli il tipo di output della tua applicazione, se l'obiettivo è massimizzare la compatibilità, utilizza ImageFormat.YUV_420_888 per l'analisi del frame e ImageFormat.JPEG per le immagini statiche. Per gli scenari di anteprima e registrazione, è probabile che tu stia utilizzando SurfaceView, TextureView, MediaRecorder, MediaCodec o RenderScript.Allocation. In questi casi, non specificare un formato dell'immagine. Ai fini della compatibilità, verrà conteggiata come ImageFormat.PRIVATE, indipendentemente dal formato effettivamente utilizzato internamente. Per eseguire query sui formati supportati da un dispositivo in base al relativo CameraCharacteristics, utilizza il codice seguente:

Kotlin

val characteristics: CameraCharacteristics = ...
val supportedFormats = characteristics.get(
    CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP).outputFormats

Java

CameraCharacteristics characteristics = …;
        int[] supportedFormats = characteristics.get(
CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP).getOutputFormats();

Dimensioni output

Tutte le dimensioni di output disponibili sono elencate per StreamConfigurationMap.getOutputSizes(), ma solo due sono correlate alla compatibilità: PREVIEW e MAXIMUM. Le dimensioni fungono da limiti superiori. Se un elemento con dimensioni PREVIEW funziona, è valido anche qualsiasi elemento con una dimensione inferiore a PREVIEW. Lo stesso vale per MAXIMUM. La documentazione relativa a CameraDevice illustra queste dimensioni.

Le dimensioni di output disponibili dipendono dal formato scelto. Dati l'elemento CameraCharacteristics e un formato, puoi eseguire query sulle dimensioni di output disponibili in questo modo:

Kotlin

val characteristics: CameraCharacteristics = ...
val outputFormat: Int = ...  // such as ImageFormat.JPEG
val sizes = characteristics.get(
    CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)
    .getOutputSizes(outputFormat)

Java

CameraCharacteristics characteristics = …;
        int outputFormat = …;  // such as ImageFormat.JPEG
Size[] sizes = characteristics.get(
                CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)
                .getOutputSizes(outputFormat);

Nei casi d'uso di anteprima e registrazione della fotocamera, utilizza la classe target per determinare le dimensioni supportate. Il formato verrà gestito dal framework della fotocamera stesso:

Kotlin

val characteristics: CameraCharacteristics = ...
val targetClass: Class <T> = ...  // such as SurfaceView::class.java
val sizes = characteristics.get(
    CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)
    .getOutputSizes(targetClass)

Java

CameraCharacteristics characteristics = …;
   int outputFormat = …;  // such as ImageFormat.JPEG
   Size[] sizes = characteristics.get(
                CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)
                .getOutputSizes(outputFormat);

Per ottenere la dimensione MAXIMUM, ordina le dimensioni di output per area e restituisci quella più grande:

Kotlin

fun <T>getMaximumOutputSize(
    characteristics: CameraCharacteristics, targetClass: Class <T>, format: Int? = null):
    Size {
  val config = characteristics.get(
      CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)

  // If image format is provided, use it to determine supported sizes; or else use target class
  val allSizes = if (format == null)
    config.getOutputSizes(targetClass) else config.getOutputSizes(format)
  return allSizes.maxBy { it.height * it.width }
}

Java

 @RequiresApi(api = Build.VERSION_CODES.N)
    <T> Size getMaximumOutputSize(CameraCharacteristics characteristics,
                                            Class <T> targetClass,
                                            Integer format) {
        StreamConfigurationMap config = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);

        // If image format is provided, use it to determine supported sizes; else use target class
        Size[] allSizes;
        if (format == null) {
            allSizes = config.getOutputSizes(targetClass);
        } else {
            allSizes = config.getOutputSizes(format);
        }
        return Arrays.stream(allSizes).max(Comparator.comparing(s -> s.getHeight() * s.getWidth())).get();
    }

PREVIEW si riferisce alla dimensione più adatta alla risoluzione dello schermo del dispositivo o a 1080p (1920 x 1080), a seconda di quale delle due opzioni è minore. Poiché le proporzioni potrebbero non corrispondere esattamente alle proporzioni dello schermo, potresti dover applicare letterbox o ritagliare lo stream per visualizzarlo in modalità a schermo intero. Per ottenere le dimensioni di anteprima corrette, confronta le dimensioni di output disponibili con quelle di visualizzazione tenendo conto che il display può essere ruotato.

Il codice seguente definisce una classe helper, SmartSize, che semplificherà un po' più il confronto delle dimensioni:

Kotlin

/** Helper class used to pre-compute shortest and longest sides of a [Size] */
class SmartSize(width: Int, height: Int) {
    var size = Size(width, height)
    var long = max(size.width, size.height)
    var short = min(size.width, size.height)
    override fun toString() = "SmartSize(${long}x${short})"
}

/** Standard High Definition size for pictures and video */
val SIZE_1080P: SmartSize = SmartSize(1920, 1080)

/** Returns a [SmartSize] object for the given [Display] */
fun getDisplaySmartSize(display: Display): SmartSize {
    val outPoint = Point()
    display.getRealSize(outPoint)
    return SmartSize(outPoint.x, outPoint.y)
}

/**
 * Returns the largest available PREVIEW size. For more information, see:
 * https://d.android.com/reference/android/hardware/camera2/CameraDevice
 */
fun <T>getPreviewOutputSize(
        display: Display,
        characteristics: CameraCharacteristics,
        targetClass: Class <T>,
        format: Int? = null
): Size {

    // Find which is smaller: screen or 1080p
    val screenSize = getDisplaySmartSize(display)
    val hdScreen = screenSize.long >= SIZE_1080P.long || screenSize.short >= SIZE_1080P.short
    val maxSize = if (hdScreen) SIZE_1080P else screenSize

    // If image format is provided, use it to determine supported sizes; else use target class
    val config = characteristics.get(
            CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)!!
    if (format == null)
        assert(StreamConfigurationMap.isOutputSupportedFor(targetClass))
    else
        assert(config.isOutputSupportedFor(format))
    val allSizes = if (format == null)
        config.getOutputSizes(targetClass) else config.getOutputSizes(format)

    // Get available sizes and sort them by area from largest to smallest
    val validSizes = allSizes
            .sortedWith(compareBy { it.height * it.width })
            .map { SmartSize(it.width, it.height) }.reversed()

    // Then, get the largest output size that is smaller or equal than our max size
    return validSizes.first { it.long <= maxSize.long && it.short <= maxSize.short }.size
}

Java

/** Helper class used to pre-compute shortest and longest sides of a [Size] */
    class SmartSize {
        Size size;
        double longSize;
        double shortSize;

        public SmartSize(Integer width, Integer height) {
            size = new Size(width, height);
            longSize = max(size.getWidth(), size.getHeight());
            shortSize = min(size.getWidth(), size.getHeight());
        }

        @Override
        public String toString() {
            return String.format("SmartSize(%sx%s)", longSize, shortSize);
        }
    }

    /** Standard High Definition size for pictures and video */
    SmartSize SIZE_1080P = new SmartSize(1920, 1080);

    /** Returns a [SmartSize] object for the given [Display] */
    SmartSize getDisplaySmartSize(Display display) {
        Point outPoint = new Point();
        display.getRealSize(outPoint);
        return new SmartSize(outPoint.x, outPoint.y);
    }

    /**
     * Returns the largest available PREVIEW size. For more information, see:
     * https://d.android.com/reference/android/hardware/camera2/CameraDevice
     */
    @RequiresApi(api = Build.VERSION_CODES.N)
    <T> Size getPreviewOutputSize(
            Display display,
            CameraCharacteristics characteristics,
            Class <T> targetClass,
            Integer format
    ){

        // Find which is smaller: screen or 1080p
        SmartSize screenSize = getDisplaySmartSize(display);
        boolean hdScreen = screenSize.longSize >= SIZE_1080P.longSize || screenSize.shortSize >= SIZE_1080P.shortSize;
        SmartSize maxSize;
        if (hdScreen) {
            maxSize = SIZE_1080P;
        } else {
            maxSize = screenSize;
        }

        // If image format is provided, use it to determine supported sizes; else use target class
        StreamConfigurationMap config = characteristics.get(
                CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
        if (format == null)
            assert(StreamConfigurationMap.isOutputSupportedFor(targetClass));
        else
            assert(config.isOutputSupportedFor(format));
        Size[] allSizes;
        if (format == null) {
            allSizes = config.getOutputSizes(targetClass);
        } else {
            allSizes = config.getOutputSizes(format);
        }

        // Get available sizes and sort them by area from largest to smallest
        List <Size> sortedSizes = Arrays.asList(allSizes);
        List <SmartSize> validSizes =
                sortedSizes.stream()
                        .sorted(Comparator.comparing(s -> s.getHeight() * s.getWidth()))
                        .map(s -> new SmartSize(s.getWidth(), s.getHeight()))
                        .sorted(Collections.reverseOrder()).collect(Collectors.toList());

        // Then, get the largest output size that is smaller or equal than our max size
        return validSizes.stream()
                .filter(s -> s.longSize <= maxSize.longSize && s.shortSize <= maxSize.shortSize)
                .findFirst().get().size;
    }

Controlla il livello di hardware supportato

Per determinare le funzionalità disponibili in fase di runtime, controlla il livello di hardware supportato utilizzando CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL.

Con un oggetto CameraCharacteristics, puoi recuperare il livello di hardware con una singola istruzione:

Kotlin

val characteristics: CameraCharacteristics = ...

// Hardware level will be one of:
// - CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY,
// - CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_EXTERNAL,
// - CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED,
// - CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_FULL,
// - CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_3
val hardwareLevel = characteristics.get(
        CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL)

Java

CameraCharacteristics characteristics = ...;

// Hardware level will be one of:
// - CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY,
// - CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_EXTERNAL,
// - CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED,
// - CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_FULL,
// - CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_3
Integer hardwareLevel = characteristics.get(
                CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL);

Riuniamo tutti i pezzi

Con il tipo di output, la dimensione dell'output e il livello hardware, puoi determinare quali combinazioni di flussi sono valide. Il grafico seguente è uno snapshot delle configurazioni supportate da un elemento CameraDevice con livello hardware di LEGACY.

Target 1 Target 2 Target 3 Casi d'uso di esempio
Tipo Dimensione massima Tipo Dimensione massima Tipo Dimensione massima
PRIV MAXIMUM Anteprima semplice, elaborazione video tramite GPU o registrazione video senza anteprima.
JPEG MAXIMUM Acquisizione di fermi immagine senza mirino.
YUV MAXIMUM Elaborazione di immagini/video in-app.
PRIV PREVIEW JPEG MAXIMUM Immagini fisse standard.
YUV PREVIEW JPEG MAXIMUM Elaborazione in-app e acquisizione di immagini fisse.
PRIV PREVIEW PRIV PREVIEW Registrazione standard.
PRIV PREVIEW YUV PREVIEW Anteprima ed elaborazione in-app.
PRIV PREVIEW YUV PREVIEW Anteprima ed elaborazione in-app.
PRIV PREVIEW YUV PREVIEW JPEG MAXIMUM Acquisizione ancora più elaborazione in-app.

LEGACY è il livello hardware più basso possibile. Questa tabella mostra che ogni dispositivo che supporta Camera2 (livello API 21 e livelli successivi) può produrre fino a tre flussi simultanei utilizzando la configurazione corretta e, se le prestazioni non sono limitate da un sovraccarico, come memoria, CPU o vincoli termici.

L'app deve anche configurare i buffer di output del targeting. Ad esempio, per scegliere come target un dispositivo con livello hardware LEGACY, puoi configurare due piattaforme di output di destinazione, una utilizzando ImageFormat.PRIVATE e un'altra utilizzando ImageFormat.YUV_420_888. Questa è una combinazione supportata durante l'utilizzo della dimensione PREVIEW. Se utilizzi la funzione definita in precedenza in questo argomento, per ottenere le dimensioni di anteprima necessarie per un ID fotocamera è necessario il seguente codice:

Kotlin

val characteristics: CameraCharacteristics = ...
val context = this as Context  // assuming you are inside of an activity

val surfaceViewSize = getPreviewOutputSize(
    context, characteristics, SurfaceView::class.java)
val imageReaderSize = getPreviewOutputSize(
    context, characteristics, ImageReader::class.java, format = ImageFormat.YUV_420_888)

Java

CameraCharacteristics characteristics = ...;
        Context context = this; // assuming you are inside of an activity

        Size surfaceViewSize = getPreviewOutputSize(
                context, characteristics, SurfaceView.class);
        Size imageReaderSize = getPreviewOutputSize(
                context, characteristics, ImageReader.class, format = ImageFormat.YUV_420_888);

Devi attendere che SurfaceView sia pronto utilizzando i callback forniti:

Kotlin

val surfaceView = findViewById <SurfaceView>(...)
surfaceView.holder.addCallback(object : SurfaceHolder.Callback {
  override fun surfaceCreated(holder: SurfaceHolder) {
    // You do not need to specify image format, and it will be considered of type PRIV
    // Surface is now ready and you could use it as an output target for CameraSession
  }
  ...
})

Java

SurfaceView surfaceView = findViewById <SurfaceView>(...);

surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
                // You do not need to specify image format, and it will be considered of type PRIV
                // Surface is now ready and you could use it as an output target for CameraSession
            }
            ...
        });

Puoi forzare SurfaceView a far corrispondere le dimensioni dell'output della videocamera chiamando SurfaceHolder.setFixedSize() oppure puoi adottare un approccio simile a AutoFitSurfaceView del Modulo comune degli esempi di fotocamere su GitHub, che imposta una dimensione assoluta, prendendo in considerazione sia le proporzioni sia lo spazio disponibile, regolando automaticamente quando vengono attivate le modifiche all'attività.

La configurazione dell'altra piattaforma da ImageReader con il formato desiderato è più semplice, poiché non ci sono callback da attendere:

Kotlin

val frameBufferCount = 3  // just an example, depends on your usage of ImageReader
val imageReader = ImageReader.newInstance(
    imageReaderSize.width, imageReaderSize.height, ImageFormat.YUV_420_888,
    frameBufferCount)

Java

int frameBufferCount = 3;  // just an example, depends on your usage of ImageReader
ImageReader imageReader = ImageReader.newInstance(
                imageReaderSize.width, imageReaderSize.height, ImageFormat.YUV_420_888,
                frameBufferCount);

Quando utilizzi un buffer di destinazione di blocco come ImageReader, elimina i frame dopo averli utilizzati:

Kotlin

imageReader.setOnImageAvailableListener({
  val frame =  it.acquireNextImage()
  // Do something with "frame" here
  it.close()
}, null)

Java

imageReader.setOnImageAvailableListener(listener -> {
            Image frame = listener.acquireNextImage();
            // Do something with "frame" here
            listener.close();
        }, null);

Il livello di hardware LEGACY ha come target i dispositivi con il minimo comune denominatore. Puoi aggiungere la diramazione condizionale e utilizzare la dimensione RECORD per una delle piattaforme di destinazione di output nei dispositivi con livello hardware LIMITED o persino aumentarla alla dimensione MAXIMUM per i dispositivi con livello hardware FULL.