我想用相机中的图像进行一些图像处理并将其显示在SurfaceView上,但我不知道如何修改相机框架。我尝试使用setPreviewCallbackWithBuffer和onPreviewFrame,但是它们没有按预期工作,框架没有被修改。
/** A basic Camera preview class */
public class CameraPreview extends SurfaceView implements
SurfaceHolder.Callback, Camera.PreviewCallback {
private SurfaceHolder mHolder;
private Camera mCamera;
private byte[] mData;
private long prevFrameTick = System.currentTimeMillis();
Canvas mCanvas;
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
Size previewSize = mCamera.getParameters().getPreviewSize();
mData = new byte[(int) (previewSize.height * previewSize.width * 1.5)];
initBuffer();
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
private void initBuffer() {
mCamera.addCallbackBuffer(mData);
mCamera.addCallbackBuffer(mData);
mCamera.addCallbackBuffer(mData);
mCamera.setPreviewCallbackWithBuffer(this);
}
public void setCamera(Camera cam) {
mCamera = cam;
initBuffer();
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the
// preview.
try {
mCamera.setPreviewDisplay(holder);
initBuffer();
mCamera.startPreview();
} catch (IOException e) {
Log.d("APP",
"Error setting camera preview: " + e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null) {
// preview surface does not exist
return;
}
// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e) {
// ignore: tried to stop a non-existent preview
}
// set preview size and make any resize, rotate or
// reformatting changes here
// start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
initBuffer();
mCamera.startPreview();
} catch (Exception e) {
Log.d("APP",
"Error starting camera preview: " + e.getMessage());
}
}
public void onPreviewFrame(byte[] data, Camera camera) {
// System.arraycopy(data, 0, mData, 0, data.length);
Log.e("onPreviewFrame", data.length + " "
+ (System.currentTimeMillis() - prevFrameTick));
prevFrameTick = System.currentTimeMillis();
mData = new byte[data.length];
mCamera.addCallbackBuffer(mData);
}
}
答案 0 :(得分:10)
如果您正在使用setPreviewDisplay()调用,则无法修改发送到SurfaceView的预览数据。预览视频流完全在您的应用程序之外进行管理,无法访问。
您可以选择几个选项:
您可以在SurfaceView顶部放置第二个视图,例如ImageView或其他SurfaceView,并将onPreviewFrame回调接收的数据绘制到此视图中。您必须从预览回调格式(通常是NV21)进行一些颜色/像素格式转换才能显示,显然您还必须首先对该数据运行图像处理。这不是很有效,除非你愿意写一些JNI代码。
在Android 3.0或更高版本中,您可以使用Camera.setPreviewTexture()方法,并使用SurfaceTexture对象将相机预览流传输到OpenGL纹理中,然后您可以在OpenGL中操作在显示之前。那么你根本不需要预览回调。如果GPU处理足够,这会更有效。您还可以使用OpenGL readPixels调用将处理后的预览数据恢复到您的应用程序,如果您想以其他方式显示/处理它。