1

我有一个活动正在启动该服务中的服务我尝试捕获图像并将其保存到 sd 卡。原始代码源在这里告诉我出了什么问题http://easyandroidtutorials.blogspot.in/2012/09/capture-无预览图像 as.html

错误

07-04 08:24:41.579: D/Camera(3036): app passed NULL surface

从活动

Intent service = new Intent(context, CameraService.class);
    context.startService(service); 

服务代码

public class CameraService extends Service
{
//Camera variables
//a surface holder
private SurfaceHolder sHolder; 
//a variable to control the camera
private Camera mCamera;
//the camera parameters
private Parameters parameters;
/** Called when the activity is first created. */
@Override
public void onCreate()
{
    super.onCreate();

}
@Override
public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub
    super.onStart(intent, startId);

    mCamera = Camera.open();
    SurfaceView sv = new SurfaceView(getApplicationContext());


    try {
        mCamera.setPreviewDisplay(sv.getHolder());
        parameters = mCamera.getParameters();

        //set camera parameters
        mCamera.setParameters(parameters);
        mCamera.startPreview();
        mCamera.takePicture(null, null, mCall);

    } catch (Throwable e) {
        e.printStackTrace();
    }


    //Get a surface
    sHolder = sv.getHolder();
    //tells Android that this surface will have its data constantly replaced
    sHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}



Camera.PictureCallback mCall = new Camera.PictureCallback()
{

    public void onPictureTaken(byte[] data, Camera camera)
    {
        //decode the data obtained by the camera into a Bitmap

        FileOutputStream outStream = null;
        try{
            outStream = new FileOutputStream("/sdcard/Image.jpg");
            outStream.write(data);
            outStream.close();
        } catch (Throwable e){
            Log.d("CAMERA", e.getMessage());
        } 
    }
};


@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}
}
4

1 回答 1

0

服务用于应在后台执行的任务。您不能在服务内启动相机,因为相机需要预览表面。

于 2013-07-04T05:36:04.667 回答