This commit is contained in:
@@ -47,9 +47,54 @@ static unsigned char* CaptureFullDesktop(int* outLen, int quality) {
|
||||
}
|
||||
|
||||
@autoreleasepool {
|
||||
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithCGImage:image];
|
||||
// Calculate new size maintaining aspect ratio (limiting to max 1024x768)
|
||||
CGFloat originalWidth = CGImageGetWidth(image);
|
||||
CGFloat originalHeight = CGImageGetHeight(image);
|
||||
CGFloat maxWidth = 1024.0f;
|
||||
CGFloat maxHeight = 768.0f;
|
||||
CGFloat ratio = 1.0f;
|
||||
if (originalWidth > maxWidth || originalHeight > maxHeight) {
|
||||
CGFloat ratioW = maxWidth / originalWidth;
|
||||
CGFloat ratioH = maxHeight / originalHeight;
|
||||
ratio = ratioW < ratioH ? ratioW : ratioH;
|
||||
}
|
||||
size_t newWidth = (size_t)(originalWidth * ratio);
|
||||
size_t newHeight = (size_t)(originalHeight * ratio);
|
||||
if (newWidth < 1) newWidth = 1;
|
||||
if (newHeight < 1) newHeight = 1;
|
||||
|
||||
// Create bitmap context
|
||||
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
|
||||
CGContextRef context = CGBitmapContextCreate(NULL,
|
||||
newWidth,
|
||||
newHeight,
|
||||
8,
|
||||
newWidth * 4,
|
||||
colorSpace,
|
||||
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
|
||||
CGColorSpaceRelease(colorSpace);
|
||||
if (!context) {
|
||||
CGImageRelease(image);
|
||||
*outLen = 0;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Draw image into context to resize
|
||||
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
|
||||
CGContextDrawImage(context, CGRectMake(0, 0, newWidth, newHeight), image);
|
||||
CGImageRelease(image);
|
||||
|
||||
// Get resized image
|
||||
CGImageRef resizedImage = CGBitmapContextCreateImage(context);
|
||||
CGContextRelease(context);
|
||||
if (!resizedImage) {
|
||||
*outLen = 0;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithCGImage:resizedImage];
|
||||
CGImageRelease(resizedImage);
|
||||
|
||||
if (!rep) {
|
||||
*outLen = 0;
|
||||
return NULL;
|
||||
|
||||
@@ -6,11 +6,35 @@ import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/jpeg"
|
||||
|
||||
"github.com/kbinani/screenshot"
|
||||
)
|
||||
|
||||
// resizeRGBA resizes a *image.RGBA image using Nearest-Neighbor scaling for maximum performance.
|
||||
func resizeRGBA(src *image.RGBA, width, height int) *image.RGBA {
|
||||
dst := image.NewRGBA(image.Rect(0, 0, width, height))
|
||||
srcBounds := src.Bounds()
|
||||
dx := srcBounds.Dx()
|
||||
dy := srcBounds.Dy()
|
||||
minX := srcBounds.Min.X
|
||||
minY := srcBounds.Min.Y
|
||||
|
||||
for y := 0; y < height; y++ {
|
||||
srcY := minY + (y * dy) / height
|
||||
for x := 0; x < width; x++ {
|
||||
srcX := minX + (x * dx) / width
|
||||
|
||||
srcOffset := src.PixOffset(srcX, srcY)
|
||||
dstOffset := dst.PixOffset(x, y)
|
||||
|
||||
copy(dst.Pix[dstOffset:dstOffset+4], src.Pix[srcOffset:srcOffset+4])
|
||||
}
|
||||
}
|
||||
return dst
|
||||
}
|
||||
|
||||
// CaptureScreen chụp màn hình chính và trả về chuỗi Base64 dạng "data:image/jpeg;base64,..."
|
||||
// Non-darwin: dùng kbinani/screenshot
|
||||
func CaptureScreen() (string, error) {
|
||||
@@ -26,9 +50,36 @@ func CaptureScreen() (string, error) {
|
||||
return "", fmt.Errorf("failed to capture screen: %w", err)
|
||||
}
|
||||
|
||||
// Tối ưu hóa kích thước ảnh giống dự án Raia:
|
||||
// Giới hạn chiều rộng tối đa là 1024px và chiều cao tối đa là 768px để giảm dung lượng mạng.
|
||||
dx := bounds.Dx()
|
||||
dy := bounds.Dy()
|
||||
|
||||
maxWidth := 1024
|
||||
maxHeight := 768
|
||||
|
||||
newWidth := dx
|
||||
newHeight := dy
|
||||
|
||||
if dx > maxWidth || dy > maxHeight {
|
||||
ratioWidth := float64(maxWidth) / float64(dx)
|
||||
ratioHeight := float64(maxHeight) / float64(dy)
|
||||
ratio := ratioWidth
|
||||
if ratioHeight < ratioWidth {
|
||||
ratio = ratioHeight
|
||||
}
|
||||
newWidth = int(float64(dx) * ratio)
|
||||
newHeight = int(float64(dy) * ratio)
|
||||
}
|
||||
|
||||
var finalImg image.Image = img
|
||||
if newWidth != dx || newHeight != dy {
|
||||
finalImg = resizeRGBA(img, newWidth, newHeight)
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
// Nén chất lượng JPEG khoảng 50% để truyền tải mượt mà qua mạng
|
||||
err = jpeg.Encode(&buf, img, &jpeg.Options{Quality: 50})
|
||||
err = jpeg.Encode(&buf, finalImg, &jpeg.Options{Quality: 50})
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to encode jpeg: %w", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user