PictureCallback.onPictureTaken 从未调用 [英] PictureCallback.onPictureTaken never called
问题描述
我正在编写一个需要用相机拍摄照片的应用程序.当我尝试拍摄实际照片时会出现问题.这是困扰我的代码:
I am writing an application that needs pictures taken with the camera. The problem occurs when I try to take an actual picture. Here's the code that is troubling me:
final ShutterCallback shutterCallback = new ShutterCallback() {
@Override
public void onShutter() {
Log.d(TAG, "onShutter");
}
};
final PictureCallback callback = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Log.d(TAG, "onPictureTaken - jpeg");
try {
//async task for storing the photo
new SavePhotoTask(CameraView.this.ctx, data).execute();
} catch (final SavePhotoException e) {
//some exceptionhandling
}
}
};
this.camera = Camera.open();
this.camera.setPreviewDisplay(surfaceHolder);
final Camera.Parameters parameters = findBestParameters(w, h);
this.camera.setParameters(parameters);
this.camera.startPreview();
Log.d(TAG, "takePicture now!");
this.camera.takePicture(shutterCallback, null, callback);
在模拟器上它似乎可以解决,但在我的手机上(摩托罗拉 Defy - android 2.1).
On the emulator it seems to work out but on my phone (Motorola Defy - android 2.1).
实际问题:在电话上永远不会发生图片回调onPictureTaken"永远不会被调用.Shuttercallback 已执行,但另一个未执行(我尝试使用 raw 而不是 jpeg,同样的事情).
The actual problem: on the phone the picturecallback is never happening "onPictureTaken" gets never called. The Shuttercallback is executed but the other is not (and I tried with raw instead of jpeg, same thing).
有人知道这个问题吗?我只是不明白现在模拟器的区别在哪里.感谢您的帮助.
Does anyone know this problem? I just don't see where the difference lies to the emulator right now. I appreciate your help.
推荐答案
我终于去调试问题了.突然间它起作用了,因为调试要慢得多:这是一个时间问题.回调需要一些时间才能被调用.一边调试手机,一边有足够的时间拍完照片……
I finally went and debugged the problem. All of a sudden it worked, because debugging is much slower: It's a timing problem. The callback takes some time to be called. While debugging the phone had enough time to finish taking the picture ...
也不要过早调用 Camera.stopPreview() 和 Camera.release().
Also don't go calling Camera.stopPreview() and Camera.release() too early.
这篇关于PictureCallback.onPictureTaken 从未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!