|
1 | | -import { Observable } from 'tns-core-modules/data/observable'; |
2 | | -import { ImageCropper } from 'nativescript-imagecropper'; |
| 1 | +import { Observable } from "tns-core-modules/data/observable"; |
| 2 | +import { ImageCropper } from "nativescript-imagecropper"; |
3 | 3 | import * as camera from "nativescript-camera"; |
4 | 4 | import * as permissions from "nativescript-permissions"; |
5 | 5 | import { Image } from "tns-core-modules/ui/image"; |
6 | 6 | import * as frameModule from "tns-core-modules/ui/frame"; |
7 | | -import * as imageSource from 'tns-core-modules/image-source'; |
| 7 | +import * as imageSource from "tns-core-modules/image-source"; |
8 | 8 |
|
9 | 9 | declare var android: any; |
10 | 10 |
|
11 | 11 | export class ImageCropperModel extends Observable { |
12 | | - private imageCropper: ImageCropper; |
13 | | - private imageSource: imageSource.ImageSource; |
14 | | - private croppedImage; |
| 12 | + private imageCropper: ImageCropper; |
| 13 | + private imageSource: imageSource.ImageSource; |
| 14 | + private croppedImage; |
15 | 15 |
|
16 | | - constructor() { |
17 | | - super(); |
| 16 | + constructor() { |
| 17 | + super(); |
18 | 18 |
|
19 | | - this.imageCropper = new ImageCropper(); |
20 | | - this.croppedImage = frameModule.topmost().getViewById("croppedImage"); |
21 | | - } |
| 19 | + this.imageCropper = new ImageCropper(); |
| 20 | + this.croppedImage = frameModule.topmost().getViewById("croppedImage"); |
| 21 | + } |
22 | 22 |
|
23 | | - tapCameraAction = function() { |
24 | | - if (camera.isAvailable()) { |
25 | | - permissions.requestPermission([android.Manifest.permission.CAMERA, android.Manifest.permission.WRITE_EXTERNAL_STORAGE]) |
26 | | - .then(() => { |
27 | | - camera.takePicture({width:300,height:300,keepAspectRatio:true}) |
28 | | - .then((imageAsset) => { |
29 | | - let source = new imageSource.ImageSource(); |
30 | | - source.fromAsset(imageAsset).then((source) => { |
31 | | - this.imageCropper.show(source).then((args) => { |
32 | | - console.dir(args); |
33 | | - if(args.image !== null){ |
34 | | - this.croppedImage.imageSource = args.image; |
35 | | - } |
36 | | - }) |
37 | | - .catch(function(e){ |
38 | | - console.dir(e); |
39 | | - }); |
| 23 | + tapCameraAction = function() { |
| 24 | + if (camera.isAvailable()) { |
| 25 | + permissions |
| 26 | + .requestPermission([ |
| 27 | + android.Manifest.permission.CAMERA, |
| 28 | + android.Manifest.permission.WRITE_EXTERNAL_STORAGE |
| 29 | + ]) |
| 30 | + .then(() => { |
| 31 | + camera |
| 32 | + .takePicture({ |
| 33 | + width: 500, |
| 34 | + height: 500, |
| 35 | + keepAspectRatio: true |
| 36 | + }) |
| 37 | + .then(imageAsset => { |
| 38 | + let source = new imageSource.ImageSource(); |
| 39 | + source.fromAsset(imageAsset).then(source => { |
| 40 | + this.imageCropper |
| 41 | + .show(source) |
| 42 | + .then(args => { |
| 43 | + console.dir(args); |
| 44 | + if (args.image !== null) { |
| 45 | + this.croppedImage.imageSource = |
| 46 | + args.image; |
| 47 | + } |
| 48 | + }) |
| 49 | + .catch(function(e) { |
| 50 | + console.dir(e); |
| 51 | + }); |
| 52 | + }); |
| 53 | + }) |
| 54 | + .catch(err => { |
| 55 | + console.log("Error -> " + err.message); |
| 56 | + }); |
| 57 | + }) |
| 58 | + .catch(function() { |
| 59 | + // When user denies permission |
| 60 | + console.log("User denied permissions"); |
40 | 61 | }); |
41 | | - }).catch((err) => { |
42 | | - console.log("Error -> " + err.message); |
43 | | - }); |
44 | | - }) |
45 | | - .catch(function () { |
46 | | - // When user denies permission |
47 | | - console.log("User denied permissions"); |
48 | | - }); |
49 | | - } |
50 | | - }; |
| 62 | + } |
| 63 | + }; |
51 | 64 |
|
52 | | - tapCameraActionResize = function() { |
53 | | - if (camera.isAvailable()) { |
54 | | - permissions.requestPermission([android.Manifest.permission.CAMERA, android.Manifest.permission.WRITE_EXTERNAL_STORAGE]) |
55 | | - .then(() => { |
56 | | - camera.takePicture({width:300,height:300,keepAspectRatio:true}) |
57 | | - .then((imageAsset) => { |
58 | | - let source = new imageSource.ImageSource(); |
59 | | - source.fromAsset(imageAsset).then((source) => { |
60 | | - this.imageCropper.show(source,{width:100,height:100}).then((args) => { |
61 | | - console.dir(args); |
62 | | - if(args.image !== null){ |
63 | | - this.croppedImage.imageSource = args.image; |
64 | | - } |
65 | | - }) |
66 | | - .catch(function(e){ |
67 | | - console.dir(e); |
68 | | - }); |
| 65 | + tapCameraActionResize = function() { |
| 66 | + if (camera.isAvailable()) { |
| 67 | + permissions |
| 68 | + .requestPermission([ |
| 69 | + android.Manifest.permission.CAMERA, |
| 70 | + android.Manifest.permission.WRITE_EXTERNAL_STORAGE |
| 71 | + ]) |
| 72 | + .then(() => { |
| 73 | + camera |
| 74 | + .takePicture({ |
| 75 | + width: 300, |
| 76 | + height: 300, |
| 77 | + keepAspectRatio: true |
| 78 | + }) |
| 79 | + .then(imageAsset => { |
| 80 | + let source = new imageSource.ImageSource(); |
| 81 | + source.fromAsset(imageAsset).then(source => { |
| 82 | + this.imageCropper |
| 83 | + .show(source, { width: 500, height: 500 }) |
| 84 | + .then(args => { |
| 85 | + console.dir(args); |
| 86 | + if (args.image !== null) { |
| 87 | + this.croppedImage.imageSource = |
| 88 | + args.image; |
| 89 | + } |
| 90 | + }) |
| 91 | + .catch(function(e) { |
| 92 | + console.dir(e); |
| 93 | + }); |
| 94 | + }); |
| 95 | + }) |
| 96 | + .catch(err => { |
| 97 | + console.log("Error -> " + err.message); |
| 98 | + }); |
| 99 | + }) |
| 100 | + .catch(function() { |
| 101 | + // When user denies permission |
| 102 | + console.log("User denied permissions"); |
69 | 103 | }); |
70 | | - }).catch((err) => { |
71 | | - console.log("Error -> " + err.message); |
72 | | - }); |
73 | | - }) |
74 | | - .catch(function () { |
75 | | - // When user denies permission |
76 | | - console.log("User denied permissions"); |
77 | | - }); |
78 | | - } |
79 | | - }; |
| 104 | + } |
| 105 | + }; |
80 | 106 | } |
0 commit comments