1

I'm implementing a camera app which will be contained within a larger, main application (the main application will utilize said camera app, instead of relying on the native one).

I've managed to get it working - I used Google's Camera Documentation as the basis for the implementation (so it's essentially the same code). The only difference is that, onPictureTaken(), the only thing I'm doing is saving the raw data byte array into a static field (so the main application can access the raw data; I don't want to save the photo locally).

The problem I'm having is that, if I rotate the phone (change orientation) from within the camera activity, everything appears to be working, but I never get anything: no data, no picture, nothing.

If I don't rotate the phone, everything works fine; if I rotate the phone BEFORE starting the camera activity, again, everything works as expected.

I'm guessing it has something to do with the activity being recreated when the orientation changes, but there aren't any objects that I can think of that need saving; the camera object is retrieved by camera.getInstance(), which I'm always doing in onCreate().

I'm using ActionBarSherlock.

public class CamActivity extends SherlockActivity {

    private Camera cam;
    private CameraPreview mPreview;
    private PictureCallback data= new PictureCallback() { 
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            CamController.RAW_PIC = data;
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.camera_view);

        cam= CamController.getCameraInstance();

        mPreview = new CamSurface(this, camera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.preview);
        preview.addView(mPreview);


        Button snapshot = (Button) findViewById(R.id.snapshot);
        snapshot.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                cam.takePicture(null, null, data);
            }
        });
     }
}

When orientation changes, I'm doing this (seems to be the only thing that correctly aligns the SurfaceView):

        Camera.Parameters parameters = camera.getParameters();
        Display display = activity.getWindowManager().getDefaultDisplay();
        int rotation = activity.getResources().getConfiguration().orientation;
        if (display.getRotation() == Surface.ROTATION_0) {

                if (rotation == Configuration.ORIENTATION_LANDSCAPE) {
                    parameters.setPreviewSize(h, w);
                    camera.setDisplayOrientation(0);
                } else {
                    parameters.setPreviewSize(h, w);
                    camera.setDisplayOrientation(90);
                }
            }

            else if (display.getRotation() == Surface.ROTATION_90) {
                if (rotation == Configuration.ORIENTATION_PORTRAIT) {
                    parameters.setPreviewSize(w, h);
                    camera.setDisplayOrientation(270);
                } else {
                    parameters.setPreviewSize(w, h);
                }
            }

            else if (display.getRotation() == Surface.ROTATION_180) {
                if (rotation == Configuration.ORIENTATION_LANDSCAPE) {
                    parameters.setPreviewSize(h, w);
                    camera.setDisplayOrientation(180);
                }else {
                    parameters.setPreviewSize(h, w);
                    camera.setDisplayOrientation(270);
                }
            }

            else if (display.getRotation() == Surface.ROTATION_270) {
                if (rotation == Configuration.ORIENTATION_PORTRAIT) {
                    parameters.setPreviewSize(w, h);
                    camera.setDisplayOrientation(90);
                } else {
                    parameters.setPreviewSize(w, h);
                    camera.setDisplayOrientation(180);
                }
            }
cintron
  • 434
  • 4
  • 16

1 Answers1

0

Make sure your activity is having the below code in the Manifest file.

<activity
            android:name="XXXActivity"
            android:configChanges="orientation|screenSize|keyboard">
        </activity>
Noundla Sandeep
  • 3,289
  • 5
  • 26
  • 55
  • Tried it, and if I start in portrait mode and switch to landscape, the preview goes dark, cant't see anything. The activity seems to be running though (I can rotate back to portrait and get back the preview). – cintron Jan 30 '14 at 15:23