在不同的屏幕尺寸上使用屏幕坐标不正确裁剪 NSImage

时间:2021-05-07 22:00:40

标签: macos core-graphics cgrect quartz

我正在尝试复制 macOS 的屏幕截图功能,在屏幕上拖动选择以提供用于裁剪图像的坐标。我在我的台式机 Mac (2560x1600) 上运行良好,但在我的笔记本电脑上测试 (2016 rMBP 15", 2880x1800),裁剪后的图像完全错误。我不明白为什么我会在我的桌面上得到正确的结果,但不是在我的笔记本电脑上。我认为这与 Quarts 坐标与 Cocoa 坐标不同有关,看看在笔记本电脑上如何,结果图像似乎在 Y 轴上翻转了坐标。

这是我用来生成裁剪 CGRect 的代码:

# Segment used to draw the CAShapeLayer:
 private func handleDragging(_ event: NSEvent) {
        let mouseLoc = event.locationInWindow

        if let point = self.startPoint,
            let layer = self.shapeLayer {
            let path = CGMutablePath()
            path.move(to: point)
            path.addLine(to: NSPoint(x: self.startPoint.x, y: mouseLoc.y))
            path.addLine(to: mouseLoc)
            path.addLine(to: NSPoint(x: mouseLoc.x, y: self.startPoint.y))
            path.closeSubpath()
            layer.path = path
            self.selectionRect = path.boundingBox
        }
    }

    private func startDragging(_ event: NSEvent) {
        if let window = self.window,
            let contentView = window.contentView,
            let layer = contentView.layer,
            !self.isDragging {

            self.isDragging = true
            self.startPoint = window.mouseLocationOutsideOfEventStream

            shapeLayer = CAShapeLayer()
            shapeLayer.lineWidth = 1.0
            shapeLayer.fillColor = NSColor.white.withAlphaComponent(0.5).cgColor
            shapeLayer.strokeColor = NSColor.systemGray.cgColor
            layer.addSublayer(shapeLayer)
        }
    }

然后这是我使用 CGRect 实际生成屏幕截图和裁剪的代码:

 public func processResults(_ rect: CGRect) {
        if let windowID = self.globalWindow?.windowNumber,
            let screen = self.getScreenWithMouse(), rect.width > 5 && rect.height > 5 {
            self.delegate?.processingResults()

            let cgScreenshot = CGWindowListCreateImage(screen.frame, .optionOnScreenBelowWindow, CGWindowID(windowID), .bestResolution)


            var rect2 = rect
            rect2.origin.y = NSMaxY(self.getScreenWithMouse()!.frame) - NSMaxY(rect);
            
            if let croppedCGScreenshot = cgScreenshot?.cropping(to: rect2) {


                let rep = NSBitmapImageRep(cgImage: croppedCGScreenshot)
                let image = NSImage()
                image.addRepresentation(rep)

                self.showPreviewWindow(image: image)

                let requests = [self.getTextRecognitionRequest()]
                let imageRequestHandler = VNImageRequestHandler(cgImage: croppedCGScreenshot, orientation: .up, options: [:])

                DispatchQueue.global(qos: .userInitiated).async {
                    do {
                        try imageRequestHandler.perform(requests)
                    } catch let error {
                        print("Error: \(error)")
                    }
                }
                
                DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
                    self.hidePreviewWindow()
                }
            }
        }
        
        self.globalWindow = nil
    }

1 个答案:

答案 0 :(得分:0)

在我提出这个问题后不到 15 分钟,我又尝试了一件事情,它奏效了!

相关片段:

var correctedRect = rect

// Set the Y origin properly (counteracting the flipped Y-axis)
correctedRect.origin.y = screen.frame.height - rect.origin.y - rect.height;

// Checks if we're on another screen
if (screen.frame.origin.y < 0) {
    correctedRect.origin.y = correctedRect.origin.y - screen.frame.origin.y
}


// Finally, correct the x origin (if we're on another screen, the origin will be larger than zero)
correctedRect.origin.x = correctedRect.origin.x + screen.frame.origin.x

// Generate the screenshot inside the requested rect
let cgScreenshot = CGWindowListCreateImage(correctedRect, .optionOnScreenBelowWindow, CGWindowID(windowID), .bestResolution)