3

Phonegap とカメラ機能の使用について、ここで多くの質問があることは承知しています。問題を解決しようとしましたが、まだ解決策がありません。

デバイスは Nexus 7 で、アプリのビルド後にカメラが起動しません。コードは次のとおりです。

<script type="text/javascript" src="PhoneGap.js" charset="utf-8"></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, { 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, { 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: 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>

サムスンギャラクシーs2で試してみました...もう一度何も起こりませんでした! 問題がわからない!? コードは次のとおりです。

androidmainfest.xml 権限:

<uses-sdk android:minSdkVersion="14" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.RECORD_VIDEO"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />   
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />   
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

そしてここにhtmlファイルからのコード:

<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 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, { 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, { 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: 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

3 に答える 3

0

ドキュメントに記載されているように、オプションを提供する必要があります。例:

navigator.camera.getPicture(onSuccess, onFail, { quality: 50,
    destinationType: Camera.DestinationType.DATA_URL
 }); 

参照: http://docs.phonegap.com/en/2.5.0/cordova_camera_camera.md.html#camera.getPicture

于 2013-04-03T10:31:15.950 に答える
0

モバイル メモリが不足している可能性があります。この問題は実際には Phonegap に関するものではありません。これは、ネイティブ Android アプリでもよくある問題です。カメラがトリガーされると、Android アクティビティがバックグラウンド (onStop 状態) になり、カメラが写真を撮るのを待つために発生します。次に、GC が来てアクティビティを強制終了し、カメラ アクションが終了する前にメモリを解放します。カメラが完了すると、アクティビティは既に終了しています。そのため、アプリを再起動します。

これが私の提案です

1 .cramer プラグインを置き換えます。カスタム プラグインの使用を避け、ガベージ コレクションを開始します ( http://code.google.com/p/foreground-camera-plugin/http://code.google.com/p/foreground- gallery- plugin /) 2. 他のプロセスを強制終了するには不十分な場合にメモリを検出し、メモリをアクティブに解放します。

    private void clearMemory(boolean killAll)
{
    mklog("当前系统可用内存大小是:" + getAvailMemory(getApplicationContext()));
    ActivityManager activityManger = (ActivityManager) this
            .getSystemService(ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> list = activityManger
            .getRunningAppProcesses();
    if (list != null)
        for (int i = 0; i < list.size(); i++)
        {
            ActivityManager.RunningAppProcessInfo apinfo = list.get(i);

            System.out.println("pid            " + apinfo.pid);
            System.out.println("processName              "
                    + apinfo.processName);
            System.out
                    .println("importance            " + apinfo.importance);
            String[] pkgList = apinfo.pkgList;

            if (apinfo.importance >= ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND
                    || (killAll && apinfo.importance >= ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE))
            {
                // Process.killProcess(apinfo.pid);
                for (int j = 0; j < pkgList.length; j++)
                {
                    activityManger.killBackgroundProcesses(pkgList[j]);
                    mklog("准备杀死进程:" + pkgList[j]);
                }
            }

        }

    mklog("清理之后 当前系统可用内存大小是:" + getAvailMemory(getApplicationContext()));
}

private long getAvailMemory(Context context)
{
    ActivityManager am = (ActivityManager) context
            .getSystemService(Context.ACTIVITY_SERVICE);
    MemoryInfo mi = new MemoryInfo();
    am.getMemoryInfo(mi);
    return mi.availMem / (1024 * 1024);
}   
      public void mklog(String contentString)
{
    Log.i("Web Console", contentString);
}
于 2013-09-13T04:01:30.380 に答える
0

今では動作します。問題はわかりませんが、動作します。コードは次のとおりです。

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

    <link rel="stylesheet" href="css/style.css" />

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

function capturePhoto(){
    navigator.camera.getPicture(uploadPhoto,null,{sourceType:1,quality:60});
}

function uploadPhoto(data){
    // this is where you would send the image file to server
    //output image to screen
        cameraPic.src = "data:image/jpeg;base64," + data;
    }

navigator.notification.alert(message, alertCallback, [title], [buttonName]);

navigator.notification.alert(
        'Your Photo has been uploaded', // message
        okay,                           // callback
        'Photo Uploaded',               // title
        'OK'                            // buttonName
    );

function uploadPhoto(data){
    // this is where you would send the image file to server
        cameraPic.src = "data:image/jpeg;base64," + data;
        // Successful upload to the server
        navigator.notification.alert(
            'Your Photo has been uploaded',  // message
            okay,                           // callback
            'Photo Uploaded',              // title
            'OK'                          // buttonName
        );
        // upload has failed Fail
        /*
        if (failedToUpload){
        navigator.notification.alert(
            'Your Photo has failed to upload',
            failedDismissed,
            'Photo Not Uploaded',
            'OK'
            );
        }
        */
    }
    function okay(){
        // Do something
    }

    </script>
  </head>
  <body>
<div id="camera">
    <button class="camera-control" onclick="capturePhoto();">Capture Photo</button>
    <div style="text-align:center;margin:20px;">
        <img id="cameraPic" src="" style="width:auto;height:120px;"></img>
    </div>
</div>
  </body>
</html>
于 2013-04-10T12:52:12.943 に答える