我正在开发类似相机应用的东西。它可以在一个原生OpenGL纹理上绘制实时摄像头预览。如果按下按钮,则以其原始形式捕获静态图像(可能比实时预览更高分辨率)。
这里是我的代码概述。为了清晰起见,我没有展示不太相关的部分。这是一个简化的情况,其中预览和仍然捕获的分辨率是相同的。
void Setup(Activity a) {
// .. snip ..
mSurfaceView = new SurfaceView(a);
mSurfaceView.getHolder().addCallback(this);
//put the surface off-screen
AbsoluteLayout.LayoutParams alp = new AbsoluteLayout.LayoutParams(8, 8, -8, -8);
a.addContentView(mSurfaceView, alp); //need this, otherwise surface is not created
mCamera = Camera.open(miCameraId);
mCamera.setPreviewCallbackWithBuffer(this);
ConfigureCameraDefaults(640,480);
}
// -------------------------------------------------------------------- //
void ConfigureCameraDefaults(int iX, int iY) {
Camera.Parameters oParameters=mCamera.getParameters();
oParameters.setPreviewFormat(ImageFormat.NV21);
oParameters.setPictureFormat(ImageFormat.NV21);
oParameters.setPreviewSize(iX, iY); // for live preview
oParameters.setPictureSize(iX, iY); // for still capture
int bpp = ImageFormat.getBitsPerPixel(ImageFormat.NV21);
int iBufSize = (int) (iX*iY*((float)bpp/8));
mBuffer = new byte[iBufSize];
mCamera.addCallbackBuffer(mBuffer);
try {
mCamera.setParameters(oParameters);
} catch (Exception e) {
// .. snip ..
}
}
void startPreview() {
mCamera.startPreview();
}
void stopPreview() {
mCamera.stopPreview();
}
void takePicture() {
mCamera.takePicture(null, this, null, null); //2nd is the raw callback
}
// -------------------------------------------------------------------- //
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
// -- at this point the buffer is in a safe state, until it is added again
onPreviewFrame_native(data);
// -- make buffer writable again
mCamera.addCallbackBuffer(mBuffer);
}
@Override
public void onPictureTaken(byte[] data, Camera camera) {
onStillCapture_native(data);
mCamera.startPreview(); //otherwise preview will stop
}
好的,现在是问题时间。当没有调用
takePicture()
时,一切都按计划进行。预览帧到达,它们被处理,缓冲区重新排队。然而,如果我先调用startPreview()
再调用takePicture()
,那么每次拍照回调都会得到一个空的数据数组。谷歌文档对takePicture()
有以下说明:
原始回调发生在原始图像数据可用时(注意:如果没有可用的原始图像回调缓冲区或原始图像回调缓冲区不足以容纳原始图像,则数据将为空)。
这对我来说并不完全清楚,因为:
- 没有函数设置静态捕获的缓冲区,只有用于预览帧的缓冲区
- 即使相同的缓冲区用于静态捕获和预览帧,我也有一个排队的缓冲区。
- 添加第二个缓冲区(与预览缓冲区相同)专门用于静态捕获没有任何效果
- 必须启动预览才能使
takePicture()
成为有效调用。
非常感谢任何帮助。
PS. 我已经尝试使用不同的颜色格式进行静态捕获、在拍照之前取消预览回调、添加第二个缓冲区、重新排队第二个缓冲区、将表面持有者设置为推送类型以及其他一些我此刻无法回忆起来的事情。但都没有任何影响。