インターネットで見つけたチュートリアルには以下のようにインポートすることでカメラにアクセスできると書いてありましたが、
バージョンによっては?できないみたいで
1 2 3 | // これはできない例 import {Component} from "@angular/core"; import *as Camera from "camera"; |
nativescript-cameraというカメラモジュールをインポートしてから起動すると無事に動きます!
1 2 | $ npm install nativescript-camera --save $ tns livesync android |
ちなみにこんな感じで簡単に関数作れます
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | // component 側 import {Component} from "@angular/core"; // このようにインポートすることで使用可能になる import Camera = require("nativescript-camera"); @Component({ moduleId: module.id, selector: "test", templateUrl: "./test.component.html", }) export class TestComponent { public capture() { Camera.takePicture().then((picture) => { //何かしらの処理を加える }) } } // html側 // angularでは(click)を頻繁に使っていましたけど、nativescriptでは(tap) を使用するみたいです // tapするとカメラが起動します <ActionBar title="Testのバー"> <NavigationButton text="戻る"></NavigationButton> <ActionItem text="カメラを起動する" (tap)="capture()"></ActionItem> </ActionBar> <StackLayout> <Label text="Hello world"></Label> </StackLayout> |
参考にした記事
Camera