9

Android で Phone gap を使用して Camera API を実行しようとしていますが、あらゆる種類の問題が発生しています。

phonegapカメラの例をコピーしました。

次のエラーが表示されます

07-12 18:18:00.706: E/Web Console(17837): Uncaught TypeError: Cannot read property 'SAVEDPHOTOALBUM' of undefined at file:///android_asset/www/index.html:98

07-12 18:17:59.456: E/Web Console(17837): Uncaught ReferenceError: Camera is not defined at file:///android_asset/www/index.html:67

他のすべての宛先タイプを試しました。それはうまくいきません

destinationType: destinationType.FILE_URI
destinationType: Camera.DestinationType.FILE_URI
destinationType: destinationType.DATA_URL

また、カメラの許可とハードウェア カメラの許可を追加しました。しかし、それでも失敗しました

以下はこちら

<!DOCTYPE html>
<html>
<head>
<title>Capture Photo</title>

<script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script>
<script type="text/javascript" charset="utf-8">

var pictureSource;   // picture source
var destinationType; // sets the format of returned value 

// Wait for PhoneGap to connect with the device
//
document.addEventListener("deviceready",onDeviceReady,false);

// PhoneGap 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;
}

// A button will call this function
//
function capturePhoto() {
  // Take picture using device camera and retrieve image as base64-encoded string
  navigator.camera.getPicture(onPhotoDataSuccess, onFail, {destinationType: Camera.DestinationType.FILE_URI, quality: 50 });
}

// A button will call this function
//
function capturePhotoEdit() {
  // Take picture using device camera, allow edit, and retrieve image as base64-encoded string  
  navigator.camera.getPicture(onPhotoDataSuccess, onFail, { destinationType: Camera.DestinationType.FILE_URI,quality: 20, allowEdit: true }); 
}

// A button will call this function
//
function getPhoto(source) {
  // Retrieve image file location from specified source
  navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50, 
    destinationType: Camera.DestinationType.FILE_URI,
    sourceType: source });
}

// Called if something bad happens.
// 
function onFail(message) {
  alert('Failed because: ' + message);
}

</script>
</head>
<body>
<button onclick="capturePhoto();">Capture Photo</button> <br>
<button onclick="capturePhotoEdit();">Capture Editable Photo</button> <br>
<button onclick="getPhoto(pictureSource.PHOTOLIBRARY);">From Photo Library</button><br>
<button onclick="getPhoto(pictureSource.SAVEDPHOTOALBUM);">From Photo Album</button><br>
<img style="display:none;width:60px;height:60px;" id="smallImage" src="" />
<img style="display:none;" id="largeImage" src="" />
</body>
</html>
4

6 に答える 6

2

HTMLに正しい (最新の) cordovaファイルが含まれていることを確認してください。

私は同じ問題を抱えていて、含めていました

cordova-1.8.0.js

それよりも

cordova-2.4.0.js
于 2013-02-11T09:55:37.330 に答える
1

phonegapを使用して写真をキャプチャするためにこれを試すことができます

<script type="text/javascript" charset="utf-8" src="cordova-2.5.0.js"></script>
<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
    //alert(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) {
    alert("inside large image")
    // 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;
}

// A button will call this function
//
function capturePhoto() {
  // Take picture using device camera and retrieve image as base64-encoded string
    navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50,
    destinationType: destinationType.DATA_URL });
}

// A button will call this function
//
function capturePhotoEdit() {
  // 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 });
}

// A button will call this function
//
function getPhoto(source) {
  // Retrieve image file location from specified source
    navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50, 
    destinationType: destinationType.FILE_URI,
    sourceType: source });
}


// Called if something bad happens.
// 
function onFail(message) {
    alert('Failed because: ' + message);
}

</script>
</head>
<body>
<button onclick="capturePhoto();">Capture Photo</button> <br>
<button onclick="capturePhotoEdit();">Capture Editable Photo</button> <br>
<button onclick="getPhoto(pictureSource.PHOTOLIBRARY);">From Photo Library</button><br>
<button onclick="getPhoto(pictureSource.SAVEDPHOTOALBUM);">From Photo Album</button><br>
<img style="display:none;width:60px;height:60px;" id="smallImage" src="" />
<img style="display:none;" id="largeImage" src="" />
</body>
</html>
于 2013-04-25T20:15:08.860 に答える
1

私は3つのステップでそれを解決した同じ問題を抱えていました

JavaScriptが含まれています

私はcordova.jsのみを使用し、phonegap.jsを削除しました

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>

プラグインのインストール

コマンド ラインを使用してカメラ プラグインをインストールする必要があります。xml のみが機能しない

cordova plugin add org.apache.cordova.camera

適切な Android パッケージを使用する

使用している xmlorg.apache.cordova.CameraLauncherが間違っている可能性があり、NullPointer 例外が発生します。適切なパッケージはそのように含めることができます

<feature name="Camera">
    <param name="ios-package" value="CDVCamera" />
    <param name="android-package" value="org.apache.cordova.camera.CameraLauncher" />
</feature>

それらを試してみてください!ありがとう :)

于 2014-12-06T04:03:18.677 に答える
0

私も同じ問題を抱えていました。

pictureSource.PHOTOLIBRARYコードでand をpictureSource.SAVEDPHOTOALBUM直接使用しています。

問題は、javascript の前に html がロードされてから、に到達しようとしていることpictureSourceです。しかし、まだ特定されていません。

アプリが完全に読み込まれたときにこれを使用する必要があります (例: documents.ready で jQuery イベントを使用するか、javascript で代替手段を使用します)。

于 2014-08-28T14:19:25.930 に答える
0


<script src="phonegap.js"></script>
私のhtmlに 追加するだけで解決しました!
また、私も持っていた<script src="cordova.js"></script>ので、取り外して保管してい
<script src="phonegap.js"></script>ました。

于 2013-12-25T07:57:52.460 に答える
0

2.0.0.js これで問題が解決する場合があります

あなたのコードは正しいですこれらの cordova-2.0.0.js ファイルのみをチェックしてください

于 2012-12-17T10:34:17.793 に答える