TitaniumMobileでカメラを使う方法
Titanium.UI.setBackgroundColor('#000'); | |
var win = Titanium.UI.createWindow({ | |
title : 'Tab 1', | |
backgroundColor : '#fff' | |
}); | |
var view = Titanium.UI.createView({ | |
backgroundColor : '#fff', | |
left : 0, | |
width : '320dp' | |
}); | |
var mainMenu = Ti.UI.createTableView({ | |
data : [{ | |
header : "カメラメニューいろいろ", | |
title : "カメラを撮る", | |
font : { | |
fontSize : '20dp' | |
} | |
}, { | |
title : "写真を見る", | |
font : { | |
fontSize : '20dp' | |
} | |
}], | |
left : 0, | |
width : '320dp' | |
}); | |
mainMenu.addEventListener("click", function(e) { | |
switch(e.index) { | |
case 0: | |
showCameraWindow(); | |
break; | |
case 1: | |
showPhotoWindow(); | |
break; | |
} | |
}); | |
view.add(mainMenu); | |
win.add(view); | |
win.open(); | |
function showCameraWindow() { | |
var options = { | |
success : function(event) { | |
drawImage(event); | |
}, | |
cancel : function() { | |
alert('なんでキャンセルするの!'); | |
}, | |
error : function(e) { | |
alert('error'); | |
}, | |
saveToPhotoGallery : true, | |
mediaTypes : [Ti.Media.MEDIA_TYPE_PHOTO] | |
}; | |
if(Ti.Platform.osname !== 'android') { | |
options.allowEditing = true; | |
} | |
Ti.Media.showCamera(options); | |
} | |
function showPhotoWindow() { | |
var options = { | |
success : function(event) { | |
drawImage(event); | |
}, | |
cancel : function() { | |
alert('なんでキャンセルするの!'); | |
}, | |
error : function(e) { | |
alert('error'); | |
}, | |
saveToPhotoGallery : true, | |
mediaTypes : [Ti.Media.MEDIA_TYPE_PHOTO] | |
}; | |
if(Ti.Platform.osname !== 'android') { | |
options.allowEditing = true; | |
} | |
Ti.Media.openPhotoGallery(options); | |
} | |
function drawImage(event) { | |
var win = Ti.UI.createWindow({ | |
left : 0, | |
width : '320dp', | |
zIndex : 2000 | |
}); | |
var view = Titanium.UI.createView({ | |
backgroundColor : 'black', | |
left : 0, | |
width : '320dp' | |
}); | |
win.add(view); | |
// cropRectにはx,y,height,widthといったデータがはいる。 | |
var cropRect = event.cropRect; | |
var image = event.media; | |
// 撮影されたデータが写真ならばImageViewとしてWindowに貼り付ける | |
if(event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO) { | |
var imageView = Ti.UI.createImageView({ | |
width : Ti.Platform.displayCaps.platformWidth, | |
height : Ti.Platform.displayCaps.platformHeight, | |
image : event.media | |
}); | |
view.add(imageView); | |
} | |
var backButton = Ti.UI.createButton({ | |
top : '7dp', | |
left : '5dp', | |
width : '60dp', | |
height : '31dp', | |
title : "閉じる", | |
}); | |
view.add(backButton); | |
backButton.addEventListener("click", function() { | |
var matrix = Ti.UI.create2DMatrix() | |
matrix = matrix.rotate(200); | |
matrix = matrix.scale(2, 2); | |
win.animate({ | |
transform : matrix, | |
duration : 350, | |
opacity : 0.1, | |
height : 200, | |
width : 200 | |
}, function() { | |
win.close(); | |
}); | |
}); | |
win.open(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment