camera

package
v1.0.31-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 24 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package camera shows a live camera preview inside a Giode view and takes full-resolution pictures: a document scanner's viewfinder, a QR scanner, a photo booth.

cam, err := camera.Open(ctx, app, camera.Options{
	OnFrame: func(f camera.Frame) { quad = findPage(f.Image) },
})
...
view := cam.Preview(camera.PreviewOptions{
	Fill:    true,
	Overlay: func(gtx layout.Context, toView func(f32.Point) f32.Point) { drawQuad(gtx, quad, toView) },
})
...
jpeg, err := cam.Capture(ctx)

Frames arrive upright, whichever way the phone is held. The preview is drawn by Giode itself, so anything can be drawn over it, and OnFrame receives small grey frames for analysis (edge detection, barcode reading) on its own goroutine.

Android (Camera2), iOS and macOS (AVFoundation), Windows (Media Foundation) and Linux (Video4Linux) are supported; elsewhere Open returns giode.ErrNotSupported. The app needs the camera permission: on Android the package adds it to the manifest; on iOS set ios.usage.camera in giode.toml (NSCameraUsageDescription).

Index

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("camera: closed")

ErrClosed is returned by a closed Camera's methods.

View Source
var ErrPermissionDenied = errors.New("camera: permission denied")

ErrPermissionDenied is returned by Open when the user didn't allow the app to use the camera.

Functions

func RequestPermission

func RequestPermission(ctx context.Context, a *giode.App) (bool, error)

RequestPermission asks the user to let the app use the camera, if it hasn't yet, and reports whether it may. Open asks too; call this to ask at a moment of your choosing.

func Supported

func Supported() bool

Supported reports whether this platform has a camera Open can use.

Types

type Camera

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

Camera is an open camera. Its methods may be called from any goroutine.

func Open

func Open(ctx context.Context, a *giode.App, opts Options) (*Camera, error)

Open starts the camera, asking for permission first if needed. Close it when the view showing it goes away.

func (*Camera) Capture

func (c *Camera) Capture(ctx context.Context) ([]byte, error)

Capture takes a picture at the camera's full resolution and returns it as an upright JPEG. The preview keeps running.

func (*Camera) Close

func (c *Camera) Close()

Close stops the camera. It's safe to call more than once.

func (*Camera) Focus

func (c *Camera) Focus(p f32.Point) error

Focus focuses (and meters exposure) at p, a point in the upright frame from (0, 0) at the top left to (1, 1) at the bottom right; the preview's tap-to-focus calls it. Cameras that can't focus on a point ignore it.

func (*Camera) HasTorch

func (c *Camera) HasTorch() bool

HasTorch reports whether the camera has a light it can keep on.

func (*Camera) Preview

func (c *Camera) Preview(opts PreviewOptions) giode.Element

Preview returns an element showing the live picture. It fills the space it's given.

func (*Camera) Size

func (c *Camera) Size() image.Point

Size returns the preview frames' size, upright: zero until the first frame arrives.

func (*Camera) Torch

func (c *Camera) Torch(on bool) error

Torch turns the camera's light on or off.

func (*Camera) TorchOn

func (c *Camera) TorchOn() bool

TorchOn reports whether the light is on.

type Device

type Device struct {
	ID, Name string
}

Device is a camera, on computers.

func Devices

func Devices(ctx context.Context) ([]Device, error)

Devices lists the cameras on computers (Windows, Linux, macOS); on phones, choose by Facing instead.

type Facing

type Facing int

Facing selects the camera.

const (
	// Back is the main camera, facing away from the user.
	Back Facing = iota
	// Front faces the user; its preview is mirrored, as a mirror is.
	Front
)

type Frame

type Frame struct {
	// Image is the frame's brightness, upright, AnalysisSize on its long
	// side. It belongs to the callback: keep it, it isn't reused.
	Image *image.Gray
	// RGBA is the color picture at the same size, as the preview shows
	// it (the front camera mirrored), with Options.Color; nil otherwise.
	// It belongs to the callback too.
	RGBA *image.RGBA
	// Time is when the frame arrived.
	Time time.Time
}

Frame is an analysis frame.

type Options

type Options struct {
	// Facing selects the back (default) or front camera on phones.
	Facing Facing
	// Device selects a camera by its ID from Devices, on computers; the
	// first one when empty.
	Device string
	// AnalysisSize is the long side, in pixels, of the frames OnFrame
	// receives: 640 by default.
	AnalysisSize int
	// AnalysisRate caps how many frames per second OnFrame receives: 15
	// by default. Frames arriving while OnFrame still runs are dropped,
	// so a slow analysis never builds a backlog.
	AnalysisRate int
	// OnFrame receives analysis frames on its own goroutine; nil for
	// none. Use App.Post to hand results to the UI.
	OnFrame func(Frame)
	// Color adds the color picture to analysis frames (Frame.RGBA), for
	// encoding into a video call or a recording.
	Color bool
	// OnError receives errors the camera reports while running (another
	// app took it, the hardware failed), from any goroutine. They're
	// logged when it's nil.
	OnError func(error)
}

Options configure Open.

type PreviewOptions

type PreviewOptions struct {
	// Fill crops the frame to fill the element, as camera apps do;
	// otherwise the whole frame shows, letterboxed.
	Fill bool
	// NoTapFocus turns off focusing where the user taps.
	NoTapFocus bool
	// Overlay draws over the preview: toView converts a point in the
	// frame, from (0, 0) to (1, 1), to the element's pixels, so a page
	// outline found in an analysis frame lands on the page.
	Overlay func(gtx layout.Context, toView func(f32.Point) f32.Point)
}

PreviewOptions configure Preview.

Jump to

Keyboard shortcuts

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