Hamee Advent Calendar 2015の19日目です。
はじめに
作りたいアプリがあってハイブリッドアプリフレームワークionicを触ってるので、さくっとバーコードリーダーアプリを作りたいと思います。アプリのプロトタイプをスピーディーに作るのにionic(Cordova + AngularJS)かなり便利です。
ionicをインストールする
指示に従えば問題なくインストールな難なく完了するはず。
npm install -g cordova ionic
プロジェクトを作る
ionic start BarcodeReader blank
cd BarcodeReader
プラットフォームを追加
実行するプラットフォームを追加します。私は両方で実行したいのでios,android両方を追加。
ionic platform add ios
ionic platform add android
バーコードリーダーのCordovaプラグインを追加
cordova plugin add https://github.com/wildabeast/BarcodeScanner.git
ngCordovaをインストール
AngularJSからCordovaプラグインを簡単に呼び出す為にngCordovaをインストールします。
bower install ngCordova
ngCordovaを読み込む
<script src="lib/ngCordova/dist/ng-cordova.js"></script>
をindex.htmlに追記する。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title></title>
<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
<link href="css/ionic.app.css" rel="stylesheet">
-->
<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="lib/ngCordova/dist/ng-cordova.js"></script>
<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>
<!-- your app's js -->
<script src="js/app.js"></script>
</head>
<body ng-app="starter">
<ion-nav-view></ion-nav-view>
</body>
</html>
viewを書く
www/views/barcode.htmlを作成
<ion-view>
<ion-content>
<button class="button" ng-click='scan()'>click!
</button>
</ion-content>
</ion-view>
app.jsを書く
ngCordovaのモジュールを追加。
//angular.module('starter', ['ionic'])
angular.module('starter', ['ionic', 'ngCordova'])
ルーティングを追加。
.config(function($stateProvider, $urlRouterProvider){
$stateProvider
.state('barcode',{
url:'/barcode',
templateUrl: 'views/barcode.html',
controller: 'BarcodeCtrl'
});
$urlRouterProvider.otherwise('/barcode');
})
コントローラーを追加
.controller('BarcodeCtrl', ['$scope','$cordovaBarcodeScanner','$ionicPlatform',function($scope,$cordovaBarcodeScanner,$ionicPlatform) {
$scope.scan = function(){
$ionicPlatform.ready(function() {
$cordovaBarcodeScanner.scan().then(function(barcodeData) {
alert(JSON.stringify(barcodeData));
}, function(error) {
alert(JSON.stringify(error));
});
});
}
}]);
最終的にはこんな感じになります。
// Ionic Starter App
// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
//angular.module('starter', ['ionic'])
angular.module('starter', ['ionic', 'ngCordova'])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
if(window.cordova && window.cordova.plugins.Keyboard) {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
// Don't remove this line unless you know what you are doing. It stops the viewport
// from snapping when text inputs are focused. Ionic handles this internally for
// a much nicer keyboard experience.
cordova.plugins.Keyboard.disableScroll(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
})
.config(function($stateProvider, $urlRouterProvider){
$stateProvider
.state('barcode',{
url:'/barcode',
templateUrl: 'views/barcode.html',
controller: 'HomeCtrl'
});
$urlRouterProvider.otherwise('/barcode');
})
.controller('BarcodeCtrl', ['$scope','$cordovaBarcodeScanner','$ionicPlatform',function($scope,$cordovaBarcodeScanner,$ionicPlatform) {
$scope.scan = function(){
$ionicPlatform.ready(function() {
$cordovaBarcodeScanner.scan().then(function(barcodeData) {
alert(JSON.stringify(barcodeData));
}, function(error) {
alert(JSON.stringify(error));
});
});
}
}]);
コンパイル
ionic build ios
ionic build android
エミュレーターや実機で確認
ionic run ios
ionic run android
終わりに
ionicにはionic creatorというツールもあるので、UIと遷移をザーッと作って、ngCordovaでネイティブ機能を組み込めばかなりスピーディーに作れそう。
おまけ
使ってるバーコードリーダーのライブラリの読み取り精度が低い、、scanditの方が精度いいなぁ。