It's my first use with the Camera dev on Android and I have tested the CameraDemo that I have find at the link https://thenewcircle.com/s/post/39/using__the_camera_api
But I have somes problems with this code :(
First, this seem necessary to add this line into the code of Preview(Context context)
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB)
(the program crash if I don't add this test)
just before this line
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
because the setType() call seem to be now deprecated (that is what say to me Android Studio)
Secondly, this seem necessary to comment the camera.setParameters() call into the SurfaceChanged(SurfaceHolder holder, int format, int w, int h) code
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// Now that the size is known, set up the camera parameters and begin
// the preview.
Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewSize(w, h);
// camera.setParameters(parameters);
camera.startPreview();
(it crash if this line is uncommented)
Now, this example don't crash at startup on my Android devices and I can see the camera preview :)
But it crash now when I rotate the screen :(
=> what is the method for that to handle the screen rotation during the camera preview ?
(since camera.setParameters(parameters) seem deprecated ...)
EDIT : this is now partially resolved :)
I have make this little transformation :
add a boolean variable mPreviewRunning that is is initialised to false at the beginning
add a camera.stopPreview() call when this variable is already set to true
set this variable to true just after the call of camera.startPreview();
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
{
// Now that the size is known, set up the camera parameters and begin
// the preview
// ERROR : setParameter() is deprecated
// Camera.Parameters parameters = camera.getParameters();
// parameters.setPreviewSize(w, h);
// camera.setParameters(parameters);
if( bPreviewRunning == true)
{
bPreviewRunning = false;
camera.stopPreview();
}
camera.startPreview();
bPreviewRunning = true;
}
But the application crash hovewer sometimes but I don't understand why :(
(and the preview is on the bad orientation when in portrait mode)