Crop a two side page document to a single page document

Have you ever had the problem that you scan documents that’s are printed with two pages on each side and you need this document as a single page Document?

The MBS Xojo Plugins can help you with this problem in Xojo. Our Xojo Plugins includes a GraphicsMagick plugin. With the classes in this plugin you can do a lot of graphic editing, like using graphics effects, add text on an image or crop an image by a given area. Compared to Xojo pictures, we can work even with 16bit color depth here and use exact same functions cross platform.

In this article I want to show you how to cut the pages and set a serial page number at the bottom of each page. At first we load the scanned pictures from a folder to our application. For that we query the folder items to an array. With the method SortedFilesMBS we get an array that is sorted by name already. In the parameters of this method, we can set as the second Parameter that we want to sort the array by filenames. My scanned documents are names Scan_1, Scan_2 and so on.

Dim folder As FolderItem = SpecialFolder.Desktop.Child("Scan")
Dim files() As FolderItem = folder.SortedFilesMBS(False, True, False)

Then we want to loop over this array and cut the pages:

Dim u As Integer = files.Ubound
For i As Integer = 0 To u

We load the current file in two GMImage Objects. One for the Left side (imageL) and one to the right side (imageR). Instead of loading the file twice, we use the copy constructor to get two identical image objects.

Dim imageL As New GMImageMBS(files(i))
Dim imageR As New GMImageMBS(imageL) // copy constructor

If no exception is raised, we determine the height and width of the target pictures. We imagine we have a Din A4 paper on which two pages are printed. The long site is at the bottom side. The page is halved on this side. Therefore we need the full height of the sheet but only half the width. The clipping area is defined as GMGeometryMBS object. In the parameters we can defined the height and width of the cropping rectangle and optional the position of the top left edge coordinates. The default position coordinates are 0/0. That’s why we don’t need to set the coordinates for the left mask. For the left side we only need the height and width of the output. The right side has the same size but the x position is different. It is the half of the long bottom page side and that is w.

Dim h As Integer = imageL.height
Dim w As Integer = imageL.width / 2
Dim geoL As New GMGeometryMBS(w, h)
Dim geoR As New GMGeometryMBS(w, h, w, 0)

With the method crop we cut out the pages.

imageL.crop(geoL)
imageR.crop(geoR)

Then we want to write a page number in the bottom of each page. For drawing on a GMImage we set a graphic object as a property of the image.Then we set the color, the stroke width and the font, before we setting the text and the position of the page number. Afterwards we can draw the page number to the image. We need to take care about the stroke width, because if the value is too big the number is no longer legible. We need to calculate the text of the page number. With each loop run we manage two pages, we multiply it with two. Our variable i starts at 0, but our first left side has the number 1. That's why we add 1 for the left side and add 2 for the right side. For this reason we have for the left side the formula: I * 2 + 1 and for the right side the formula I * 2 + 2.

The code for the right page number looks similar.

Dim drawL As GMGraphicsMBS = imageL.Graphics
drawL.strokeColor(New GMColorRGBMBS("black"))
drawL.strokeWidth(1)
drawL.Font("/Library/Fonts/Verdana.ttf")
drawL.Text(w / 2, h - 100, str(i * 2 + 1))
drawL.Draw

Next we set the format of the image, create folder items and save the images on our desktop in a folder named “Output”, creating it if necessary.

imageL.magick = "PNG"
imageR.magick = "PNG"
Dim Output as folderitem = SpecialFolder.Desktop.Child("Output")
Output.CreateAsFolder
Dim sl As FolderItem = Output.Child(Str(i) + "a.png")
Dim sr As FolderItem = Output.Child(Str(i) + "b.png")

imageL.write(sl)
imageR.write(sr)
Next

I hope that you like this example. We wish you lots of fun with the GraphicsMagick classes and functionalities. If you have any questions, please do not hesitate to contact us.

Xojo developer meeting in New York City

The Xojo.Connect conference will take place March 25-27 in Nashville, TN. On our way to Nashville we stop in New York for a few days and we like to organize another Xojo developer meeting, this time in New York City.

Date set: 20th March 2020.
Please email us if you like to be added to the guest list.

We meet in a nice restaurant somewhere in New York City, so we can shop talk about Xojo and have dinner together. Time will probably be around 18 to 22 o'clock and you can of course come later or leave earlier.

If you are interested in private time for consulting, training or discussion MBS or Xojo topics, we can of course schedule a meeting. Please contact me directly interested.

Multithreaded JavaScript usage

We got some interest from customers about our JavaScriptEngineMBS class and requests for better Memoryblock handling and threading. So for next prerelease of our 20.1 plugins, we include better handling of Memoryblocks. You can pass a memory block to JavaScript and receive a Memoryblock back. Either with using global properties or as parameter/result of a function or evaluate call. The data in the Memoryblock is copied and available in JavaScript as an UInt8 buffer of the matching size. We add GlobalMemoryBlock() to help you and pass a memory block which is then used by reference, so data is not copied and JavaScript can directly edit the memory from Xojo.



Combine this with our new EvaluateMT and CallFunctionMT methods and you can run various JavaScript codes on preemptive threads in the background and get all CPU cores busy. Our screenshot above shows 8 threads running the function to do the image effect 100 times.

Please makes sure the memory blocks are not released too early and you use one JavaScriptEngineMBS object per thread to keep things separated. If you access a JavaScriptEngineMBS object on two threads, we now raise an exception telling you the object is busy. Even if you use only one thread with one instance, you can keep your GUI responsive while JavaScript runs in background. Using Print and Input events or Xojo delegate method calls, it may talk to your main thread and the GUI. (more)

One month till Xojo.Connect 2020 in Nashville

Just one month till the XOJO.CONNECT 2020 in Nashville, Tennessee, USA. Tickets are still available for $999 USD.



It will be held March 25-27, 2020 in Nashville, TN at the Sheraton Music City Hotel. This conferences is the best place to meet Xojo developers from around the world in real live, make contacts, present yourself as expert and learn what is new in Xojo. Tickets are available in the Xojo store and if you bring your partner, you can order an extra guest ticket for the dinner events.

Check out the conference highlights video if you want to see what it's like - or ask one of the many attendees from the forum!

Wether you are full or part time Xojo developer, this is your chance to learn all about the Web 2.0 framework, the Android progress and what's new in the Xojo world.

MBS Xojo Plugins, version 20.1pr5

New in this prerelease of the 20.1 plugins:
  • Added EvaluateMT and CallFunctionMT methods to JavaScriptEngineMBS class.
  • Added SkipMode parameter to constructors for FileListMBS class to skip over files/folders and hidden items.
  • Disabled a warning from our own check code about data size of SQLDatabaseMBS class.
  • Fixed NameFieldStringValue in NSSavePanelMBS to properly set with empty text.
  • Updated SQLAPI to version 5.0.5.
  • Changed AbsolutePathMBS (and AbsolutePath) to return directory path with double colon.
  • Fixed problem with DynaPDF's graphics class integration on Windows 64-bit when running Xojo report to PDF.
  • Fixed a problem with recursive templates for ExtractPageText and ExtractPageTextRect functions in DynaPDFMBS class.
  • Added ExifThumbnail function to GM16ImageMBS and GMImageMBS class.
  • Added ExifThumbnail property to JPEGImporterMBS class.
  • Added Looping property to MFPMediaPlayerMBS class.
Download: monkeybreadsoftware.com/xojo/download/plugin/Prerelease/.
Or ask us to be added to our shared Dropbox folder.

Reading JPEG Thumbnails from EXIF

For next MBS Xojo Plugins we add new methods to JPEGImporterMBS class to read thumbnail from EXIF data in a JPEG file. This allows you to very quickly get a thumbnail image. We support ScaleFactor property for some years now, which allows you to read a JPEG scaled down and get only the pixel data decompressed you need. We use this often for a preview. The time differences may be for a test image here: 5.6 milliseconds for the thumbnail at 160x120, 64 milliseconds for a preview at 504x378 and 276 milliseconds for full image at 4032x3024 pixels. So in your projects, if you need to show JPEGs quickly, you may first check for a thumbnail, then for a scaled down preview and only if user double click the entry load the whole picture.

Here some sample code:

Dim file As FolderItem = SpecialFolder.Desktop.Child("test.jpg") // first read header and check for thumbnail in EXIF Dim j As New JPEGImporterMBS j.file = file j.ReadExifData = True If j.ReadHeader Then Dim thumbnailData As String = j.ExifThumbnail If thumbnailData.LenB > 0 Then thumbnailPic = picture.FromData(thumbnailData) End If // now read a scaled down image Dim factor As Double = Min( j.Height / 300.0, j.Width / 300.0) If factor >= 8.0 Then j.ScaleFactor = 8 Elseif factor >= 4.0 Then j.ScaleFactor = 4 Else j.ScaleFactor = 2 End If j.Import Pic2 = j.Picture // finally read full image j.ScaleFactor = 0 j.Import Pic3 = j.Picture Else MsgBox j.ErrorMessage End If
Coming soon for MBS Plugins version 20.1pr5. See also CGImageSourceMBS class for MacOS to get quickly thumbnails for various image formats.

Programming your own Solar System with Xojo + MBS SceneKit

Stefanie wrote another blog post for the official Xojo blog:



Programming your own Solar System with Xojo and MBS SceneKit

If you join Xojo Connect conference next month, please come to Stefanie's presentation: Scenekit - Stefanie Juchmes, 4 pm on 26th March. Please don't hesitate to send us questions.

Upgrading WebKit for Windows support

As you may have seen Xojo 2019r3 uses a newer version of Chromium Embedded Framework for WebKit support on Windows. For us this means we have to put the upgrade of our Chromium plugin classes on the todo list.

For MBS Xojo Plugins 20.1 we upgrade our Chromium classes. You can try it with the recent 20.1pr4 pre-release version.

The changes include a few fixes for ChromiumCookieManagerMBS and ChromiumWebPluginInfoMBS class which both had crashing bugs, but seem to work fine now. You guys can be happy to use a high level language like Xojo where the compiler always makes sure your delegates have the right parameters as any mismatch can cause hard to track bugs later. Luckily we fixed it and now leverage C++ compiler to test those, too.

With newer plugin you can use Chromium with all Xojo versions since the introduction of WebKit support. We now support 3 different versions of the Chromium Embedded Framework used by Xojo with the same plugin. When you start using the plugin classes, we check which CEF version is used and use the right function pointers. Enjoy!

MBS Xojo Plugins, version 20.1pr4

New in this prerelease of the 20.1 plugins: Download: monkeybreadsoftware.com/xojo/download/plugin/Prerelease/.
Or ask us to be added to our shared Dropbox folder.

Improve your work live balance with do not disturb

To improve the balance between work and live, some simple things can help:

First, it's a great idea to set a rule in the household to have no phones for meals. So I just leave my iPhone in the office, so it doesn't grab my attention. Some families have baskets for the phones so when you enter kitchen, you put it there. We'll see if we do this when our children get older and we get more devices.

Next figure out the time you allow others to disturb you. The default setting for iOS is to be silent between 22:00 at night and 7:00 o'clock in the morning. Now it seems to work better for me to move those times to 20:00 and 9:00 o'clock. In the morning I may get up long before 9, but to get ready, have breakfast and read through emails, it may be 9 when I am ready to take calls.

For the evening it's difficult alone to get away from work and not work till midnight, but at least the iPhone won't disturb me when I am busy doing some focused work, or watch a movie with my family.

What do you do to get some rest from screens?

Multithreaded plugin functions can increase speed of Xojo application

Have you seen the "NSImageMBS multithreading" or "Threaded Encryption and Hashes" example projects in Xojo?


Do you notice that with 4 Xojo threads, the plugin can actually get 4 CPU cores busy and get work done faster?

Over the last releases we added quite a few MT methods. Those are plugin methods which perform work on a preemptive thread. The Xojo thread calling them yields CPU time to other threads while it waits for the preemptive thread to finish work. And if you use several threads in Xojo already, this will help you keep more CPU cores busy. (more)

Building slides for Xojo.Connect

As a speaker you should now take the time to prepare slides a month before the XOJO.CONNECT conference in Nashville, Tennessee, USA.
What is your favorite MBS Plugin feature from the last 12 months we should highlight?



It will be held March 25-27, 2020 in Nashville, TN at the Sheraton Music City Hotel. This conferences is the best place to meet Xojo developers from around the world in real live, make contacts, present yourself as expert and learn what is new in Xojo. Tickets are available in the Xojo store and if you bring your partner, you can order an extra guest ticket for the dinner events.

Check out the conference highlights video if you want to see what it's like - or ask one of the many attendees from the forum!

Wether you are full or part time Xojo developer, this is your chance to learn all about the Web 2.0 framework, the Android progress and what's new in the Xojo world.

MBS Xojo Plugins, version 20.1pr3

New in this prerelease of the 20.1 plugins: Download: monkeybreadsoftware.com/xojo/download/plugin/Prerelease/.
Or ask us to be added to our shared Dropbox folder.

Customized File Open and Save Dialogs for Windows

This week, I learnt a lot about the APIs from Microsoft for open and save dialogs. For Xojo we got new WinFileOpenDialogMBS and WinFileSaveDialogMBS classes. Both are based on WinFileDialogMBS class and report errors via WinFileDialogExceptionMBS. To return items, we use WinShellItemMBS class and WinShellItemArrayMBS for a list of items. To define file types, we got the WinFileTypeMBS class. The dialog may look like this:



We customized the title, the OK button, the label for the file name and added a list of file types to fill the file types popup menu. As you see there are custom controls on the dialog with two text fields and a checkbox with a popupmenu below. We added a lot of events for the WinFileDialogMBS class, so you not just learn about changed selections, folder changes, but also about events to custom controls. In the CheckButtonToggled event as an example we can react on the checkbox and enable/disable the popupmenu below.

While most people are happy with the built-in OpenFileDialog and SaveFileDialog classes, our plugin classes offer many more features for Windows specific projects. Please check NSOpenPanelMBS and NSSavePanelMBS classes for MacOS to do similar things. And we still have OpenDialogMBS as a cross platform class.

The example project with a lot of settings:



Please try soon with next prerelease of MBS Xojo Plugins 20.1.

Our JavaScript engine, a game changer

Yesterday Sean Beach, one of our clients in New York emailed me back about our JavaScript integration. To quote him:

"Wanted to follow back and say that this plugin is a game changer for my showcontrol product. Thanks so much!”

The discussion was about our JavaScript functions and a couple of his questions got answered in our Tips for our JavaScript functions blog post:

"This document is fantastic, and actually answers most of my questions from the previous email."

We are delighted about the feedback we got!

The same day Stefanie got two of her latest articles published about using JavaScript to calculate distances.

On our FileMaker blog the version using our FileMaker Plugin:
FileMaker and JavaScript - the perfect combination

And even on the official Xojo blog an article about our Xojo class to run JavaScript:
Xojo and JavaScript – A Perfect Combination

We are happy to see that our JavaScript integration is so well received by clients from around the world. And it just started with a hint from one user in early December to take a look into this topic.

Check out the JavaScriptEngineMBS class for Xojo and the JavaScript functions for FileMaker!

20 Years Monkeybread Software Party

This year in April our company will turn 20 years old.
We'll plan to have a big party here in Germany near our office with over 100 guests.

Today we send invitations to guests who have not yet responded.

If you like to join and you miss an invitation, you can contact us and ask whether your invitation got lost.

As people confirm they are coming, we'll add them to the guest list. If we run out of space, we may put people on the wait list.
If you can't make it, please respond soon, so we don't need to contact you again later.

MBS Xojo Plugins, version 20.1pr2

New in this prerelease of the 20.1 plugins: Download: monkeybreadsoftware.com/xojo/download/plugin/Prerelease/.
Or ask us to be added to our shared Dropbox folder.

Airplay related classes for AVFoundation

For MacOS, we add AVRouteDetectorMBS class to detect if multiple routes are available, e.g. for Airplay.
And for MacOS 10.15 we add AVRoutePickerViewControlMBS control to show the standard control from Apple to select the output device:



If you have an Airplay enabled device, it should show up in the popup menu.
Our example project uses AVRouteDetectorMBS class to detect if multiple routes are available. If that is true, we show the AVRoutePickerViewControlMBS control and let the user pick the destination for the current video player.

Tips for our JavaScript functions

Here a few tips we recently learnt for our JavaScript engine based on Duktape in the MBS Plugins for FileMaker and Xojo:
  1. Use performance.now() to get time within JavaScript and measure how long something takes. Here an example where we measure how many milli seconds it takes to run a loop one million times:
    function testFunction() {
        for (var i = 0; i < 1e6; i++) {}
    }
    
    var t1 = performance.now();
    testFunction();
    var t2 = performance.now();
    
    Print('test took: ' + (t2 - t1) + ' milliseconds');
  2. All global functions and properties in JavaScript are attached to the global object. You can access them explicitly via globalThis keyword.
  3. List global properties via JavaScript:
    Var list = Object.keys(globalThis);
    The list will include all global names including functions like Print registered by MBS Plugin.
  4. Check Duktape.version for the version number. If we update the engine, you can check the version number and compare. Currently the value is 20500 for version 2.5.
  5. Use unicode escapes in strings if needed to mask special characters. While the engine handles UTF-8 fine, you can escape some characters with \u followed with a hex 4 digit number, e.g.
  6. n = '\u0041';
    Which will assign "A" to variable n.
  7. Use built-in functions to encode text as hex or base64 via Duktape.enc:
    Duktape.enc('base64', 'Hello')
    This returns "SGVsbG8=".
  8. Use Duktape.dec and TextDecoder class to decode base64/hex encoded text to UTF-8 string:
    var result = new TextDecoder().decode(Duktape.dec('base64', 'Zm9v'));
    Result is now a variable with text "foo".
  9. Access call stack with Duktape.act function. Query Duktape.act(i) with various values of i. Pass -1 for the act() call, -2 for the current function and -3 for the caller of current function. The returned object contains properties for lineNumber and function. When you query function.name, you get the name of the function.
  10. Add console object if needed. You can add a console object and include a log function, which sends arguments to a Print function:
    console = { log: function() { Print(Array.prototype.join.call(arguments, ' ')); } };
  11. Provide environment some JavaScript may need. If some JavaScript expects a global window object to store something, you can simply create a dummy one:
    window = {};
    This may not provide functionality, but allows at least code to check for this to work.
  12. Prefer functions over evaluating code. Code in functions is parsed and uses registers for the virtual machine, while code evaluated may use named variables, so if you can put your code in a function and just evaluate the function call, things may execute a bit faster.
  13. Be aware all numbers in JavaScript are doubles. Evaluate "9999999999999999" and you get "10000000000000000" back. Comparing numbers may not give the result you expect as "0.1+0.2==0.3" evaluates to false.
If you have questions, please do not hesitate to contact us. See also JS.Evaluate and related JavaScript functions in MBS FileMaker Plugin and JavaScriptEngineMBS class in MBS Xojo Plugins.

17 Years Xojo Developer Magazine

Did you notice that over the last 17 years we got 103 issues of the Xojo developer magazine?



As you know the magazine is more up to date than any book and we highly recommend a subscription for all Xojo developers. Keep up to date on news, development best practice and learn tips and tricks.

Today you get all 103 back issues with 7360 pages in total including 400 pages mentioning MBS classes, modules or controls. Check the Welcome to Xojo bundle for new users.

You need a subscription? Contact us via form before 5th Feb 2020 to win one of three short subscriptions (3 issues).
The biggest plugin in space...

Archives

Mar 2024
Feb 2024
Jan 2024
Dec 2023
Nov 2023
Oct 2023
Sep 2023
Aug 2023
Jul 2023
Jun 2023
May 2023
Apr 2023
Mar 2023
Feb 2023
Jan 2023
Dec 2022
Nov 2022
Oct 2022
Sep 2022
Aug 2022
Jul 2022
Jun 2022
May 2022
Apr 2022
Mar 2022
Feb 2022
Jan 2022
Dec 2021
Nov 2021
Oct 2021
Sep 2021
Aug 2021
Jul 2021
Jun 2021
May 2021
Apr 2021
Mar 2021
Feb 2021
Jan 2021
Dec 2020
Nov 2020
Oct 2020
Sep 2020
Aug 2020
Jul 2020
Jun 2020
May 2020
Apr 2020
Mar 2020
Feb 2020
Jan 2020
Dec 2019
Nov 2019
Oct 2019
Sep 2019
Aug 2019
Jul 2019
Jun 2019
May 2019
Apr 2019
Mar 2019
Feb 2019
Jan 2019
Dec 2018
Nov 2018
Oct 2018
Sep 2018
Aug 2018
Jul 2018
Jun 2018
May 2018
Apr 2018
Mar 2018
Feb 2018
Jan 2018
Dec 2017
Nov 2017
Oct 2017
Sep 2017
Aug 2017
Jul 2017
Jun 2017
May 2017
Apr 2017
Mar 2017
Feb 2017
Jan 2017
Dec 2016
Nov 2016
Oct 2016
Sep 2016
Aug 2016
Jul 2016
Jun 2016
May 2016
Apr 2016
Mar 2016
Feb 2016
Jan 2016
Dec 2015
Nov 2015
Oct 2015
Sep 2015
Aug 2015
Jul 2015
Jun 2015
May 2015
Apr 2015
Mar 2015
Feb 2015
Jan 2015
Dec 2014
Nov 2014
Oct 2014
Sep 2014
Aug 2014
Jul 2014
Jun 2014
May 2014
Apr 2014
Mar 2014
Feb 2014
Jan 2014
Dec 2013
Nov 2013
Oct 2013
Sep 2013
Aug 2013
Jul 2013
Jun 2013
May 2013
Apr 2013
Mar 2013
Feb 2013
Jan 2013
Dec 2012
Nov 2012
Oct 2012
Sep 2012
Aug 2012
Jul 2012
Jun 2012
May 2012
Apr 2012
Mar 2012
Feb 2012
Jan 2012
Dec 2011
Nov 2011
Oct 2011
Sep 2011
Aug 2011
Jul 2011
Jun 2011
May 2011
Apr 2011
Mar 2011
Feb 2011
Jan 2011
Dec 2010
Nov 2010
Oct 2010
Sep 2010
Aug 2010
Jul 2010
Jun 2010
May 2010
Apr 2010
Mar 2010
Feb 2010
Jan 2010
Dec 2009
Nov 2009
Oct 2009
Sep 2009
Aug 2009
Jul 2009
Apr 2009
Mar 2009
Feb 2009
Dec 2008
Nov 2008
Oct 2008
Aug 2008
May 2008
Apr 2008
Mar 2008
Feb 2008