How to take a screenshot from a layout in an ion / cord / telephone bundle?

I am trying to create a cordon based application using ionic. My application has a section in which users can select images from our server, move them or perform some actions (for example, scaling and rotation ...). In the end, I want them to be able to share the results on our website and social media. My problem is how can I take a screenshot from the layout they create? I have already seen the html2canvas library, but it has a problem with the source images, which are stored on our server and are not removed from the screen.

+1
source share
3 answers

Install the following plugin in your project

cordova plugin add https://github.com/gitawego/cordova-screenshot.git

Add this service to your angular module

.service('$cordovaScreenshot', ['$q', function($q) {
    return {
        capture: function(filename, extension, quality) {
            extension = extension || 'jpg';
            quality = quality || '100';

            var defer = $q.defer();

            navigator.screenshot.save(function(error, res) {
                if (error) {
                    console.error(error);
                    defer.reject(error);
                } else {
                    console.log('screenshot saved in: ', res.filePath);
                    defer.resolve(res.filePath);
                }
            }, extension, quality, filename);

            return defer.promise;
        }
    };
}]);

Then you can simply add a button to take a screenshot using this service.

I have a good example here to take a screenshot and share it on Facebook:

//Take a picture
$cordovaScreenshot.capture()
     .then(function(result) {
          //on success you get the image url

          //post on facebook (image & link can be null)
          $cordovaSocialSharing
            .shareViaFacebook("Text to post here...", result, "Link to share")
                  .then(function(result) {
                        //do something on post success or ignore it...
                   }, function(err) {
                        console.log("there was an error sharing!");
                   });
     }, function(err) {
         console.log("there was an error taking a a screenshot!");
 });

Note that this example uses a social plugin for ngCordova: http://ngcordova.com/docs/plugins/socialSharing/

+3
source

Screenshot.js file:

var formats = ['png','jpg'];

function Screenshot() {
}

Screenshot.prototype.save = function (callback,format,quality, filename) {
    format = (format || 'png').toLowerCase();
    filename = filename || 'screenshot_'+Math.round((+(new Date()) + Math.random()));
    if(formats.indexOf(format) === -1){
        return callback && callback(new Error('invalid format '+format));
    }
    quality = typeof(quality) !== 'number'?100:quality;
    cordova.exec(function(res){
        callback && callback(null,res);
    }, function(error){
        callback && callback(error);
    }, "Screenshot", "saveScreenshot", [format, quality, filename]);
};

Screenshot.install = function () {
      if (!window.plugins) {
        window.plugins = {};
      }

      window.plugins.screenshot = new Screenshot();
      return window.plugins.screenshot;
    };

cordova.addConstructor(Screenshot.install); 

This way I can make the call with the following code:

window.plugins.screenshot.save(function(error,res){
          if(error){
            alert(error);
          }else{
            alert('ok',res.filePath); //should be path/to/myScreenshot.jpg
          }
        },'jpg',50,'myScreenShot');

This worked perfectly on my Android smartphone.

I also added res / xml / config.xml:

<feature name="Screenshot">
    <param name="android-package" value="org.apache.cordova.screenshot.Screenshot"/>
</feature>

In the AndroidManifest.xml file:

And added the java class in the following package: org.apache.cordova.screenshot.Screenshot

All these configurations contain information in the plugin.xml file of the plugin.

0
source

Source: https://habr.com/ru/post/1627152/


All Articles