Reputation: 1
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)
Upvotes: 0
Views: 6790
Reputation: 709
Here Google official example for Camera API 2:
https://github.com/googlesamples/android-Camera2Basic
New library develop by google engineers, to help you easily support Camera API 1 and Camera API 2 (be aware, that not yet completely stable, and this is not a official example) :
https://github.com/google/cameraview
Upvotes: 0
Reputation: 57173
Here are few examples:
https://stackoverflow.com/a/19312182/192373
https://stackoverflow.com/a/20883662/192373
https://stackoverflow.com/a/19599599/192373
Choose the one that suits your needs (changing camera orientation, or keeping the activity fixed, or whatever).
Upvotes: -1