How to convert from android.graphics.Bitmap to javascript ImageData in NativeScript? - nativescript

Is there any existing libraries that I can easily convert a android.graphics.Bitmap object to Javascript ImageData? I am using NativeScript to build an app that takes Bitmap from camera and use tfjs for further processing. The tfjs library requires to use ImageData as an input.
In the Android part, I have the android.graphics.Bitmap object ready, which is an output from my TextureView.
var bitmap = myTextureView.getBitmap();
https://developer.android.com/reference/android/view/TextureView#getBitmap(int,%20int)
In the Tensorflow part, I need to first convert it into ImageData type.
const segmentation = await net.segmentPerson(img);
https://github.com/tensorflow/tfjs-models/tree/master/body-pix
https://developer.mozilla.org/en-US/docs/Web/API/ImageData

NativeScript !== Browser
{N} just gives you a JS runtime, it's not a Browser or WebView. It's just a way for you to reuse your web skills but you can't expect all browser dependent APIs to work here.
Thats being said, you have to take a closer look at the ImageSource module which is kind of alternate for ImageData here. But still it may not work with tfjs and I doubt tfjs works at all with {N} without significant efforts.
I see that tfjs is trying to support ReactNative, not sure how far they have got. But I presume they do it by exposing C++ APIs into native app, the same could be ported into {N} but that would require significant efforts.
So I would suggest you to find a plugin Or native equivalent on iOS / Android, which you may directly be able to use in your project.

Related

ImageSharp extremely slow on Android devices

I'm building a Xamarin app, using ImageSharp for image manipulation.
I've noticed that the ImageSharp operations are very slow in the emulator and ridiculously slow when deployed to a physical device (Galaxy Note 8).
Things that are way slow:
// Load image (very slow)
var image = SixLabors.ImageSharp.Image.Load(stream);
// Manipulate image (equally slow)
image.Mutate(i => i.Resize(new ResizeOptions
{
Mode = ResizeMode.Pad,
Position = AnchorPositionMode.Top,
Size = new SixLabors.Primitives.Size(600,400))
}));
Note that the image loads fine (albeit slowly) and the resizing works, but it's very slow (30-60 seconds).
Executing the same code from a .NET Core console program reduces the load and manipulation times to about 1-2 seconds.
With the debugger attached, I see a flood of these when ImageSharp operations are running:
GC_MINOR: (Nursery full)
I'm not really sure how to troubleshoot this, is my only option to clone the ImageSharp repo and debug using a custom build?
Edit: Not sure if this may be related to this issue?
Edit #2: I gave up on ImageSharp and also tested OpenCV to no particular avail and then settled on SkiaSharp which works great right out of the box.

Xamarin - create an environment to view HTML files

I have only written PC programs using C#, .NET, and WinForms, and am new to app development.
I have written a program that uses HTML files (as a type of "form"), that will then be passed to an app, which can then be used to read/record data. I.e.
On the PC, a user creates a HTML "form"
This is loaded onto a mobile device (phone or tablet)
The user can then open the app, and access each individual (or individual copies) a HTML form to read/record data.
This is then transferred back to the PC, and stored.
Now, I'm not too concerned with the transfer between the PC/device, or the storage of the data, or any of that at this point. What I am concerned about is the ability to open/view these HTML files within a single app.
As the program has already been written using .NET and WinForms, it needs to be converted to be used on a mobile device, and I have chosen Xamarin for this purpose. However I only have limited knowledge of this package, so I am not sure if I can achieve this within a single app (i.e. without having to pass it to a separate app to view the HTML files).
So: is there an environment in Xamarin that I can use to view HTML files within my app?
Xamarin.Forms has a WebView class that you can use to display HTML content, so viewing the content is rarely an issue.
Interacting with the HTML content can sometimes be a bit tricky because of the links not conforming to what the app expects. But you can often solve these issues using the WebView.Navigating event handler that will allow you to catch and change the navigation behavior.
Xamarin.Forms's WebView relies on the native UIWebView (iOS) and WebView (Android) and Xamarin.Forms allows you to override the native Renderers. Overriding the native renderer will give you access to the native WebView with full flexibility.
You also have the possibility to completely ignore Xamarin.Forms's WebView and write your own view using native views.
It would probably be smart to test your WinForms app on iOS using Safari and on Android using Chrome as they are using the same rendering engines that will be used in respectively UIWebView (iOS) and WebView (Android)

MediaCapture gives poor quality of QR code image captured(Windows Phone 8.1 universal app)

MediaCapture gives poor quality of QR code image captured, because of which QR code decoder(Zxing) is not able to decode the QR code(Windows Phone 8.1 universal app)
The problem comes only if QR code becomes more complex, i.e. generated out of long string(more then 180 characters), for QR codes generated out of small strings(less then 180) it works fine.
If I feed image directly to Zxing decoder(copy qr image to solution and provide uri) it works fine. But if I capture image using mediacapture api await captureManager.CapturePhotoToStorageFileAsync(imgFormat, file);
and feed it to decoder it does not work,
By looking at the image you posted in the comments for the other answer in conjunction to the code snippets you're using for focus, I would guess that you're calling the focus API at the wrong time. Make sure to use the FocusControl (and other "auto" controls such as WhiteBalance and Exposure) after starting the preview.
Also, focus takes some time to converge, so make sure you're waiting for the operation to finish before capturing a photo.
Have a look at the CameraResolution SDK sample on the Microsoft Windows Universal Samples GitHub repo. You should be looking into using the GetAvailableMediaStreamProperties method, pick the resolution you want to use, and set it on the MediaStreamType of your choosing (Video, Photo, Preview) by using the SetMediaStreamPropertiesAsync method.
Do read through the full sample, though. There are some details (like the number of MediaStreamTypes supported by the camera, see VideoDeviceCharacteristic) that you should understand to build a robust camera app.

Using non native camera or gallery

I have a part in my app where users can set a picture from gallery or take it with the camera. The app works perfectly with the native camera or gallery, but with other camera or gallery apps both intents returns null. What I have to do to make my app works fine with non native apps?
Please understand that a negative message is not a fault of the messanger. Let me cite the ultimate answer to a different question here:
there are thousands of possible camera apps -- preloaded or installed by users -- that could claim to support ACTION_VIDEO_CAPTURE, and any of them can have bugs as this one does. There is no EXTRA_PLEASE_AVOID_BUGS that you can pass on the Intent to change that.
This said, check that you are not running into the infamous low-memory condition when you launch an intent. You can find more info and some references here.

Interactive 3d Web Technology

I would like to use interactive 3D models in a web page. The required functionality is:
Import dxf file which defines & displays a room.
Add/move prebuilt objects from javascript
Add/move lamp which cast shadows from javascript
Return room dimensions to javascript
Return object positions to javascript
Can I import dxf files into any WebGL engine?
I have a small repeat user base so a browser installation is no problem at all. Is there any plugin technology I could use? Java applets? Unity? Can I use an OpenGL engine as a plugin? How about a java3d applet?
I will start with desktop but would need to be targeting tablets soon and mobiles in two years or so.
I am becoming convinced I will need to hire an expert to write this but I want to understand the options. Can you recommend a suitable technology?
I think WebGL is an excellent choice for this application; the graphics functions you describe are well within its capabilities. I can't comment on model loading, though, as I'm not familiar with WebGL engines.
However, mobile is a big wrinkle. Regarding the techniques you mention:
WebGL is supported in Chrome and Firefox for Android. On iOS, Mobile Safari does not enable WebGL, but an implementation exists (used strictly for in-app ads), so it is likely that there may be broader WebGL support in the future (possibly requiring a custom web view wrapper to enable it).
Java applets are not supported by any browser on either Android or iOS.
Unity is a viable choice; the Unity Web Player browser plugin allows embedding Unity content in a web page. However, there is no such plugin for mobile-OS devices; Unity content may be compiled into a application for iOS or Android but it cannot be viewed within a web page.
News as of August 2014: Unity has announced that the upcoming Unity 5 will include publishing to JavaScript + WebGL (no plugin required). Assuming this works as promised, you can use Unity if your target platform has a browser with WebGL support.
So, if you absolutely need cross-platform 3D including iOS right now, Unity is where it's at, but WebGL is a good choice for desktop and Android now and likely to improve on mobile in the future, and is the only way to embed 3D in a web page, not an app across desktop and Android.
WebGL is not the only way to embed 3D in a web page - see phoria.js:
http://www.kevs3d.co.uk/dev/phoria/
https://github.com/kevinroast/phoria.js
Also three.js has a canvas rendering for without web-gl.
Phoria.js and others like it will work on iPhone/iPad and Android phones that don't support web-gl. Of course, the performance is MUCH lower but if you don't have complex models and want it to work everywhere...

Resources