開源相機管理庫Aravis例程學習(六)——camera-features

paw5zx發表於2024-05-01

目錄
  • 簡介
  • 例程程式碼
  • 函式說明
    • arv_camera_get_integer
    • arv_camera_get_string

簡介

本文針對官方例程中的:04-camera-features做簡單的講解。並介紹其中呼叫的arv_camera_get_integerarv_camera_get_string

aravis版本:0.8.31
作業系統:ubuntu-20.04
gcc版本:9.4.0

例程程式碼

這段程式碼使用Aravis的API,獲取相機的一些基本設定,如影像的寬度、高度和畫素格式,主要操作步驟如下:

  • 連線相機
  • 獲取影像寬度,高度,畫素格式等資訊
  • 釋放資源
/* SPDX-License-Identifier:Unlicense */

/* Aravis header */
#include <arv.h>

/* Standard headers */
#include <stdlib.h>
#include <stdio.h>

/*
 * Connect to the first available camera, then display the current settings for image width and height, as well as the
 * pixel format, using the more generic ArvCamera feature API.
 */

int main (int argc, char **argv)
{
	ArvCamera *camera;
	GError *error = NULL;

	//連線相機
	camera = arv_camera_new (NULL, &error);

	if (ARV_IS_CAMERA (camera)) {
		int width;
		int height;
		const char *pixel_format;

		printf ("Found camera '%s'\n", arv_camera_get_model_name (camera, NULL));

		/* Retrieve generally mandatory features for transmitters */

		if (!error) width = arv_camera_get_integer (camera, "Width", &error);
		if (!error) height = arv_camera_get_integer (camera, "Height", &error);
		if (!error) pixel_format = arv_camera_get_string (camera, "PixelFormat", &error);

		if (error == NULL) {
			printf ("Width = %d\n", width);
			printf ("Height = %d\n", height);
			printf ("Pixel format = %s\n", pixel_format);
		}

		g_clear_object (&camera);
	}

	if (error != NULL) {
		/* En error happened, display the correspdonding message */
		printf ("Error: %s\n", error->message);
		return EXIT_FAILURE;
	}

	return EXIT_SUCCESS;
}

這個例程與03-camera-api實現的功能相似,但是不同的是本文的程式碼使用的是更為通用的API(arv_camera_get_integerarv_camera_get_string)來獲取的相機的引數。

我們檢視03-camera-api中的arv_camera_get_regionarv_camera_get_pixel_format_as_string的函式定義可以發現,他們的底層其實就是透過呼叫arv_camera_get_integerarv_camera_get_string來實現的相關功能:

//file: arvcamera.c
void arv_camera_get_region (ArvCamera *camera, gint *x, gint *y, gint *width, gint *height, GError **error)
{
	ArvCameraPrivate *priv = arv_camera_get_instance_private (camera);
	GError *local_error = NULL;

	g_return_if_fail (ARV_IS_CAMERA (camera));

	if (x != NULL)
		*x = priv->has_region_offset ? arv_camera_get_integer (camera, "OffsetX", &local_error) : 0;
	if (y != NULL && local_error == NULL)
		*y = priv->has_region_offset ? arv_camera_get_integer (camera, "OffsetY", &local_error) : 0;
	if (width != NULL && local_error == NULL)
		*width = arv_camera_get_integer (camera, "Width", &local_error);
	if (height != NULL && local_error == NULL)
		*height = arv_camera_get_integer (camera, "Height", &local_error);

	if (local_error != NULL)
		g_propagate_error (error, local_error);
}


const char * arv_camera_get_pixel_format_as_string (ArvCamera *camera, GError **error)
{
	return arv_camera_get_string (camera, "PixelFormat", error);
}

執行結果:

函式說明

arv_camera_get_integer

簡介:獲取已連線相機的一個整數型特性的值

gint64 arv_camera_get_integer (
  ArvCamera* camera,
  const char* feature,
  GError** error
)

Available since: 0.8.0

arv_camera_get_string

簡介:獲取已連線相機的一個字串型特性的值

const char* arv_camera_get_string (
  ArvCamera* camera,
  const char* feature,
  GError** error
)

Available since: 0.8.0

相關文章