// pkg/screen/screen.go
packagescreenimport("bytes""image""image/png""io""sync""github.com/kbinani/screenshot")constdefaultBufferSize=1<<24// 16MB
typeScreeninterface{Capture()errorReader()io.Reader}typescreenstruct{musync.Mutexboundimage.Rectanglebuf[]byte}// NewScreen returns a new screen
funcNewScreen(boundimage.Rectangle)Screen{return&screen{bound:bound,buf:make([]byte,0,defaultBufferSize),}}// Capture captures the screen
func(s*screen)Capture()error{s.mu.Lock()defers.mu.Unlock()img,err:=screenshot.CaptureRect(s.bound)iferr!=nil{returnerr}s.buf=s.buf[:0]buf:=bytes.NewBuffer(s.buf)iferr:=png.Encode(buf,img);err!=nil{returnerr}s.buf=buf.Bytes()returnnil}// Reader returns a reader for the cached image
func(s*screen)Reader()io.Reader{s.mu.Lock()defers.mu.Unlock()returnbytes.NewReader(s.buf)}// MaxBound returns the maximum bounds of all displays
funcMaxBound()image.Rectangle{minX,minY,maxX,maxY:=0,0,0,0n:=screenshot.NumActiveDisplays()fori:=0;i<n;i++{bounds:=screenshot.GetDisplayBounds(i)ifbounds.Min.X<minX{minX=bounds.Min.X}ifbounds.Min.Y<minY{minY=bounds.Min.Y}ifbounds.Max.X>maxX{maxX=bounds.Max.X}ifbounds.Max.Y>maxY{maxY=bounds.Max.Y}}returnimage.Rect(minX,minY,maxX,maxY)}
// pkg/handler/screen.go
packagehandlerimport("io""net/http""github.com/LoveSnowEx/screen-cropper/pkg/screen")// ScreenHandler is a http.Handler that serves the screen image.
typeScreenHandlerstruct{screenscreen.Screen}// NewScreenHandler creates a new ScreenHandler.
funcNewScreenHandler(screenscreen.Screen)http.Handler{return&ScreenHandler{screen:screen}}// ServeHTTP serves the screen image.
func(s*ScreenHandler)ServeHTTP(whttp.ResponseWriter,r*http.Request){ifr.URL.Path!="/screen.png"{http.NotFound(w,r)return}w.Header().Set("Content-Type","image/png")if_,err:=io.Copy(w,s.screen.Reader());err!=nil{http.Error(w,err.Error(),http.StatusInternalServerError)}}
// Greet returns a greeting for the given name
func(a*App)Greet(namestring)string{goa.screen.Capture()returnfmt.Sprintf("Hello %s, It's show time!",name)}
functiongreet():void{Greet(name).then((result)=>(resultText=result))getImage()}constgetImage=async()=>{try{constresponse=awaitfetch("screen.png")if(response.ok){constblob=awaitresponse.blob()constimageUrl=URL.createObjectURL(blob)constimageElement=document.getElementById("logo")asHTMLImageElementimageElement.src=imageUrl}else{console.error("failed to fetch image")}}catch(error){console.error("error:",error)}}