我正在尝试从onPreviewFrame(byte []数据,相机相机)获取数据。但它似乎没有奏效。你看到这段代码有什么问题吗?大部分内容直接来自http://developer.android.com/training/camera/cameradirect.html
public class UAVCamera extends Thread {
Camera mCamera;
CameraPreview mPreview;
public UAVCamera(Context context) {
mPreview = new CameraPreview(context);
}
@Override
public void run() {
safeCameraOpen();
mPreview.setCamera(mCamera);
}
private boolean safeCameraOpen() {
boolean qOpened = false;
try {
releaseCameraAndPreview();
mCamera = Camera.open();
qOpened = (mCamera != null);
} catch (Exception e) {
//Log.e(getString(R.string.app_name), "failed to open Camera");
e.printStackTrace();
}
return qOpened;
}
private void releaseCameraAndPreview() {
mPreview.setCamera(null);
if (mCamera != null) {
mCamera.release();
mCamera = null;
}
}
}
这是我的预览课程
class CameraPreview extends ViewGroup implements SurfaceHolder.Callback {
SurfaceView mSurfaceView;
SurfaceHolder mHolder;
Camera mCamera;
CameraPreview(Context context) {
super(context);
mSurfaceView = new SurfaceView(context);
addView(mSurfaceView);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = mSurfaceView.getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
private void stopPreviewAndFreeCamera() {
if (mCamera != null) {
/*
Call stopPreview() to stop updating the preview surface.
*/
mCamera.stopPreview();
/*
Important: Call release() to release the camera for use by other applications.
Applications should release the camera immediately in onPause() (and re-open() it in
onResume()).
*/
mCamera.release();
mCamera = null;
}
}
public void setCamera(Camera camera) {
if (mCamera == camera) { return; }
stopPreviewAndFreeCamera();
mCamera = camera;
if (mCamera != null) {
try {
mCamera.setPreviewDisplay(mHolder);
} catch (IOException e) {
e.printStackTrace();
}
/*
Important: Call startPreview() to start updating the preview surface. Preview must
be started before you can take a picture.
*/
mCamera.startPreview();
mCamera.setPreviewCallback(new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera camera) {
}
});
}
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
@Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
// TODO Auto-generated method stub
}
}
这是我的活动
public class MainActivity extends Activity {
private UAVCamera uavCamera;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uavCamera = new UAVCamera(this);
uavCamera.start();
}
}
我甚至不需要向用户显示图片。我只需要获取数据。有可能吗?我在onPreviewFrame(byte []数据,相机相机)上设置了一个断点,但它永远不会被击中。
答案 0 :(得分:0)
你应该使用
setPreviewCallbackWithBuffer (Camera.PreviewCallback cb)
代替setPreviewCallback
。
并使用addCallbackBuffer(byte[])
使用addCallbackBuffer(byte[])
时,字节长度大于预览大小(宽度*高度)。
然后,我认为您可以使用onPreviewFrame
方法获取数据。
答案 1 :(得分:0)
我按照http://developer.android.com/guide/topics/media/camera.html#custom-camera上的说明操作 问题是我没有创建活动。我想将此作为服务运行,即使屏幕未打开也能获取此信息。不知道怎么做。