服用从照相机的图像失败的20%的时间 [英] Taking a picture from the camera fails 20% of the time

查看:186
本文介绍了服用从照相机的图像失败的20%的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var camera = {
  settings : {
    quality : 50,
    targetWidth : 1024,
    targetHeight : 1024,
    correctOrientation : true
  }
};
var error = function(message) {
  alert("Error happened while trying to get a picture", message);
};
document.addEventListener("deviceready", function() {
  camera.toFile = function() {
    this.settings.destinationType = navigator.camera.DestinationType.FILE_URI;
    return this;
  },
  camera.toBase64 = function() {
    this.settings.destinationType = navigator.camera.DestinationType.DATA_URL;
    return this;
  },
  camera.fromCamera = function() {
    this.settings.sourceType = navigator.camera.PictureSourceType.CAMERA;
    return this;
  };
  camera.fromLibrary = function() {
    this.settings.sourceType = navigator.camera.PictureSourceType.PHOTOLIBRARY;
    return this;
  };
  camera.fromPhotoAlbum = function() {
    this.settings.sourceType = navigator.camera.PictureSourceType.SAVEDPHOTOALBUM;
    return this;
  }
  camera.get = function(callback) {
    navigator.camera.getPicture(function(data) {
      alert("taking a picture successful");
      callback(data);
    }, error, camera.settings);
  };
}, false);

这是我的小包装的摄像头。我把它叫做是这样的:

This is my small wrapper for the camera. And I call it like this:

camera.fromPhotoAlbum().toBase64().get(function(base64){});

大约有20%的时间里,警报(拍照成功);不叫,而不会显示任何错误。如果我取消拍照,有消息显示警报,而试图让一个图片时发生错误,所以错误回调的工作。

About 20% of the time, the "alert("taking a picture successful");" is not called, while no error is shown. If I cancel taking a picture, an alert with the message "Error happened while trying to get a picture" is shown, so the error callback works.

基本上没有任何反应。我在三星Galaxy S2上CM9和一个全新的HTC One X的测试了。

Basically nothing happens. I've tested it on a Samsung Galaxy S2 on CM9 and a brand new HTC One X.

推荐答案

有<一个href="http://stackoverflow.com/questions/11505127/phonegap-does-not-restore-the-application-after-camera-activity/11544431">another问题最近关于这一点,我回答同样的问题。我们遇到了这个在我的公司,并解决它。它更多的是与Android系统比PhoneGap的。

There was another question recently about this same problem that I answered. We ran into this at my company and solved it. It has more to do with the Android system than Phonegap.

当你启动相机,你的应用程序进入的onStop发生了什么事的是()。在那里,Android系统已经干掉你的应用程序是否需要记忆的权利。它只是恰巧,内存通常变低时,相机拍摄的图片,它转储到内存中,所以这是一个很好的机会,而你的用户需要的图片你的应用程序将被杀死。

What's happening is when you start the camera, your app goes into onStop(). While there, the Android system has the right to kill your app if it needs memory. It just so happens that memory usually gets low when the camera takes a picture and dumps it into memory, so there's a good chance your app will get killed while your user takes a picture.

现在,您的应用程序是死的,当相机完成,将重新启动您的应用程序。这就是为什么它如此行事怪异;相机回来到您的应用程序,但不是同一个实例,它之前,让您的回调不会被调用,因为它已经不存在了。

Now that your app is dead, when the camera finishes, it restarts your app. That's why it's acting so weird; the camera comes back into your app, but not the same instance that it had before, so your callback never gets called, since it doesn't exist anymore.

您可以减少发生这种情况时通过降低图像的质量和传球,而不是数据,你的应用程序它通过URI的频率,但问题也不会完全消失。

You can reduce the frequency at which this occurs by reducing the quality of the picture and passing it by URI instead of data to your app, but the problem won't go away completely.

要解决的回调从来没有发生的事情,我们做了一个Java回调启动相机并保存图片到同一位置每次需要一个时间。然后,当应用程序启动备份从被杀,它看起来在该位置的图象。

To work around the callback never happening, we made a Java callback that starts the camera and saves the picture to the same location every time it takes one. Then, when the app starts back up from getting killed, it looks in that location for the picture.

这是一个奇怪的解决方案,一个愚蠢的问题,但如果你的应用程序就会被杀死,该摄像机的回调根本不会发生。如果您需要关于如何使Java的回调操作的更多信息,请让我知道,我现在就把我们的code在这里。否则,看看<一href="http://stackoverflow.com/questions/2727763/communication-between-android-java-and-phonegap-javascript">this SO回答获取更多的信息。

It's a weird solution to a stupid problem, but if your app gets killed, that camera callback simply won't happen. If you need more information on how to make the Java callback to do this, let me know and I'll put our code up here. Otherwise, take a look at this SO answer for more info.

编辑:这是我们在我们的主要DroidGap活动使用code:

Here's the code we use in our main DroidGap activity:

private static final String folderPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/appName";
private static final String filePath = "phonegapImage.jpg";

@Override
public void onCreate(Bundle savedState) {
    //...The rest of onCreate, this makes the Java available in JavaScript
    appView.addJavascriptInterface(this, "Camera");
}

public void takePhoto(final String callback) {
    Log.v("Camera Plugin", "Starting takePhoto callback");

    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(folderPath, filePath)));
    startActivityForResult(intent, TAKE_PICTURE);
}

public String getPhotoUri() {
    return Uri.fromFile(new File(folderPath, filePath)).toString();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
        case TAKE_PICTURE:
            if (resultCode == Activity.RESULT_OK) {
                //Do whatever you need to do when the camera returns
                //This is after the picture is already saved, we return to the page
            }
            break;
        default:
            Log.v("Camera", "Something strange happened...");
            break;
    }
}

然后,在你的JavaScript,你可以调用摄像头:

Then, in your JavaScript, you can invoke the camera with:

Camera.takePhoto("onPhotoURISuccess");
//Then, to get the location of the photo after you take it and load the page again
var imgPath = Camera.getPhotoUri();

所以,仅此而已。只要确保路径/文件/网页/等名称的改变所有你想要在你的应用程序来使用的。这将拍摄照片时,每次覆盖的形象,但你也许可以想办法来动态地为它们命名,如果你不希望出现这种情况。您可以使用URI就像你在你的JavaScript其他任何路径。

So, that's about it. Just make sure to change all of the path/file/page/etc names to what you want to use in your app. This will overwrite that image every time a picture is taken, but you can probably figure something out to dynamically name them if you don't want that. You can use that URI just as you would any other path in your JavaScript.

这篇关于服用从照相机的图像失败的20%的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆