私はあなたのコードを少し修正しました。よくわかりましたが、navbar
ボタンをクリックすると、自分が撮った写真や指定した場所にある写真が表示されますTwo
。
やりたいこと (撮影中の写真を表示する、デバイスにある写真を表示するなど) に応じて、3 つの異なる関数を定義show_pic
しました。それらを試して、ニーズに合ったものを使用してください (2それらのうち、すでにコメントになっています)。
また、私のコメントでの以前の提案を考慮しながら、次の代わりに HTML コード内 (タグshow_pic()
内) に関数を直接含めました。script
main.js
変更後の HTML ファイルは次のとおりです。
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<!-- BASIC INCLUDES (TO CHANGE ACCORDING TO YOU) -->
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0/jquery.mobile.structure-1.1.0.min.css" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css" />
<script type="text/javascript" charset="utf-8" src="cordova-2.0.0.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js"></script>
<!-- END - BASIC INCLUDES -->
<script type="text/javascript" charset="utf-8">
var pictureSource; // picture source
var destinationType; // sets the format of returned value
// Wait for Cordova to connect with the device
//
document.addEventListener("deviceready",onDeviceReady,false);
// Cordova is ready to be used!
//
function onDeviceReady() {
pictureSource=navigator.camera.PictureSourceType;
destinationType=navigator.camera.DestinationType;
}
// Called when a photo is successfully retrieved
//
function onPhotoDataSuccess(imageData) {
// Uncomment to view the base64 encoded image data
// console.log(imageData);
// Get image handle
//
var smallImage = document.getElementById('smallImage');
// Unhide image elements
//
smallImage.style.display = 'block';
// Show the captured photo
// The inline CSS rules are used to resize the image
//
smallImage.src = "data:image/jpeg;base64," + imageData;
}
// Called when a photo is successfully retrieved
//
function onPhotoURISuccess(imageURI) {
// Uncomment to view the image file URI
// console.log(imageURI);
// Get image handle
//
var largeImage = document.getElementById('largeImage');
// Unhide image elements
//
largeImage.style.display = 'block';
// Show the captured photo
// The inline CSS rules are used to resize the image
//
largeImage.src = imageURI;
}
// YOUR FUNCTIONS `show_pic()` --------------------------------------------------
// YOUR FUNCTION `show_pic()`
//
function show_pic() {
// Take picture using device camera and retrieve image as base64-encoded string
navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50,
destinationType: destinationType.DATA_URL });
}
// // YOUR FUNCTION `show_pic()`. Ex of use: onclick="show_pic(pictureSource.PHOTOLIBRARY);"
// //
// function show_pic() {
// // Take picture using device camera, allow edit, and retrieve image as base64-encoded string
// navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 20, allowEdit: true,
// destinationType: destinationType.DATA_URL });
// }
//
// // YOUR FUNCTION `show_pic()`
// //
// function show_pic(source) {
// // Retrieve image file location from specified source
// navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50,
// destinationType: destinationType.FILE_URI,
// sourceType: source });
// }
// END - YOUR FUNCTION `show_pic()` ----------------------------------------------
// Called if something bad happens.
//
function onFail(message) {
alert('Failed because: ' + message);
}
</script>
</head>
<body>
<div data-role="page">
<div data-role="content">
<div style="text-align:center;margin:20px;">
<img id="cameraPic" src="" style="width:120px;height:120px;"></img>
</div>
</div>
<div data-role="footer" data-position="fixed">
<div data-role="navbar">
<ul>
<li><a href="#">One</a></li>
<li><a href="#" onClick="show_pic()">Two</a></li>
<li><a href="#">Three</a></li>
</ul>
</div><!-- /navbar -->
</div><!-- /footer -->
</div>
</body><!-- <-- You forgot this missing DIV -->
</html>
PS:
外部 Web (http://code.jquery.com/) から CSS / JS ファイルをインポートしているため、適切なホワイトリスト ルールがファイルに追加されていることを確認してくださいres/xml/cordova.xml
。のようなものを追加する必要があるかもしれません<access origin="http://code.jquery.com/" />
。詳細については、次のリンクを確認してください: http://docs.phonegap.com/en/2.0.0/guide_whitelist_index.md.html#Domain%20Whitelist%20Guide .
また、Android で開発しているため、必ずファイルapp/res/xml/
に を追加して変更<plugin name="Camera" value="org.apache.cordova.CameraLauncher" />
し、マニフェストapp/AndroidManifest
に を追加してください<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
。
お役に立てれば。ご不明な点がございましたら、お問い合わせください。
また、より役立つ情報については、オンライン ドキュメントを確認してください: http://docs.phonegap.com/en/2.0.0/cordova_camera_camera.md.html#camera.getPicture