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 ¶
- Variables
- func RequestPermission(ctx context.Context, a *giode.App) (bool, error)
- func Supported() bool
- type Camera
- func (c *Camera) Capture(ctx context.Context) ([]byte, error)
- func (c *Camera) Close()
- func (c *Camera) Focus(p f32.Point) error
- func (c *Camera) HasTorch() bool
- func (c *Camera) Preview(opts PreviewOptions) giode.Element
- func (c *Camera) Size() image.Point
- func (c *Camera) Torch(on bool) error
- func (c *Camera) TorchOn() bool
- type Device
- type Facing
- type Frame
- type Options
- type PreviewOptions
Constants ¶
This section is empty.
Variables ¶
var ErrClosed = errors.New("camera: closed")
ErrClosed is returned by a closed Camera's methods.
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 ¶
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.
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 ¶
Open starts the camera, asking for permission first if needed. Close it when the view showing it goes away.
func (*Camera) Capture ¶
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 ¶
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) 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 ¶
Size returns the preview frames' size, upright: zero until the first frame arrives.
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.