gphoto2

package module
v0.1.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 21, 2023 License: LGPL-2.1 Imports: 7 Imported by: 0

README

gphoto2

Partial Go bindings for http://www.gphoto.org/proj/libgphoto2/

History

Much of the code is copied from and/or insipired of https://github.com/szank/gphoto which seems abandonend and had a couple of bugs. I waanted to write a simple stop motion program and this is an artefact from it. So it's claiming to or trying to cover the whole library but you can do a lot, and I'm very open to suggestions and/or merge requests. I only have access to one Nikon camera so it's not tested with any other brand, and it seems like all camera manufacturers have their own quirks.

Installlation

To build the library you need to have libgphoto2-6 and libgphoto2-port12 or later installed.

Usage

Under examples are there a couple of basic usage examples for setting shutter speed, taking a photo, entering live view and copying images from there. A very simple example of taking a shot would be:

func main() {
	camera, err := gphoto2.NewCamera("")
	if err != nil {
		panic(fmt.Sprintf("%s: %s", "Failed to connect to camera, make sure it's around!", err))
	}
    snapFile := "/tmp/testshot.jpeg"
	if f, err := os.Create(snapFile); err != nil {
		fmt.Println("Failed to create temp file", snapFile, "giving up!", err)
	} else {
		fmt.Println("Taking shot, then copy to", snapFile)
		if err := c.CaptureDownload(f, false); err != nil {
			fmt.Println("Failed to capture!", err)
		}
	}
	camera.Exit()
	camera.Free()
}

Notes

For Nikon (I don't know about other cameras as I haven't been able to test with them) you want to put the DSLR in manual mode, and turn auto focus off typically. Manual mode is necessary to be able to change many of the settings, and to be able to enter live view mode for instance. Manual focus makes taking shots a lot more reliable and fast as the camera won't have to focus for each shot.

Documentation

Index

Constants

View Source
const (
	//GPOK means no error
	GPOK = 0
	//Error is a Generic Error
	Error = -1
	//ErrorBadParameters : Bad parameters passed
	ErrorBadParameters = -2
	//ErrorNoMemory : Out of memory
	ErrorNoMemory = -3
	//ErrorLibrary : Error in the camera driver
	ErrorLibrary = -4
	//ErrorUnknownPort : Unknown libgphoto2 port passed
	ErrorUnknownPort = -5
	//ErrorNotSupported : Functionality not supported
	ErrorNotSupported = -6
	//ErrorIO : Generic I/O error
	ErrorIO = -7
	//ErrorFixedLimitExceeded : Buffer overflow of internal structure
	ErrorFixedLimitExceeded = -8
	//ErrorTimeout : Operation timed out
	ErrorTimeout = -10
	//ErrorIOSupportedSerial : Serial ports not supported
	ErrorIOSupportedSerial = -20
	//ErrorIOSupportedUsb : USB ports not supported
	ErrorIOSupportedUsb = -21
	//ErrorIOInit : Error initialising I/O
	ErrorIOInit = -31
	//ErrorIORead : I/O during read
	ErrorIORead = -34
	//ErrorIOWrite : I/O during write
	ErrorIOWrite = -35
	//ErrorIOUpdate : I/O during update of settings
	ErrorIOUpdate = -37
	//ErrorIOSerialSpeed : Specified serial speed not possible.
	ErrorIOSerialSpeed = -41
	//ErrorIOUSBClearHalt : Error during USB Clear HALT
	ErrorIOUSBClearHalt = -51
	//ErrorIOUSBFind : Error when trying to find USB device
	ErrorIOUSBFind = -52
	//ErrorIOUSBClaim : Error when trying to claim the USB device
	ErrorIOUSBClaim = -53
	//ErrorIOLock : Error when trying to lock the device
	ErrorIOLock = -60
	//ErrorHal : Unspecified error when talking to HAL
	ErrorHal = -70

	//ErrorCorruptedData : This error is reported by camera drivers if corrupted data has been received that
	//can not be automatically handled. Normally, drivers will do everything possible to automatically recover from this error
	ErrorCorruptedData = -102
	//ErrorFileExists : An operation failed because a file existed. This error is reported for example when the user tries to create a file that already exists.
	ErrorFileExists = -103
	//ErrorModelNotFound : The specified model could not be found. This error is reported when the user specified a model that does not seem to be supported by any driver.
	ErrorModelNotFound = -105
	//ErrorDirectoryNotFound : The specified directory could not be found. This error is reported when the user specified a directory that is non-existent.
	ErrorDirectoryNotFound = -107
	//ErrorFileNotFound : The specified file could not be found. This error is reported when the user wants to access a file that is non-existent.
	ErrorFileNotFound = -108
	//ErrorDirectoryExists : The specified directory already exists. This error is reported for example when the user wants to create a directory that already exists.
	ErrorDirectoryExists = -109
	//ErrorCameraBusy : Camera I/O or a command is in progress.
	ErrorCameraBusy = -110
	//ErrorPathNotAbsolute : The specified path is not absolute. This error is reported when the user specifies paths that are not absolute, i.e. paths like "path/to/directory". As a rule of thumb, in gphoto2, there is nothing like relative paths.
	ErrorPathNotAbsolute = -111
	//ErrorCancel : A cancellation requestion by the frontend via progress callback and GP_CONTEXT_FEEDBACK_CANCEL was successful and the transfer has been aborted.
	ErrorCancel = -112
	//ErrorCameraError : The camera reported some kind of error. This can be either a photographic error, such as failure to autofocus, underexposure, or violating storage permission, anything else that stops the camera from performing the operation.
	ErrorCameraError = -113
	//ErrorosFailure : There was some sort of OS error in communicating with the camera,
	// e.g. lack of permission for an operation.
	ErrorosFailure = -114
	//ErrorNoSpace : There was not enough free space when uploading a file.
	ErrorNoSpace = -115

	ErrorReadOnly             = -201
	ErrorWidgetHasNoOptions   = -202
	ErrorWidgetIllegalOption  = -203
	ErrorWidgetNotImplemented = -204
)

libgphoto2 error codes. See http://www.gphoto.org/doc/api/gphoto2-result_8h.html for errors < -100 and http://www.gphoto.org/doc/api/gphoto2-port-result_8h.html for errors <= 0 >= -100

View Source
const (
	//LogError : Log message is an error infomation
	LogError = iota
	//LogVerbose : Log message is an verbose debug infomation
	LogVerbose
	//LogDebug : Log message is an debug infomation
	LogDebug
	//LogData : Log message is a data hex dump
	LogData
)

Log level

View Source
const (
	//FileTypePreview is a preview of an image
	FileTypePreview = iota
	//FileTypeNormal is regular normal data of a file
	FileTypeNormal
	//FileTypeRaw usually the same as FileTypeNormal for modern cameras ( left for compatibility purposes)
	FileTypeRaw
	//FileTypeAudio is a audio view of a file. Perhaps an embedded comment or similar
	FileTypeAudio
	//FileTypeExif is the  embedded EXIF data of an image
	FileTypeExif
	//FileTypeMetadata is the metadata of a file, like Metadata of files on MTP devices
	FileTypeMetadata
)

File types

View Source
const (
	SettingProgram      = "expprogram"
	SettingFocusMode    = "drivemode"
	SettingAperture     = "aperture"
	SettingFocalLength  = "focallength"
	Settingshutterspeed = "shutterspeed"
	SettingISO          = "iso"
	SettingWB           = "whitebalance"
	SettingQuality      = "imagequality"
	SettingSize         = "size"

	SettingLiveViewZoomRatio = "d1a3"
)

Variables

View Source
var CommonSettings = map[string]settingMapping{
	SettingProgram:      {/* contains filtered or unexported fields */},
	SettingFocusMode:    {/* contains filtered or unexported fields */},
	SettingAperture:     {/* contains filtered or unexported fields */},
	SettingFocalLength:  {/* contains filtered or unexported fields */},
	Settingshutterspeed: {/* contains filtered or unexported fields */},
	SettingISO:          {/* contains filtered or unexported fields */},
	SettingWB:           {/* contains filtered or unexported fields */},
	SettingQuality:      {/* contains filtered or unexported fields */},
	SettingSize:         {/* contains filtered or unexported fields */},
}

CommonSettings contains common settings, some have different names since camera makers don't believe in standards

Functions

func SetIdleFunc

func SetIdleFunc(ctx *Context, fun ContextIdleCallback)

func SetLoggerFunc

func SetLoggerFunc(fun *LogCallback)

Types

type Camera

type Camera struct {
	Ctx      *Context
	Settings *CameraWidget
	// contains filtered or unexported fields
}

Camera struct represents a camera connected to the computer

func NewCamera

func NewCamera(name string) (*Camera, error)

NewCamera tries to connect to a camera with name name, if name is empty it tries with the first connected camera. It returns a new Camera struct.

func (*Camera) CaptureDownload

func (c *Camera) CaptureDownload(buffer io.Writer, leaveOnCamera bool) error

CaptureDownload is for the lazy, capture and download in one call

func (*Camera) CaptureImage

func (c *Camera) CaptureImage() (*CameraFilePath, error)

CaptureImage captures image with current setings into camera's internal storage call CameraFilePath.DownloadImage to

func (*Camera) CapturePreview

func (c *Camera) CapturePreview(buffer io.Writer) error

CapturePreview captures image preview and saves it in provided buffer

func (*Camera) DeleteFile

func (camera *Camera) DeleteFile(path *CameraFilePath) error

DeleteFile tries to delete file from the camera, and returns error if it fails

func (Camera) Exit

func (c Camera) Exit() error

Exit Closes a connection to the camera and therefore gives other application the possibility to access the camera, too. It is recommended that you call this function when you currently don't need the camera. The camera will get reinitialized by gp_camera_init() automatically if you try to access the camera again.

func (Camera) Free

func (c Camera) Free() error

func (*Camera) GetSetting

func (c *Camera) GetSetting(name string) (*CameraWidget, error)

GetSetting returns a CameraWidget if it can be found among the camera settings

func (*Camera) ListFiles

func (camera *Camera) ListFiles() ([]CameraStorageInfo, error)

ListFiles returns a list of files and folders on the camera

func (*Camera) LoadWidgets

func (c *Camera) LoadWidgets() error

func (*Camera) Reset

func (c *Camera) Reset() error

ResetCamera resets the camera port, can be needed at times https://github.com/gphoto/gphoto2/blob/7a48ea37832bcd19e17b80afef2f7f2d426419f3/gphoto2/main.c#L1675

type CameraFilePath

type CameraFilePath struct {
	Name     string
	Folder   string
	Dir      bool
	Children []CameraFilePath
	// contains filtered or unexported fields
}

CameraFilePath is a path to a file or dir on the camera file system

func (*CameraFilePath) DownloadImage

func (file *CameraFilePath) DownloadImage(buffer io.Writer, leaveOnCamera bool) error

DownloadImage saves image pointed by path to the provided buffer. If leave on camera is set to false,the file will be deleted from the camera internal storage

type CameraList

type CameraList struct {
	Names []string
	Ports []string
}

func ListCameras

func ListCameras() (CameraList, error)

type CameraStorageInfo

type CameraStorageInfo struct {
	Description string
	Capacity    uint64
	Free        uint64
	FreeImages  uint64
	Children    []CameraFilePath
	// contains filtered or unexported fields
}

CamersStorageInfo is a struct describing one of the camera's storage spaces (SD or CF cards for example) Children is a directory tree present on the storage space

type CameraWidget

type CameraWidget struct {
	// contains filtered or unexported fields
}

func (CameraWidget) Children

func (w CameraWidget) Children() []*CameraWidget

func (*CameraWidget) Find

func (w *CameraWidget) Find(name string) *CameraWidget

func (CameraWidget) Get

func (w CameraWidget) Get() (interface{}, error)

func (CameraWidget) ID

func (w CameraWidget) ID() int

func (CameraWidget) Info

func (w CameraWidget) Info() string

func (CameraWidget) Label

func (w CameraWidget) Label() string

func (CameraWidget) Name

func (w CameraWidget) Name() string

func (CameraWidget) Options

func (w CameraWidget) Options() ([]string, error)

func (CameraWidget) Parent

func (w CameraWidget) Parent() *CameraWidget

func (CameraWidget) ReadOnly

func (w CameraWidget) ReadOnly() bool

func (CameraWidget) Set

func (w CameraWidget) Set(input interface{}) error

func (CameraWidget) SetInt

func (w CameraWidget) SetInt(input int) error

func (*CameraWidget) String

func (w *CameraWidget) String() string

func (CameraWidget) Type

func (w CameraWidget) Type() WidgetType

type Context

type Context struct {
	// contains filtered or unexported fields
}

func NewContext

func NewContext() (*Context, error)

type ContextIdleCallback

type ContextIdleCallback func(Context)

ContextIdleCallback should take Context, interface{} as its arguments

var IdleCallback ContextIdleCallback

IdleCallback is the libgphoto2 context idle function. It's being called when the library is waiting for the camera so small client side updates can be done when it's called

type ContextLogCallback

type ContextLogCallback func(string)

ContextLogCallback defineds a function used to log info associated to lobgphoto2 context

var ContextErrorCallback ContextLogCallback

ContextErrorCallback is the function logging error logs from libgphoto2 context. By default it logs everything to standard outout. You can assign your own method to this var

var ContextInfoCallback ContextLogCallback

ContextInfoCallback is the function logging info logs from libgphoto2 context. By default it logs everything to standard outout. You can assign your own method to this var

type GphotoError

type GphotoError struct {
	Code int
	// contains filtered or unexported fields
}

func (*GphotoError) Error

func (g *GphotoError) Error() string

type LogCallback

type LogCallback func(int, string, string)

LogCallback defines a generic libgphoto2 logging function

var LoggerCallback LogCallback

LoggerCallback is the libgphoto2 logging function. Currently there is no possibility to add multiple log function like it is possible in native C library implementation. Default implementation log everything to standard output with log level set to DEBUG

type WidgetType

type WidgetType string
const (
	//WidgetWindow is the toplevel configuration widget. It should likely contain multiple #WidgetSection entries.
	WidgetWindow WidgetType = "window"
	//WidgetSection : Section widget (think Tab)
	WidgetSection WidgetType = "section"
	//WidgetText : Text widget (string)
	WidgetText WidgetType = "text"
	//WidgetRange : Slider widget (float)
	WidgetRange WidgetType = "range"
	//WidgetToggle : Toggle widget (think check box) (int)
	WidgetToggle WidgetType = "toggle"
	//WidgetRadio : Radio button widget (string)
	WidgetRadio WidgetType = "radio"
	//WidgetMenu : Menu widget (same as RADIO) (string)
	WidgetMenu WidgetType = "menu"
	//WidgetButton : Button press widget ( CameraWidgetCallback )
	WidgetButton WidgetType = "button"
	//WidgetDate : Date entering widget (int)
	WidgetDate WidgetType = "date"
)

widget types

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL