#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：my_work 
@File    ：camera.py
@IDE     ：PyCharm 
@Author  ：hjw
@Date    ：2024/8/13 11:34 
'''
import PyRVC as RVC
import numpy as np

class camera:

    def __init__(self):
        self.caminit_isok = False
        RVC.SystemInit()

        # Choose RVC X Camera type (USB, GigE or All)
        opt = RVC.SystemListDeviceTypeEnum.GigE

        # Scan all RVC X Camera devices.
        ret, devices = RVC.SystemListDevices(opt)
        print("RVC X Camera devices number:%d" % len(devices))

        #  Find whether any RVC X Camera is connected or not.
        if len(devices) == 0:
            print("Can not find any RVC X Camera!")
            RVC.SystemShutdown()
        else:
            print("devices size = %d" % len(devices))

            # Create a RVC X Camera and choose use left side camera.
            self.x = RVC.X1.Create(devices[0], RVC.CameraID_Left)
            # x = RVC.X1.Create(devices[0], RVC.CameraID_Right)

            # Test RVC X Camera is valid or not.
            if self.x.IsValid() == True:
                print("RVC X Camera is valid!")
                # Open RVC X Camera.
                ret1 = self.x.Open()
                # Test RVC X Camera is opened or not.
                if ret1 and self.x.IsOpen() == True:
                    print("RVC X Camera is opened!")
                    self.caminit_isok = True
                else:
                    print("RVC X Camera is not opened!")
                    RVC.X1.Destroy(self.x)
                    RVC.SystemShutdown()
                    self.caminit_isok = False
            else:
                print("RVC X Camera is not valid!")
                RVC.X1.Destroy(self.x)
                RVC.SystemShutdown()
                self.caminit_isok = False


    def get_img(self):
        ""
        '''
        :param api: None
        :return: ret ,img 
        '''
        if self.caminit_isok == False:
            return 0, None
        else:
            # Capture a point map and a image.
            ret2 = self.x.Capture()
            # Create saving address of image and point map.

            if ret2 == True:
                print("RVC X Camera capture successed!")

                # Get image data and image size.
                img = self.x.GetImage()
                # Convert image to array and save it.
                img = np.array(img, copy=False)
                return 1, img
            else:
                print("RVC X Camera capture failed!")
                self.x.Close()
                RVC.X1.Destroy(self.x)
                RVC.SystemShutdown()
                return 0, None

    def get_point_map(self):
        ""
        '''
        :param api: None
        :return: img 
        '''
        if self.caminit_isok == False:
            return 0, None
        else:
            # Capture a point map and a image.
            ret2 = self.x.Capture()
            # Create saving address of image and point map.

            if ret2 == True:
                print("RVC X Camera capture successed!")
                # Convert point map (m) to array and save it.
                pm = np.array(self.x.GetPointMap(), copy=False)
                return 1, pm
            else:
                print("RVC X Camera capture failed!")
                self.x.Close()
                RVC.X1.Destroy(self.x)
                RVC.SystemShutdown()
                return 0, None

    def get_img_and_point_map(self):
        ""
        '''
        :param api: None
        :return: ret , img, point_map
        '''
        if self.caminit_isok == False:
            return 0, None, None
        else:
            # Capture a point map and a image.
            ret2 = self.x.Capture()
            # Create saving address of image and point map.

            if ret2 == True:
                print("RVC X Camera capture successed!")

                # Get image data and image size.
                img = self.x.GetImage()
                # Convert image to array and save it.
                img = np.array(img, copy=False)

                # Convert point map (m) to array and save it.
                pm = np.array(self.x.GetPointMap(), copy=False)
                return 1, img, pm
            else:
                print("RVC X Camera capture failed!")
                self.x.Close()
                RVC.X1.Destroy(self.x)
                RVC.SystemShutdown()
                return 0, None, None

    def release(self):
        if self.caminit_isok == False:
            RVC.SystemShutdown()
        else:
            RVC.X1.Destroy(self.x)
            RVC.SystemShutdown()


