Android Camera在takePicture上禁用stopPreview [英] Android Camera disable stopPreview on takePicture
问题描述
我正在尝试构建一个使用相机API的应用程序,以便在10秒钟内每秒拍摄一张照片. 我在此链接 Android Camera API 中遵循了教程,并修改了代码以获得我的照片列表(请参见下面的代码).一切都很好...
I'm trying to build an app which uses the camera API to take one picture every second for 10 seconds. I followed the tutorial in this link Android Camera API and modified the code to get my list of pictures (see code below). Everything worked perfectly...
现在,问题是(我想)在上,因为它停止了预览,因为我要在takePicture方法上,我需要在onPictureTaken回调中再次启动它屏幕冻结.
Now, the problem is (I suppose) on takePicture method because it stops the preview, I need to start it again in the callback onPictureTaken which causes a little moment of screen freeze.
private void initializeCamera() {
// Create an instance of Camera
mCamera = getCameraInstance();
// Create our Preview view and set it as the content of our activity.
CameraPreview mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = findViewById(R.id.camera_preview);
preview.addView(mPreview);
mCamera.setPreviewCallback(new Camera.PreviewCallback() {
@Override
public void onPreviewFrame(byte[] bytes, Camera camera) {
startRecognition();
}
});
}
private PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
mCamera.startPreview(); // <----- Restart preview.. stop freeze
// Task to detect an object in the picture.. Do something
}
};
public void startRecognition() {
if (SystemClock.elapsedRealtime() - startedTime > 10000) {
// Detection has failed.. Do something
}
else {
// Get a frame each second
if (SystemClock.elapsedRealtime() - elapsedTime > 1000) {
elapsedTime = SystemClock.elapsedRealtime();
mCamera.takePicture(null, null, mPicture); // <---- Take picture but stop preview
}
}
}
来自Android Camera文档:
From Android Camera doc:
8)拍照后,预览显示将停止.要拍摄更多照片,请先再次调用startPreview().
8) After taking a picture, preview display will have stopped. To take more photos, call startPreview() again first.
是否有一种方法可以禁用stopPreview ,或者我在拍照时使用此方法可以执行的其他任何操作?
Is there a way to disable the stopPreview, or anything else that this method does, when I take the picture?
谢谢您的帮助.
推荐答案
也许对您来说,onPreviewFrame()中提供的分辨率就足够了吗?然后,无需在拍照" 之后重新启动相机.实时预览不会冻结.
Maybe for you the resolution delivered in onPreviewFrame() can be enough? Then, there is no need to restart the camera after 'taking a picture'. The live preview will not freeze.
如果您使用API> = 21(Lollipop)定位设备,则应使用新的 camera2 API而不是已弃用
If you target devices with API >= 21 (Lollipop), you should use the new camera2 API instead of the deprecated Camera API. The new API has many improvements, and among them - it can help with smooth multi-image capture.
即使您坚持使用旧的API,也需要进行一些改进.
Even if you are stuck with old API, there are some improvements to make.
现有代码的问题之一是,它可与UI线程上的摄像头设备一起使用.而是使用后台HandlerThread 打开摄像机,并确保onPictureTaken()
重新启动预览并立即返回,将所有处理工作卸载到另一个工作线程.
One of the problems with your existing code is that it works with the camera device on UI thread. Rather, use a background HandlerThread to open the camera, and also make sure that onPictureTaken()
restarts preview and returns iommediately, offloading all processing to yet another worker thread.
这篇关于Android Camera在takePicture上禁用stopPreview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!