Skip to content

Commit

Permalink
feat: adjusted image size/format, interval
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyoptimist committed Aug 4, 2024
1 parent de4adeb commit 8a3994d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func main() {
// Capture screen if requested,
// serve the screenshot over HTTP
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/png")
w.Header().Set("Content-Type", "image/jpeg")

if err := capturer.CaptureMergedScreen(); err != nil {
log.Printf("Error capturing the merged screen: %v", err)
Expand Down
18 changes: 13 additions & 5 deletions internal/infrastructure/capturer/capturer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,30 @@ package capturer
import (
"errors"
"image"
"image/png"
"image/jpeg"
"os"
"path/filepath"

"github.com/kbinani/screenshot"
)

// save *image.RGBA to filePath with PNG format.
// save *image.RGBA to filePath
func save(img *image.RGBA, filePath string) error {
file, err := os.Create(filePath)
if err != nil {
return err
}
defer file.Close()

err = png.Encode(file, img)
// var imageQuality = 100
// fileSizeEstimated := len(img.Pix) * 4 // Each pixel is 4 bytes (RGBA)
// fileSizeLimit := 500 * 1024 // Target file size in bytes

// if fileSizeEstimated > fileSizeLimit {
// imageQuality = 100 * (fileSizeLimit / fileSizeEstimated)
// }

err = jpeg.Encode(file, img, nil)
if err != nil {
return err
}
Expand All @@ -27,7 +35,7 @@ func save(img *image.RGBA, filePath string) error {
}

func GetMergedScreenFilePath() string {
fileName := "merged-screen.png"
fileName := "merged-screen.jpeg"

return filepath.Join(os.TempDir(), fileName)
}
Expand All @@ -49,7 +57,7 @@ func CaptureMergedScreen() error {
if err != nil {
return err
}
fileName := fmt.Sprintf("%d_%dx%d.png", i, bounds.Dx(), bounds.Dy())
fileName := fmt.Sprintf("%d_%dx%d.jpeg", i, bounds.Dx(), bounds.Dy())
save(img, fileName)
fmt.Printf("#%d : %v \"%s\"\n", i, bounds, fileName)
Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/capturer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func RequestScreenshot(addr, outDirPath string) {

// Generate a unique filename with the current timestamp
filename := fmt.Sprintf(
"screenshot_%d-%02d-%02dT%02d-%02d-%02d.png",
"screenshot_%d-%02d-%02dT%02d-%02d-%02d.jpeg",
time.Now().Year(), time.Now().Month(), time.Now().Day(),
time.Now().Hour(), time.Now().Minute(), time.Now().Second(),
)
Expand Down

0 comments on commit 8a3994d

Please sign in to comment.