Skip to content

Commit

Permalink
Merge pull request #11 from jamesf91/feature/import-image
Browse files Browse the repository at this point in the history
Feature/import image
  • Loading branch information
jamesf91 authored Aug 14, 2021
2 parents 2a8f4d4 + fdfa7ff commit 3ac0f7a
Show file tree
Hide file tree
Showing 13 changed files with 686 additions and 412 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
packages/


*.exe
*.log
*.tlb
*.msi
*.msi
*.vcxproj.user
16 changes: 16 additions & 0 deletions ConsoleTest/ConsoleTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand All @@ -90,10 +91,25 @@
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\OnenoteAddin\OnenoteAddin.csproj">
<Project>{6fa7bd3b-28c8-4b93-b0ac-99f022986488}</Project>
<Name>OnenoteAddin</Name>
</ProjectReference>
<ProjectReference Include="..\RemarkableSync\RemarkableSync.csproj">
<Project>{090b923f-25a0-4204-846c-71c0c7f50743}</Project>
<Name>RemarkableSync</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<COMReference Include="Microsoft.Office.Interop.OneNote">
<Guid>{0EA692EE-BB50-4E3C-AEF0-356D91732725}</Guid>
<VersionMajor>1</VersionMajor>
<VersionMinor>1</VersionMinor>
<Lcid>0</Lcid>
<WrapperTool>tlbimp</WrapperTool>
<Isolated>False</Isolated>
<EmbedInteropTypes>True</EmbedInteropTypes>
</COMReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
54 changes: 24 additions & 30 deletions ConsoleTest/Program.cs
Original file line number Diff line number Diff line change
@@ -1,52 +1,46 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RemarkableSync.RmLine;
using RemarkableSync.OnenoteAddin;
using Application = Microsoft.Office.Interop.OneNote.Application;


namespace RemarkableSync
{
class Program
{
static void Main(string[] args)
{
string settingsRegPath = @"Software\Microsoft\Office\OneNote\AddInsData\RemarkableSync.OnenoteAddin";
IConfigStore _configStore = new WinRegistryConfigStore(settingsRegPath);

// setup
Dictionary<string, string> mapConfigs = new Dictionary<string, string>();
mapConfigs["sshHost"] = "10.11.99.1";
mapConfigs["SshPassword"] = "ABvontxEol";
_configStore.SetConfigs(mapConfigs);

// end setup

IRmDataSource dataSource = new RmSftpDataSource(_configStore);
//IRmDataSource dataSource = new RmCloudDataSource(_configStore);

List<RmItem> rootItems = dataSource.GetItemHierarchy().Result;
RmItem item = (from root in rootItems
where root.Type == RmItem.DocumentType
select root).ToArray()[0];

List<RmPage> pages = new List<RmPage>();
const string testFilePath = @"D:\WIP\remarkable\8920031f-06fe-4f41-90f6-d985c12718d9\5d476d18-7d06-480d-84d8-71686ba91d19.rm";
const string outImagePath = @"D:\WIP\remarkable\image.png";
RmPage page = null;

using (RmDownloadedDoc doc = dataSource.DownloadDocument(item).Result)
using (FileStream fileStream = File.OpenRead(testFilePath))
{
for (int i = 0; i < doc.PageCount; ++i)
using (MemoryStream stream = new MemoryStream())
{
pages.Add(doc.GetPageContent(i));
fileStream.CopyTo(stream);
page = RmPage.ParseStream(stream);
}
}

MyScriptClient hwrClient = new MyScriptClient(_configStore);
MyScriptResult result = hwrClient.RequestHwr(pages).Result;
if (result != null)
{
Console.WriteLine($"HWR result: {result.label}");
}
Bitmap image = RmLinesDrawer.DrawPage(page);
image.Save(outImagePath, ImageFormat.Png);

Console.WriteLine("Done exporting image");

Application oneNoteApp = new Application();
string thisPage = oneNoteApp.Windows.CurrentWindow.CurrentPageId;
OneNoteHelper helper = new OneNoteHelper(oneNoteApp);
List<Bitmap> images = new List<Bitmap> { image };
helper.AppendPageImages(thisPage, images, 0.5);

Console.WriteLine("Done inserting image");
Console.ReadKey();
}
}
Expand Down
95 changes: 94 additions & 1 deletion OnenoteAddin/OneNoteHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -8,7 +11,7 @@

namespace RemarkableSync.OnenoteAddin
{
class OneNoteHelper
public class OneNoteHelper
{
static readonly List<string> PageObjectNames = new List<string>()
{
Expand All @@ -20,6 +23,12 @@ class OneNoteHelper
"FutureObject"
};

static private int PageXOffset = 36;
static private int PageYOffset = 86;
static private int ImageGap = 50;
static private string PositionElementName = "Position";
static private string SizeElementName = "Size";

private Application _application;
private XNamespace _ns;

Expand Down Expand Up @@ -121,6 +130,51 @@ public void AddPageContent(string pageId, string content)
_application.UpdatePageContent(doc.ToString(), DateTime.MinValue, XMLSchema.xs2013);
}

public void AppendPageImages(string pageId, List<Bitmap> images, double zoom = 1.0)
{
string xml;
_application.GetPageContent(pageId, out xml, PageInfo.piAll, XMLSchema.xs2013);
var pageDoc = XDocument.Parse(xml);

int yPos = GetBottomContentYPos(pageDoc);

foreach(var image in images)
{
yPos = AppendImage(pageDoc, image, zoom, yPos) + ImageGap;
}

_application.UpdatePageContent(pageDoc.ToString(), DateTime.MinValue, XMLSchema.xs2013);
}

private int AppendImage(XDocument pageDoc, Bitmap bitmap, double zoom, int yPos)
{
int height = (int) Math.Round(bitmap.Height * zoom);
int width = (int)Math.Round(bitmap.Width * zoom);

var ns = pageDoc.Root.Name.Namespace;
XElement imageEl = new XElement(ns + "Image");

XElement positionEl = new XElement(ns + "Position");
positionEl.Add(new XAttribute("x", PageXOffset));
positionEl.Add(new XAttribute("y", yPos));

XElement sizeEl = new XElement(ns + "Size");
sizeEl.Add(new XAttribute("width", width));
sizeEl.Add(new XAttribute("height", height));

XElement dataEl = new XElement(ns + "Data");
MemoryStream stream = new MemoryStream();
bitmap.Save(stream, ImageFormat.Png);
dataEl.Value = Convert.ToBase64String(stream.ToArray());

imageEl.Add(positionEl);
imageEl.Add(sizeEl);
imageEl.Add(dataEl);

pageDoc.Root.Add(imageEl);
return (yPos + height);
}

private void GetNamespace()
{
string xml;
Expand All @@ -130,5 +184,44 @@ private void GetNamespace()
_ns = doc.Root.Name.Namespace;
}

private int GetBottomContentYPos(XDocument pageDoc)
{
var ns = pageDoc.Root.Name.Namespace;
int lowestYPos = PageYOffset;

foreach(var child in pageDoc.Root.Elements())
{
var posEl = child.Element(ns + PositionElementName);
var sizeEl = child.Element(ns + SizeElementName);
if (posEl == null || sizeEl == null)
{
continue;
}

try
{
int yPos = 0;
int height = 0;
string yAttribValue = posEl.Attribute("y")?.Value;
if (yAttribValue != null)
{
yPos = (int)double.Parse(yAttribValue);
}
string heightAttribValue = sizeEl.Attribute("height")?.Value;
if (heightAttribValue != null)
{
height = (int)double.Parse(heightAttribValue);
}

lowestYPos = Math.Max(lowestYPos, (yPos + height));
}
catch (Exception err)
{
Console.WriteLine($"OneNoteHelper.GetBottomContentYPos() - error: {err.Message}");
continue;
}
}
return lowestYPos;
}
}
}
13 changes: 11 additions & 2 deletions OnenoteAddin/RmDownloadForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 32 additions & 5 deletions OnenoteAddin/RmDownloadForm.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using RemarkableSync.RmLine;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
Expand Down Expand Up @@ -124,11 +125,12 @@ private async void btnOk_Click(object sender, EventArgs e)
}

RmTreeNode rmTreeNode = (RmTreeNode) rmTreeView.SelectedNode;
bool importAsGraphics = chkImportAsGraphics.Checked;
Console.WriteLine($"Selected: {rmTreeNode.VisibleName} | {rmTreeNode.ID}");

try
{
bool success = await ImportDocument(rmTreeNode);
bool success = await ImportDocument(rmTreeNode, importAsGraphics);
Console.WriteLine("Import " + (success ? "successful" : "failed"));
}
catch (Exception err)
Expand All @@ -141,7 +143,7 @@ private async void btnOk_Click(object sender, EventArgs e)
}


private async Task<bool> ImportDocument(RmTreeNode rmTreeNode)
private async Task<bool> ImportDocument(RmTreeNode rmTreeNode, bool importAsGraphics)
{
if (rmTreeNode.IsCollection)
{
Expand All @@ -167,15 +169,22 @@ private async Task<bool> ImportDocument(RmTreeNode rmTreeNode)
}
}

lblInfo.Text = $"Digitising {rmTreeNode.VisibleName}...";
return importAsGraphics ?
ImportContentAsGraphics(pages, rmTreeNode.VisibleName) :
await ImportContentAsText(pages, rmTreeNode.VisibleName);
}

private async Task<bool> ImportContentAsText(List<RmPage> pages, string visibleName)
{
lblInfo.Text = $"Digitising {visibleName}...";
MyScriptClient hwrClient = new MyScriptClient(_configStore);
Console.WriteLine("ImportDocument() - requesting hand writing recognition");
MyScriptResult result = await hwrClient.RequestHwr(pages);

if (result != null)
{
UpdateOneNoteWithHwrResult(rmTreeNode.VisibleName, result);
lblInfo.Text = $"Imported {rmTreeNode.VisibleName} successfully.";
UpdateOneNoteWithHwrResult(visibleName, result);
lblInfo.Text = $"Imported {visibleName} successfully.";
Task.Run(() =>
{
Thread.Sleep(500);
Expand All @@ -197,6 +206,24 @@ private void UpdateOneNoteWithHwrResult(string name, MyScriptResult result)
oneNoteHelper.AddPageContent(newPageId, result.label);
}

private bool ImportContentAsGraphics(List<RmPage> pages, string visibleName)
{
lblInfo.Text = $"Importing {visibleName} as graphics...";
OneNoteHelper oneNoteHelper = new OneNoteHelper(_application);
string currentSectionId = oneNoteHelper.GetCurrentSectionId();
string newPageId = oneNoteHelper.CreatePage(currentSectionId, visibleName);

oneNoteHelper.AppendPageImages(newPageId, RmLinesDrawer.DrawPages(pages), 0.5);

lblInfo.Text = $"Imported {visibleName} successfully.";
Task.Run(() =>
{
Thread.Sleep(500);
}).Wait();
Close();
return true;
}

private void RmDownloadForm_FormClosing(object sender, FormClosingEventArgs e)
{
_rmDataSource?.Dispose();
Expand Down
Loading

0 comments on commit 3ac0f7a

Please sign in to comment.