Utilizzare più stream della videocamera contemporaneamente

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

Un'applicazione per fotocamera può utilizzare più stream di frame contemporaneamente. In alcuni casi, stream diversi richiedono persino una risoluzione dei frame o un formato dei pixel diversi. Alcuni casi d'uso tipici includono:

  • 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, un altro per il rilevamento dei codici a barre.
  • Fotografia computazionale: uno stream per l'anteprima, un altro per il rilevamento di volti o scene.

L'elaborazione dei frame comporta un costo di prestazioni non trascurabile, che si moltiplica quando si esegue l'elaborazione di stream o pipeline in parallelo.

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

Più target per richiesta

Più stream della videocamera possono essere combinati in un unico CameraCaptureRequest. Il seguente snippet di codice mostra come configurare una sessione della fotocamera con uno stream per l'anteprima della fotocamera e un altro stream 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 basic 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 basic 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 correttamente le piattaforme di destinazione, questo codice produrrà solo stream che soddisfano i requisiti minimi di FPS determinati da StreamComfigurationMap.GetOutputMinFrameDuration(int, Size) e StreamComfigurationMap.GetOutputStallDuration(int, Size). Le prestazioni effettive variano da dispositivo a dispositivo, anche se Android fornisce alcune garanzie per il supporto di combinazioni specifiche in base a tre variabili: tipo di output, dimensioni dell'output e livello hardware.

L'utilizzo di una combinazione di variabili non supportata potrebbe funzionare con una frequenza fotogrammi bassa; in caso contrario, attiverà uno dei callback di errore. La documentazione di createCaptureSession descrive ciò che è garantito che funzioni.

Tipo output

Tipo di output si riferisce al formato in cui vengono codificati i frame. I valori possibili sono PRIV, YUV, JPEG e RAW. La documentazione per createCaptureSession li descrive.

Quando scegli il tipo di output dell'applicazione, se l'obiettivo è massimizzare la compatibilità, utilizza ImageFormat.YUV_420_888 per l'analisi dei frame e ImageFormat.JPEG per le immagini statiche. Per gli scenari di anteprima e registrazione, probabilmente utilizzerai un SurfaceView, TextureView, MediaRecorder, MediaCodec o RenderScript.Allocation. In questi casi, non specificare un formato dell'immagine. Per motivi di compatibilità, verrà conteggiato come ImageFormat.PRIVATE, indipendentemente dal formato effettivo utilizzato internamente. Per eseguire una query sui formati supportati da un dispositivo in base al suo CameraCharacteristics, utilizza il seguente codice:

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 di output

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

Le dimensioni di output disponibili dipendono dal formato scelto. Dato l'oggetto CameraCharacteristics e un formato, puoi eseguire una query per le 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 direttamente dal framework della fotocamera:

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 le dimensioni MAXIMUM, ordina le dimensioni di output in base all'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 che meglio corrisponde alla risoluzione dello schermo del dispositivo o a 1080p (1920 x 1080), a seconda di quale sia inferiore. Le proporzioni potrebbero non corrispondere esattamente a quelle dello schermo, quindi potrebbe essere necessario applicare il letterbox o il ritaglio allo stream per visualizzarlo in modalità a schermo intero. Per ottenere le dimensioni di anteprima corrette, confronta le dimensioni di output disponibili con quelle del display, tenendo conto che il display potrebbe essere ruotato.

Il seguente codice definisce una classe helper, SmartSize, che semplifica i confronti 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 hardware supportato

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

Con un oggetto CameraCharacteristics, puoi recuperare il livello 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);

Metto insieme tutti gli elementi

Con il tipo di output, le dimensioni dell'output e il livello hardware, puoi determinare quali combinazioni di stream sono valide. Il seguente grafico è un'istantanea delle configurazioni supportate da un CameraDevice con livello hardware 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 GPU o registrazione video senza anteprima.
JPEG MAXIMUM Acquisizione di immagini fisse senza mirino.
YUV MAXIMUM Elaborazione di video/immagini in-app.
PRIV PREVIEW JPEG MAXIMUM Acquisizione di immagini statiche standard.
YUV PREVIEW JPEG MAXIMUM Elaborazione in-app e acquisizione di immagini fisse.
PRIV PREVIEW PRIV PREVIEW Registrazione standard.
PRIV PREVIEW YUV PREVIEW Anteprima più elaborazione in-app.
PRIV PREVIEW YUV PREVIEW JPEG MAXIMUM Acquisizione di immagini fisse ed elaborazione in-app.

LEGACY è il livello hardware più basso possibile. Questa tabella mostra che ogni dispositivo che supporta Camera2 (livello API 21 e versioni successive) può generare fino a tre stream simultanei utilizzando la configurazione corretta e se non ci sono troppi overhead che limitano le prestazioni, come memoria, CPU o vincoli termici.

L'app deve anche configurare i buffer di output di targeting. Ad esempio, per scegliere come target un dispositivo con livello hardware LEGACY, puoi configurare due superfici di output target, una che utilizza ImageFormat.PRIVATE e un'altra che utilizza ImageFormat.YUV_420_888. Questa è una combinazione supportata quando utilizzi la dimensione PREVIEW. Utilizzando la funzione definita in precedenza in questo documento, per ottenere le dimensioni di anteprima richieste per un ID videocamera è 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);

È necessario 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 la SurfaceView in modo che corrisponda alle dimensioni dell'output della fotocamera chiamando SurfaceHolder.setFixedSize() oppure puoi adottare un approccio simile a AutoFitSurfaceView dal modulo comune degli esempi di fotocamera su GitHub, che imposta una dimensione assoluta, tenendo conto sia delle proporzioni sia dello spazio disponibile, con regolazione automatica quando vengono attivate le modifiche all'attività.

La configurazione dell'altra piattaforma da ImageReader con il formato scelto è 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 hardware LEGACY ha come target i dispositivi con il minimo comune denominatore. Puoi aggiungere la ramificazione condizionale e utilizzare le dimensioni RECORD per una delle superfici di destinazione di output nei dispositivi con livello hardware LIMITED o persino aumentarle alle dimensioni MAXIMUM per i dispositivi con livello hardware FULL.