此单击图像作为服务的代码在某些方面非常有效:三星聊天,但不适用于:HTC One,Google Nexus。在调用takepicture时抛出RuntimeException
。给我一些建议,我在这个实现中出错了。
public class cs extends Service implements SurfaceHolder.Callback{
private SurfaceView sv;
boolean previewing = false;
private SurfaceHolder sHolder;
private Camera mCamera=null;
private Parameters parameters;
@SuppressLint("NewApi")
public cs( Context cc) {
mCamera = Camera.open(); //using default camera
sv = new SurfaceView(cc);
try {
mCamera.startPreview();
mCamera.takePicture(null, null, mCall);
} catch (Exception e) {
e.printStackTrace();
}
sHolder = sv.getHolder();
sHolder.addCallback(this);
sHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mCamera.release();
}
Camera.PictureCallback mCall = new Camera.PictureCallback()
{
public void onPictureTaken(byte[] data, Camera camera)
{
FileOutputStream outStream = null;
try{
outStream = new FileOutputStream("/sdcard/Image.jpg");
outStream.write(data);
outStream.close();
} catch (FileNotFoundException e){
Log.d("CAMERA", e.getMessage());
} catch (IOException e){
Log.d("CAMERA", e.getMessage());
}
}
};
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
if(previewing){
System.out.println("stop previewing");
mCamera.stopPreview();
previewing = false;
}
}
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
public void surfaceDestroyed(SurfaceHolder holder) {
System.out.println("camera stop previewing");
mCamera.stopPreview();
mCamera.release();
mCamera = null;
previewing = false;
}
}