.. Copyright (c) 2019, The Linux Foundation. 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 The Linux Foundation 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 "AS IS" AND ANY EXPRESS OR IMPLIED
  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
  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.

.. _basic_tut_3:

********************************************
Basic tutorial 3: Live preview with snapshot
********************************************
This tutorial will walk you through steps that are required to get preview working
both over RTSP as well as via HDMI and take snapshot when the preview is running.

.. warning:: We assume you have already checked :ref:`basic_tut_1`. If not, please
             go through it before proceeding as this tutorial only explains the
             additional code i.e. capturing snapshot.

Code
====
.. literalinclude:: ../../../tests/test-preview-snapshot.py
    :linenos:
    :lines: 28-
    :caption: test-preview-snapshot.py

.. note:: This script is present in the *tests/* directory of the SDK.

Walkthrough
===========
As you would have noticed most of the code is same as the :ref:`basic_tut_1`.
So in this walkthrough we will just see how the snapshot is being captured
using the *captureimage* API.

You can capture image anytime after obtaining the handle to the *CameraClient*
and starting the preview using the *captureimage* API. In the above mentioned example
preview is started and after 30 seconds of preview a snapshot is taken.

::

    print("Taking snapshot")
    if not camera_client.captureimage():
        print("captureimage failed")

.. note:: The captured snapshot is stored on the device as well in the directory
          from where you have triggered this script.

It's time to test
=================

Once you have the application/script ready. You can test it by following the steps mentioned in
:ref:`test` section of the :ref:`getting_started` page.