Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
04b84e188a | ||
|
|
2e971d5f8d |
@@ -123,8 +123,8 @@ enum LoginItem {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func renderTestImage(_ text: String, fontSize: CGFloat = 56) -> CGImage? {
|
func renderTestImage(_ text: String, fontSize: CGFloat = 56, width: CGFloat = 1900, height: CGFloat = 220) -> CGImage? {
|
||||||
let size = NSSize(width: 1900, height: 220)
|
let size = NSSize(width: width, height: height)
|
||||||
guard let rep = NSBitmapImageRep(
|
guard let rep = NSBitmapImageRep(
|
||||||
bitmapDataPlanes: nil, pixelsWide: Int(size.width), pixelsHigh: Int(size.height),
|
bitmapDataPlanes: nil, pixelsWide: Int(size.width), pixelsHigh: Int(size.height),
|
||||||
bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false,
|
bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false,
|
||||||
@@ -138,15 +138,20 @@ func renderTestImage(_ text: String, fontSize: CGFloat = 56) -> CGImage? {
|
|||||||
.font: NSFont.systemFont(ofSize: fontSize),
|
.font: NSFont.systemFont(ofSize: fontSize),
|
||||||
.foregroundColor: NSColor.black,
|
.foregroundColor: NSColor.black,
|
||||||
]
|
]
|
||||||
text.draw(at: NSPoint(x: 60, y: 75), withAttributes: attrs)
|
// Centre the text vertically so tiny canvases still contain it.
|
||||||
|
let textHeight = ("Ag" as NSString).size(withAttributes: attrs).height
|
||||||
|
let y = max(4, (size.height - textHeight) / 2)
|
||||||
|
text.draw(at: NSPoint(x: 20, y: y), withAttributes: attrs)
|
||||||
NSGraphicsContext.restoreGraphicsState()
|
NSGraphicsContext.restoreGraphicsState()
|
||||||
return rep.cgImage
|
return rep.cgImage
|
||||||
}
|
}
|
||||||
|
|
||||||
func runOCRSelfTest() {
|
func runOCRSelfTest() {
|
||||||
let cases: [(expected: String, contains: String)] = [
|
let cases: [(expected: String, contains: String, fontSize: CGFloat, width: CGFloat, height: CGFloat)] = [
|
||||||
("Kapture OCR test 1234, hello world", "Kapture OCR test 1234"),
|
("Kapture OCR test 1234, hello world", "Kapture OCR test 1234", 56, 1900, 220),
|
||||||
("你好世界,这是一个测试", "你好世界"),
|
("你好世界,这是一个测试", "你好世界", 56, 1900, 220),
|
||||||
|
// Tiny text: needs the upscale retry to read anything at all.
|
||||||
|
("会话导出并发冲突报错 export fail", "导出", 7, 320, 40),
|
||||||
]
|
]
|
||||||
var index = 0
|
var index = 0
|
||||||
|
|
||||||
@@ -154,9 +159,9 @@ func runOCRSelfTest() {
|
|||||||
guard index < cases.count else { exit(0) }
|
guard index < cases.count else { exit(0) }
|
||||||
let current = cases[index]
|
let current = cases[index]
|
||||||
index += 1
|
index += 1
|
||||||
guard let image = renderTestImage(current.expected) else { exit(1) }
|
guard let image = renderTestImage(current.expected, fontSize: current.fontSize, width: current.width, height: current.height) else { exit(1) }
|
||||||
OCRService.shared.recognize(image) { recognized in
|
OCRService.shared.recognize(image) { recognized in
|
||||||
print("case: \(current.expected)")
|
print("case: \(current.expected) @\(Int(current.fontSize))pt")
|
||||||
print("got: \(recognized)")
|
print("got: \(recognized)")
|
||||||
let ok = recognized.contains(current.contains)
|
let ok = recognized.contains(current.contains)
|
||||||
print(ok ? "PASS" : "FAIL")
|
print(ok ? "PASS" : "FAIL")
|
||||||
|
|||||||
@@ -8,6 +8,25 @@ final class OCRService {
|
|||||||
|
|
||||||
func recognize(_ cgImage: CGImage, completion: @escaping (String) -> Void) {
|
func recognize(_ cgImage: CGImage, completion: @escaping (String) -> Void) {
|
||||||
queue.async {
|
queue.async {
|
||||||
|
let text = Self.perform(cgImage)
|
||||||
|
// Tiny text (small UI labels) often reads as nothing at native
|
||||||
|
// size; retry upscaled and keep whichever pass read more.
|
||||||
|
guard text.count < 3 else {
|
||||||
|
completion(text)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var best = text
|
||||||
|
for factor in [CGFloat(2), CGFloat(3)] {
|
||||||
|
guard let upscaled = Self.upscaled(cgImage, factor: factor) else { continue }
|
||||||
|
let candidate = Self.perform(upscaled)
|
||||||
|
if candidate.count > best.count { best = candidate }
|
||||||
|
if best.count >= 3 { break }
|
||||||
|
}
|
||||||
|
completion(best)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static func perform(_ cgImage: CGImage) -> String {
|
||||||
let request = VNRecognizeTextRequest()
|
let request = VNRecognizeTextRequest()
|
||||||
request.recognitionLevel = .accurate
|
request.recognitionLevel = .accurate
|
||||||
request.usesLanguageCorrection = true
|
request.usesLanguageCorrection = true
|
||||||
@@ -22,8 +41,7 @@ final class OCRService {
|
|||||||
do {
|
do {
|
||||||
try handler.perform([request])
|
try handler.perform([request])
|
||||||
} catch {
|
} catch {
|
||||||
completion("")
|
return ""
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let observations = request.results ?? []
|
let observations = request.results ?? []
|
||||||
@@ -35,7 +53,21 @@ final class OCRService {
|
|||||||
return a.boundingBox.minX < b.boundingBox.minX
|
return a.boundingBox.minX < b.boundingBox.minX
|
||||||
}
|
}
|
||||||
.compactMap { $0.topCandidates(1).first?.string }
|
.compactMap { $0.topCandidates(1).first?.string }
|
||||||
completion(lines.joined(separator: "\n"))
|
return lines.joined(separator: "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static func upscaled(_ cgImage: CGImage, factor: CGFloat) -> CGImage? {
|
||||||
|
let width = Int(CGFloat(cgImage.width) * factor)
|
||||||
|
let height = Int(CGFloat(cgImage.height) * factor)
|
||||||
|
guard width > 0, height > 0, width <= 20000, height <= 20000 else { return nil }
|
||||||
|
guard let ctx = CGContext(
|
||||||
|
data: nil, width: width, height: height,
|
||||||
|
bitsPerComponent: 8, bytesPerRow: 0,
|
||||||
|
space: CGColorSpaceCreateDeviceRGB(),
|
||||||
|
bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue
|
||||||
|
) else { return nil }
|
||||||
|
ctx.interpolationQuality = .high
|
||||||
|
ctx.draw(cgImage, in: CGRect(x: 0, y: 0, width: CGFloat(width), height: CGFloat(height)))
|
||||||
|
return ctx.makeImage()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -97,7 +97,9 @@ final class OverlayView: NSView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Esc hint — the only way out once clicks are consumed by the tap.
|
// Esc hint — the only way out once clicks are consumed by the tap.
|
||||||
let hint = "Drag to select · hover + click a window to capture it · Esc to cancel"
|
let hint = frozenImage != nil
|
||||||
|
? "Frozen screen · drag to select · Esc to cancel"
|
||||||
|
: "Drag to select · hover + click a window to capture it · Esc to cancel"
|
||||||
let hintAttrs: [NSAttributedString.Key: Any] = [
|
let hintAttrs: [NSAttributedString.Key: Any] = [
|
||||||
.font: NSFont.monospacedSystemFont(ofSize: 12, weight: .medium),
|
.font: NSFont.monospacedSystemFont(ofSize: 12, weight: .medium),
|
||||||
.foregroundColor: NSColor.white.withAlphaComponent(0.85),
|
.foregroundColor: NSColor.white.withAlphaComponent(0.85),
|
||||||
@@ -115,22 +117,17 @@ final class OverlayView: NSView {
|
|||||||
|
|
||||||
guard let controller else { return }
|
guard let controller else { return }
|
||||||
|
|
||||||
// Frozen mode: no window highlight; the marquee is cut straight out
|
// Frozen mode: same look as live mode — a dark veil over everything,
|
||||||
// of the still image so it shows the frozen pixels.
|
// with the selection showing the frozen pixels undimmed.
|
||||||
if frozenImage != nil {
|
if let frozenImage {
|
||||||
|
NSColor.black.withAlphaComponent(0.35).setFill()
|
||||||
|
NSBezierPath(rect: bounds).fill()
|
||||||
|
|
||||||
if let sel = controller.marqueeRect, sel.width > 0, sel.height > 0 {
|
if let sel = controller.marqueeRect, sel.width > 0, sel.height > 0 {
|
||||||
let vf = viewRect(sel)
|
let vf = viewRect(sel)
|
||||||
// Dim everything except the selection (even-odd fill).
|
|
||||||
let maskPath = CGMutablePath()
|
|
||||||
maskPath.addRect(bounds)
|
|
||||||
maskPath.addRect(vf)
|
|
||||||
NSGraphicsContext.current?.saveGraphicsState()
|
NSGraphicsContext.current?.saveGraphicsState()
|
||||||
let ctx = NSGraphicsContext.current?.cgContext
|
NSBezierPath(rect: vf).addClip()
|
||||||
ctx?.addPath(maskPath)
|
drawFrozen(frozenImage)
|
||||||
ctx?.fillPath(using: .evenOdd)
|
|
||||||
NSColor.black.withAlphaComponent(0.35).setFill()
|
|
||||||
ctx?.addPath(maskPath)
|
|
||||||
ctx?.fillPath(using: .evenOdd)
|
|
||||||
NSGraphicsContext.current?.restoreGraphicsState()
|
NSGraphicsContext.current?.restoreGraphicsState()
|
||||||
|
|
||||||
let border = NSBezierPath(rect: vf)
|
let border = NSBezierPath(rect: vf)
|
||||||
|
|||||||
+2
-2
@@ -108,9 +108,9 @@ cat > "$APP/Contents/Info.plist" <<'PLIST'
|
|||||||
<key>CFBundleIconFile</key>
|
<key>CFBundleIconFile</key>
|
||||||
<string>Icon</string>
|
<string>Icon</string>
|
||||||
<key>CFBundleShortVersionString</key>
|
<key>CFBundleShortVersionString</key>
|
||||||
<string>1.10.0</string>
|
<string>1.10.2</string>
|
||||||
<key>CFBundleVersion</key>
|
<key>CFBundleVersion</key>
|
||||||
<string>100</string>
|
<string>102</string>
|
||||||
<key>LSUIElement</key>
|
<key>LSUIElement</key>
|
||||||
<true/>
|
<true/>
|
||||||
<key>NSHighResolutionCapable</key>
|
<key>NSHighResolutionCapable</key>
|
||||||
|
|||||||
Reference in New Issue
Block a user