jim800121chen c54f16fca0 Initial commit: visionA monorepo with local-tool subproject
local-tool/: visionA-local desktop app
- M1: Wails shell + Go server + Next.js UI + Mock mode (macOS dmg ready)
- M2: i18n (zh-TW/en) + Settings 4-tab refactor
- M3: Embedded Python 3.12 runtime (python-build-standalone) + KneronPLUS wheels
- M4: Windows Inno Setup script (build on Windows runner)
- M5: Linux AppImage script + udev rule (build on Linux runner)
- M6: ffmpeg (GPL, pending legal review) + yt-dlp bundled
- Lifecycle: watchServer health check, fatal native dialog,
            Wails IPC raise endpoint, stale process cleanup

.autoflow/: full PRD / Design Spec / Architecture / Testing docs
            (4 rounds tri-party discussion + cross review)
.github/workflows/: macOS / Windows / Linux build CI

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-11 22:10:38 +08:00

83 lines
1.8 KiB
Go

package camera
import (
"bytes"
"fmt"
"image"
"image/jpeg"
"image/png"
"os"
"path/filepath"
"strings"
)
// ImageSource provides a single JPEG frame from an uploaded image file.
// ReadFrame() returns the frame once, then subsequent calls return an error
// to signal the pipeline that the source is exhausted.
type ImageSource struct {
jpegData []byte
width int
height int
filePath string
done bool
}
// NewImageSource reads an image file (JPG or PNG) and converts to JPEG bytes.
func NewImageSource(filePath string) (*ImageSource, error) {
f, err := os.Open(filePath)
if err != nil {
return nil, fmt.Errorf("failed to open image: %w", err)
}
defer f.Close()
ext := strings.ToLower(filepath.Ext(filePath))
var img image.Image
switch ext {
case ".jpg", ".jpeg":
img, err = jpeg.Decode(f)
case ".png":
img, err = png.Decode(f)
default:
return nil, fmt.Errorf("unsupported image format: %s", ext)
}
if err != nil {
return nil, fmt.Errorf("failed to decode image: %w", err)
}
bounds := img.Bounds()
var buf bytes.Buffer
if err := jpeg.Encode(&buf, img, &jpeg.Options{Quality: 90}); err != nil {
return nil, fmt.Errorf("failed to encode JPEG: %w", err)
}
return &ImageSource{
jpegData: buf.Bytes(),
width: bounds.Dx(),
height: bounds.Dy(),
filePath: filePath,
}, nil
}
func (s *ImageSource) ReadFrame() ([]byte, error) {
if !s.done {
s.done = true
}
// Always return the same frame so the MJPEG streamer can serve it
// to clients that connect at any time.
return s.jpegData, nil
}
func (s *ImageSource) Close() error {
return os.Remove(s.filePath)
}
// Dimensions returns the image width and height.
func (s *ImageSource) Dimensions() (int, int) {
return s.width, s.height
}
// IsDone returns whether the single frame has been consumed.
func (s *ImageSource) IsDone() bool {
return s.done
}