Skip to content

Commit 13db880

Browse files
author
Dimitar Tachev
authored
Merge pull request #78 from NativeScript/tachev/improve-demos
Improved the dimensions logging in the demos, remove unused method, bump the version.
2 parents f1cfeb5 + 28fe9d7 commit 13db880

File tree

6 files changed

+57
-56
lines changed

6 files changed

+57
-56
lines changed

demo-angular/app/app.component.html

+1-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,5 @@
44
<Switch [(ngModel)]="saveToGallery"></Switch>
55
</StackLayout>
66
<Image row="1" [src]="cameraImage" stretch="fill" margin="10"></Image>
7-
<StackLayout orientation="horizontal" row="2" padding="10">
8-
<Button text="Take Picture" (tap)='onTakePictureTap($event)'></Button>
9-
<Button text="Request Permissions" (tap)='onRequestPermissionsTap()'></Button>
10-
</StackLayout>
7+
<Button text="Take Picture" (tap)='onTakePictureTap($event)' row="2" padding="10"></Button>
118
</GridLayout>

demo-angular/app/app.component.ts

+25-15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { Component } from '@angular/core';
22
import { takePicture, requestPermissions } from 'nativescript-camera';
33
import { ImageSource } from 'tns-core-modules/image-source';
44
import { ImageAsset } from 'tns-core-modules/image-asset';
5+
import { layout } from 'tns-core-modules/utils/utils';
6+
import * as app from "tns-core-modules/application";
57

68
@Component({
79
selector: 'my-app',
@@ -12,22 +14,30 @@ export class AppComponent {
1214
public cameraImage: ImageAsset;
1315

1416
onTakePictureTap(args) {
15-
takePicture({ width: 180, height: 180, keepAspectRatio: true, saveToGallery: this.saveToGallery })
16-
.then((imageAsset) => {
17-
let source = new ImageSource();
18-
source.fromAsset(imageAsset).then((source) => {
19-
console.log(`Size: ${source.width}x${source.height}`);
20-
});
21-
this.cameraImage = imageAsset;
22-
}, (error) => {
23-
console.log("Error: " + error);
24-
});
25-
}
26-
27-
onRequestPermissionsTap() {
2817
requestPermissions().then(
29-
() => console.log('got permissions'),
30-
() => console.log('permissions rejected')
18+
() => {
19+
takePicture({ width: 180, height: 180, keepAspectRatio: true, saveToGallery: this.saveToGallery })
20+
.then((imageAsset: any) => {
21+
this.cameraImage = imageAsset;
22+
23+
// if you need image source
24+
let source = new ImageSource();
25+
source.fromAsset(imageAsset).then((source) => {
26+
let width = source.width;
27+
let height = source.height;
28+
if (app.android) {
29+
// the android dimensions are in device pixels
30+
width = layout.toDeviceIndependentPixels(width);
31+
height = layout.toDeviceIndependentPixels(height);
32+
}
33+
34+
console.log(`Size: ${width}x${height}`);
35+
});
36+
}, (error) => {
37+
console.log("Error: " + error);
38+
});
39+
},
40+
() => alert('permissions rejected')
3141
);
3242
}
3343
}

demo/app/main-page.ts

+28-18
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { View } from 'tns-core-modules/ui/core/view';
44
import { takePicture, requestPermissions } from "nativescript-camera";
55
import * as appModule from "tns-core-modules/application";
66
import * as imageSourceModule from "tns-core-modules/image-source";
7+
import { layout } from 'tns-core-modules/utils/utils';
8+
import * as app from "tns-core-modules/application";
79

810
import * as trace from "tns-core-modules/trace";
911
trace.addCategories(trace.categories.Debug);
@@ -13,28 +15,36 @@ export function navigatingTo(args: EventData) {
1315
let page = <Page>args.object;
1416
let picturePath = null;
1517

16-
page.bindingContext = fromObject({cameraImage: picturePath, saveToGallery: true});
17-
}
18-
19-
export function onRequestPermissionsTap(args: EventData) {
20-
requestPermissions().then(
21-
() => console.log('got permissions'),
22-
() => console.log('permissions rejected')
23-
);
18+
page.bindingContext = fromObject({ cameraImage: picturePath, saveToGallery: true });
2419
}
2520

2621
export function onTakePictureTap(args: EventData) {
2722
let page = <Page>(<View>args.object).page;
2823
let saveToGallery = page.bindingContext.get("saveToGallery");
29-
takePicture({width: 180, height: 180, keepAspectRatio: true, saveToGallery: saveToGallery}).
30-
then((imageAsset) => {
31-
let source = new imageSourceModule.ImageSource();
32-
source.fromAsset(imageAsset).then((source) => {
33-
console.log(`Size: ${source.width}x${source.height}`);
34-
});
35-
page.bindingContext.set("cameraImage", imageAsset);
24+
requestPermissions().then(
25+
() => {
26+
takePicture({ width: 180, height: 180, keepAspectRatio: true, saveToGallery: saveToGallery }).
27+
then((imageAsset) => {
28+
page.bindingContext.set("cameraImage", imageAsset);
29+
30+
// if you need image source
31+
let source = new imageSourceModule.ImageSource();
32+
source.fromAsset(imageAsset).then((source) => {
33+
let width = source.width;
34+
let height = source.height;
35+
if (app.android) {
36+
// the android dimensions are in device pixels
37+
width = layout.toDeviceIndependentPixels(width);
38+
height = layout.toDeviceIndependentPixels(height);
39+
}
40+
41+
console.log(`Size: ${width}x${height}`);
42+
});
43+
},
44+
(err) => {
45+
console.log("Error -> " + err.message);
46+
});
3647
},
37-
(err) => {
38-
console.log("Error -> " + err.message);
39-
});
48+
() => alert('permissions rejected')
49+
);
4050
}

demo/app/main-page.xml

+1-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
<Switch checked="{{ saveToGallery }}"/>
66
</StackLayout>
77
<Image row="1" src="{{ cameraImage }}" id="image" stretch="fill" margin="10"/>
8-
<StackLayout orientation="horizontal" row="2" padding="10">
9-
<Button text="Take Picture" tap="onTakePictureTap" />
10-
<Button text="Request Permissions" tap="onRequestPermissionsTap" />
11-
</StackLayout>
8+
<Button text="Take Picture" tap="onTakePictureTap" row="2" padding="10"/>
129
</GridLayout>
1310
</Page>

src/camera.ios.ts

-13
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,6 @@ class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePic
3737
return this;
3838
}
3939

40-
// create date from a string with format yyyy:MM:dd HH:mm:ss (like the format used in image description)
41-
private createDateFromString(value: string): Date {
42-
let year = parseInt(value.substr(0, 4));
43-
let month = parseInt(value.substr(5, 2));
44-
let date = parseInt(value.substr(8, 2));
45-
46-
let hour = parseInt(value.substr(11, 2));
47-
let minutes = parseInt(value.substr(14, 2));
48-
let seconds = parseInt(value.substr(17, 2));
49-
50-
return new Date(year, month - 1, date, hour, minutes, seconds);
51-
}
52-
5340
imagePickerControllerDidFinishPickingMediaWithInfo(picker, info): void {
5441
if (info) {
5542
let currentDate: Date = new Date();

src/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-camera",
3-
"version": "3.1.4",
3+
"version": "3.2.0",
44
"description": "Provides API for using device camera",
55
"repository": {
66
"type": "git",
@@ -21,7 +21,7 @@
2121
"test.android": "npm i && npm run tsc && npm run tslint && cd ../demo && tns build android && tns test android --justlaunch",
2222
"test.ios": "npm i && npm run tsc && npm run tslint && cd ../demo && tns build ios && tns test ios --justlaunch",
2323
"tslint": "cd .. && tslint \"**/*.ts\" --config tslint.json --exclude \"**/node_modules/**\"",
24-
"plugin.link": "npm link && cd ../demo && npm link nativescript-camera && cd ../src",
24+
"plugin.link": "npm link && cd ../demo && npm link nativescript-camera",
2525
"plugin.tscwatch": "npm run tsc -- -w",
2626
"demo.ios": "npm i && npm run tsc && cd ../demo && tns run ios --syncAllFiles",
2727
"demo.android": "npm i && npm run tsc && cd ../demo && tns run android --syncAllFiles",

0 commit comments

Comments
 (0)