减慢相机对象的 setPreviewCallback 方法
我在相机对象(mCamera)上运行以下代码来检测相机所指向的任何人脸。问题是,虽然这个过程非常快,但它确实减慢了应用程序的其余部分的速度,即当我在面部检测运行时按下按钮执行某些操作时,需要大约半秒的时间来注册它。我的想法是,我以某种方式减慢或减少它在特定时间内进行的回调次数,然后按此按钮会快得多:
Thread thread = new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
mCamera.setPreviewCallback(new PreviewCallback() {
public void onPreviewFrame(final byte[] _data, Camera _camera) {
Camera.Parameters parameters = mCamera.getParameters();
int format = parameters.getPreviewFormat();
// YUV formats require more conversion
if (format == ImageFormat.NV21) {
int w = parameters.getPreviewSize().width;
int h = parameters.getPreviewSize().height;
// Get the YuV image
YuvImage yuv_image = new YuvImage(_data, format, w, h, null);
// Convert YuV to Jpeg
Rect rect = new Rect(5 * w / 10, 1 * h / 5, 8 * w / 10, 4 * h / 5);
final ByteArrayOutputStream output_stream = new ByteArrayOutputStream();
yuv_image.compressToJpeg(rect, 10, output_stream);
// Convert from Jpeg to Bitmap
Bitmap bmp = Bitmap.createScaledBitmap(BitmapFactory.decodeByteArray(output_stream.toByteArray(), 0, output_stream.size()), 3 * w / 10, 3 * h / 5, true);
detectFaces(bmp, w, h);
}
}
});
}
});
thread.start();
它已经在线程内,因为我认为这可能会有所帮助,但实际上并没有。有没有办法改变回电频率?
I have the following code running on a camera object (mCamera) to detect faces in whatever the camera is pointing at. The problem is while this process is very fast indeed it slows the rest of the app down, i.e. when I press a button to do something while this face detection is running it takes about half a second to register it. My thinking is that is I somehow slow down or reduce the number of call back it makes in a certain time then this button press will be much faster:
Thread thread = new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
mCamera.setPreviewCallback(new PreviewCallback() {
public void onPreviewFrame(final byte[] _data, Camera _camera) {
Camera.Parameters parameters = mCamera.getParameters();
int format = parameters.getPreviewFormat();
// YUV formats require more conversion
if (format == ImageFormat.NV21) {
int w = parameters.getPreviewSize().width;
int h = parameters.getPreviewSize().height;
// Get the YuV image
YuvImage yuv_image = new YuvImage(_data, format, w, h, null);
// Convert YuV to Jpeg
Rect rect = new Rect(5 * w / 10, 1 * h / 5, 8 * w / 10, 4 * h / 5);
final ByteArrayOutputStream output_stream = new ByteArrayOutputStream();
yuv_image.compressToJpeg(rect, 10, output_stream);
// Convert from Jpeg to Bitmap
Bitmap bmp = Bitmap.createScaledBitmap(BitmapFactory.decodeByteArray(output_stream.toByteArray(), 0, output_stream.size()), 3 * w / 10, 3 * h / 5, true);
detectFaces(bmp, w, h);
}
}
});
}
});
thread.start();
It is already inside a Thread as I thought that may help but has not really. Is there anyway to change the call back frequency?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我已经做了一些工作来实时进行人脸检测,以下是我在此过程中学到的一些东西:
希望其中一些有所帮助。
I've done some work getting face detection to happen in real time and here's a few thing's I've learned along the way:
Hope some of that helps.
您可以尝试在
onPreviewFrame
方法中插入一些Thread.sleep()
调用,以便代码的其他部分也获得一些 CPU 时间。上面的代码看起来可能会在 onPreview 代码中耗尽几乎所有的 cpu 时间。您可能需要调整睡眠时间,因为睡眠过多会降低预览速率。
You may try to insert some
Thread.sleep()
calls in theonPreviewFrame
method, so that other parts of the code also get some cpu time. The code above looks like it may use up almost all cpu time in the onPreview code.You may need to play with the sleep time, as too much sleeping will turn the preview rate way down.
您可以查看 setOneShotPreviewCallback。
You could have a look at setOneShotPreviewCallback.
以下方法的文档
请检查 Camera.Parameters getSupportedPreviewFpsRange
获取预览帧率范围
在我的 Nexus 上设置PreviewFpsRange
,我有 3 个可能的预览 FPS
最小 15 帧/秒,最大 15 帧/秒
最小 15 帧/秒,最大 30 帧/秒
最小 24 fps,最大 30 fps
注意:
getSupportedPreviewFpsRange 的返回值按 1000 缩放——我不知道为什么。
我不确定这是否有效,因为我测量了两次调用 onPreviewFrame 之间的时间,并且我得到了 fps 1000....也许我的测量中有错误。
关于 Camera.Parameters 的 Android 官方文档
Please check documentation of following methods of Camera.Parameters
getSupportedPreviewFpsRange
getPreviewFpsRange
setPreviewFpsRange
on my Nexus, i have 3 possible FPS of preview
min 15fps, max 15fps
min 15fps, max 30fps
min 24fps, max 30fps
Attention :
getSupportedPreviewFpsRange's return values are scaled by 1000 -- I have no idea why.
I am not sure this works as I measured the time between two call of onPreviewFrame, and I got a fps 1000.... maybe there is a mistake in my measurement .
Android Offical Doc on Camera.Parameters