/* * Copyright (c) 2016-2018, NVIDIA CORPORATION. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of NVIDIA CORPORATION nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include #include "ArgusHelpers.h" #include "CommonOptions.h" #define EXIT_IF_NULL(val,msg) \ {if (!val) {printf("%s\n",msg); return EXIT_FAILURE;}} #define EXIT_IF_NOT_OK(val,msg) \ {if (val!=Argus::STATUS_OK) {printf("%s\n",msg); return EXIT_FAILURE;}} #ifdef ANDROID #define FILE_PREFIX "/sdcard/DCIM/" #else #define FILE_PREFIX "" #endif using namespace Argus; using namespace EGLStream; /* Constants */ static const uint32_t MAX_CAMERA_NUM = 6; static const uint32_t DEFAULT_FRAME_COUNT = 100; static const uint32_t DEFAULT_FPS = 30; static const Size2D STREAM_SIZE(640, 480); /* Globals */ UniqueObj g_cameraProvider; uint32_t g_stream_num = MAX_CAMERA_NUM; uint32_t g_frame_count = DEFAULT_FRAME_COUNT; /* * Program: oneShot * Function: Capture a single image from a camera device and write to a JPG file * Purpose: To demonstrate the most simplistic approach to getting the Argus Framework * running, submitting a capture request, retrieving the resulting image and * then writing the image as a JPEG formatted file. */ int main(int argc, char** argv) { ArgusSamples::CommonOptions options(basename(argv[0]), ArgusSamples::CommonOptions::Option_D_CameraDevice | ArgusSamples::CommonOptions::Option_M_SensorMode); if (!options.parse(argc, argv)) return EXIT_FAILURE; if (options.requestedExit()) return EXIT_SUCCESS; const uint64_t FIVE_SECONDS_IN_NANOSECONDS = 5000000000; /* * Set up Argus API Framework, identify available camera devices, and create * a capture session for the first available device */ Argus::UniqueObj cameraProvider(Argus::CameraProvider::create()); Argus::ICameraProvider *iCameraProvider = Argus::interface_cast(cameraProvider); EXIT_IF_NULL(iCameraProvider, "Cannot get core camera provider interface"); printf("Argus Version: %s\n", iCameraProvider->getVersion().c_str()); Argus::CameraDevice *device = ArgusSamples::ArgusHelpers::getCameraDevice( cameraProvider.get(), options.cameraDeviceIndex()); /* Get the camera devices */ std::vector cameraDevices; iCameraProvider->getCameraDevices(&cameraDevices); if (cameraDevices.size() == 0) ORIGINATE_ERROR("No cameras available"); Argus::SensorMode* sensorMode = ArgusSamples::ArgusHelpers::getSensorMode( device, options.sensorModeIndex()); Argus::ISensorMode *iSensorMode = Argus::interface_cast(sensorMode); if (!iSensorMode) { REPORT_ERROR("Failed to get sensor mode interface"); return EXIT_FAILURE; } printf("Capturing from device %d using sensor mode %d (%dx%d)\n", options.cameraDeviceIndex(), options.sensorModeIndex(), iSensorMode->getResolution().width(), iSensorMode->getResolution().height()); Argus::Status status; Argus::UniqueObj captureSession0( iCameraProvider->createCaptureSession(cameraDevices[0], &status)); EXIT_IF_NOT_OK(status, "Failed to create capture session"); Argus::ICaptureSession *iSession0 = Argus::interface_cast(captureSession0); EXIT_IF_NULL(iSession0, "Cannot get Capture Session Interface"); Argus::UniqueObj captureSession1( iCameraProvider->createCaptureSession(cameraDevices[1], &status)); EXIT_IF_NOT_OK(status, "Failed to create capture session"); Argus::ICaptureSession *iSession1 = Argus::interface_cast(captureSession1); EXIT_IF_NULL(iSession1, "Cannot get Capture Session Interface"); /* * Creates the stream between the Argus camera image capturing * sub-system (producer) and the image acquisition code (consumer). A consumer object is * created from the stream to be used to request the image frame. A successfully submitted * capture request activates the stream's functionality to eventually make a frame available * for acquisition. */ Argus::UniqueObj streamSettings( iSession0->createOutputStreamSettings(Argus::STREAM_TYPE_EGL)); Argus::IEGLOutputStreamSettings *iEGLStreamSettings = Argus::interface_cast(streamSettings); EXIT_IF_NULL(iEGLStreamSettings, "Cannot get IEGLOutputStreamSettings Interface"); iEGLStreamSettings->setPixelFormat(Argus::PIXEL_FMT_YCbCr_420_888); iEGLStreamSettings->setResolution(iSensorMode->getResolution()); iEGLStreamSettings->setMetadataEnable(true); Argus::UniqueObj stream0( iSession0->createOutputStream(streamSettings.get())); EXIT_IF_NULL(stream0, "Failed to create EGLOutputStream 0"); Argus::UniqueObj consumer0( EGLStream::FrameConsumer::create(stream0.get())); EGLStream::IFrameConsumer *iFrameConsumer0 = Argus::interface_cast(consumer0); EXIT_IF_NULL(iFrameConsumer0, "Failed to initialize Consumer 0"); Argus::UniqueObj request0( iSession0->createRequest(Argus::CAPTURE_INTENT_STILL_CAPTURE)); Argus::IRequest *iRequest0 = Argus::interface_cast(request0); EXIT_IF_NULL(iRequest0, "Failed to get capture request interface 0"); status = iRequest0->enableOutputStream(stream0.get()); EXIT_IF_NOT_OK(status, "Failed to enable stream in capture request 0"); Argus::ISourceSettings *iSourceSettings0 = Argus::interface_cast(request0); EXIT_IF_NULL(iSourceSettings0, "Failed to get source settings request interface 0"); iSourceSettings0->setSensorMode(sensorMode); uint32_t requestId0 = iSession0->capture(request0.get()); EXIT_IF_NULL(requestId0, "Failed to submit capture request 0"); Argus::UniqueObj stream1( iSession0->createOutputStream(streamSettings.get())); EXIT_IF_NULL(stream1, "Failed to create EGLOutputStream 1"); Argus::UniqueObj consumer1( EGLStream::FrameConsumer::create(stream1.get())); EGLStream::IFrameConsumer *iFrameConsumer1 = Argus::interface_cast(consumer1); EXIT_IF_NULL(iFrameConsumer1, "Failed to initialize Consumer 1"); Argus::UniqueObj request1( iSession1->createRequest(Argus::CAPTURE_INTENT_STILL_CAPTURE)); Argus::IRequest *iRequest1 = Argus::interface_cast(request1); EXIT_IF_NULL(iRequest1, "Failed to get capture request interface 1"); status = iRequest1->enableOutputStream(stream1.get()); EXIT_IF_NOT_OK(status, "Failed to enable stream in capture request 1"); Argus::ISourceSettings *iSourceSettings1 = Argus::interface_cast(request1); EXIT_IF_NULL(iSourceSettings1, "Failed to get source settings request interface 1"); iSourceSettings1->setSensorMode(sensorMode); uint32_t requestId = iSession1->capture(request1.get()); EXIT_IF_NULL(requestId, "Failed to submit capture request 1"); /* * Acquire a frame generated by the capture request, get the image from the frame * and create a .JPG file of the captured image */ Argus::UniqueObj frame0( iFrameConsumer0->acquireFrame(FIVE_SECONDS_IN_NANOSECONDS, &status)); EGLStream::IFrame *iFrame0 = Argus::interface_cast(frame0); EXIT_IF_NULL(iFrame0, "Failed to get IFrame interface 0"); Argus::UniqueObj frame1( iFrameConsumer1->acquireFrame(FIVE_SECONDS_IN_NANOSECONDS, &status)); EGLStream::IFrame *iFrame1 = Argus::interface_cast(frame1); EXIT_IF_NULL(iFrame1, "Failed to get IFrame interface 1"); EGLStream::Image *image0 = iFrame0->getImage(); EXIT_IF_NULL(image0, "Failed to get Image from iFrame0->getImage()"); EGLStream::IImageJPEG *iImageJPEG0 = Argus::interface_cast(image0); EXIT_IF_NULL(iImageJPEG0, "Failed to get ImageJPEG0 Interface"); status = iImageJPEG0->writeJPEG(FILE_PREFIX "argus_oneShot.jpg"); EXIT_IF_NOT_OK(status, "Failed to write JPEG"); EGLStream::Image *image1 = iFrame1->getImage(); EXIT_IF_NULL(image1, "Failed to get Image from iFrame1->getImage() 1"); EGLStream::IImageJPEG *iImageJPEG1 = Argus::interface_cast(image1); EXIT_IF_NULL(iImageJPEG1, "Failed to get ImageJPEG Interface 1"); status = iImageJPEG1->writeJPEG(FILE_PREFIX "argus_oneShot1.jpg"); EXIT_IF_NOT_OK(status, "Failed to write JPEG 1"); printf("Wrote file: " FILE_PREFIX "argus_oneShot1.jpg\n"); // Shut down Argus. cameraProvider.reset(); return EXIT_SUCCESS; }