Skip to content

Commit

Permalink
Console: Don't use GUI settings for OCR with --noprofile
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanfish committed Jan 12, 2024
1 parent aa62214 commit 6b17c5e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
51 changes: 49 additions & 2 deletions NAPS2.Lib.Tests/Automation/CommandLineIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,25 @@ await _automationHelper.RunCommand(
AssertRecoveryCleanedUp();
}

[Fact]
public async Task ScanWithNoOcr()
{
SetUpFakeOcr(new()
{
{ LoadImage(ImageResources.ocr_test), "ADVERTISEMENT." }
});
var path = $"{FolderPath}/test.pdf";
await _automationHelper.RunCommand(
new AutomatedScanningOptions
{
OutputPath = path,
Verbose = true
},
ImageResources.ocr_test);
PdfAsserts.AssertDoesNotContainText("ADVERTISEMENT.", path);
AssertRecoveryCleanedUp();
}

[Fact]
public async Task ScanWithOcrLang()
{
Expand Down Expand Up @@ -132,6 +151,30 @@ await _automationHelper.WithContainer(container =>
AssertRecoveryCleanedUp();
}

[Fact]
public async Task ScanWithOcrSettingsFromGui()
{
SetUpFakeOcr(new()
{
{ LoadImage(ImageResources.ocr_test), "ADVERTISEMENT." }
});
var path = $"{FolderPath}/test.pdf";
await _automationHelper.WithContainer(container =>
{
var config = container.Resolve<Naps2Config>();
config.User.Set(c => c.OcrLanguageCode, "eng");
config.User.Set(c => c.EnableOcr, true);
}).RunCommand(
new AutomatedScanningOptions
{
OutputPath = path,
Verbose = true
},
ImageResources.ocr_test);
PdfAsserts.AssertContainsTextOnce("ADVERTISEMENT.", path);
AssertRecoveryCleanedUp();
}

[Fact]
public async Task ImportAndExportWithOcr()
{
Expand Down Expand Up @@ -993,12 +1036,15 @@ await _automationHelper.WithContainer(container =>
profile.BitDepth = ScanBitDepth.Grayscale;
profile.Resolution = ScanDpi.Dpi300;
profile.PageSize = ScanPageSize.A4;
var config = container.Resolve<Naps2Config>();
config.User.Set(c => c.EnableOcr, true);
config.User.Set(c => c.OcrLanguageCode, "eng");
}).RunCommand(
new AutomatedScanningOptions
{
NoProfile = true,
Device = "name1",
OutputPath = $"{FolderPath}/test.jpg",
OutputPath = $"{FolderPath}/test.pdf",
Verbose = true
},
scanDriverFactoryMock);
Expand All @@ -1008,7 +1054,8 @@ await _automationHelper.WithContainer(container =>
options.PaperSource == PaperSource.Flatbed &&
options.BitDepth == BitDepth.Color &&
options.Dpi == 200 &&
options.PageSize == PageSize.Letter),
options.PageSize == PageSize.Letter &&
options.OcrParams.LanguageCode == null),
Arg.Any<CancellationToken>(),
Arg.Any<IScanEvents>(),
Arg.Any<Action<IMemoryImage>>());
Expand Down
5 changes: 5 additions & 0 deletions NAPS2.Lib/Automation/AutomatedScanning.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ public async Task Execute()

private void ConfigureOcr()
{
if (_options.NoProfile)
{
// Don't use OCR-enabled settings from the GUI if --noprofile is set
_config.Run.Set(c => c.EnableOcr, false);
}
bool canUseOcr = IsPdfFile(_options.OutputPath) || IsPdfFile(_options.EmailFileName);
if (!canUseOcr)
{
Expand Down

0 comments on commit 6b17c5e

Please sign in to comment.