0

I am trying to use camera hardware to capture list of images. But while launching the Camera View the view doesn't looks like usual. The screen shot is attached below. I am testing this in HTC Sensation running android 4.0.3.

Image ScreenShot while in portrait enter image description here It doesn't looks like usual camera view.

What is the problem here?

Where i am doing wrong?

vinothp
  • 9,939
  • 19
  • 61
  • 103

2 Answers2

1

This is the usual problem with making a custom camera with Android in portrait mode. You likely have a SurfaceView that is being used to display your camera preview. I add the following line of code to my surfaceCreated method:

camera.setDisplayOrientation(90);

For more info, check out some other answers on this topic (here is one, and another). Basically, the Android camera is set to display in Landscape mode, so you need to rotate the orientation if the Activity is in portrait mode to accomodate this.

Community
  • 1
  • 1
Daniel Smith
  • 8,561
  • 3
  • 35
  • 58
  • Android cameras have taken over my life :) happy to share. – Daniel Smith Feb 20 '13 at 12:19
  • Thanks Daniel. Any idea about adding buttons to the view – vinothp Feb 20 '13 at 12:44
  • I would like to add capture,add and zoom button in the camera view. What will be the best approach to do this? – vinothp Feb 20 '13 at 12:47
  • 1
    I would probably (full disclosure I'm not too well versed in the lore of layout optimization) throw everything into a big relative layout, and as your first element you'll want an element for placing your camera preview (I usually add my surface view with the camera preview to a FrameLayout as the first element), and below that have another relative layout where you add `Button`s or `ImageButton`s with backgrounds from your drawables. No golden bullet here.. you just have to get your hands dirty with some xml. – Daniel Smith Feb 20 '13 at 12:54
  • Thanks for your suggestion. I will do some changes to my xml. – vinothp Feb 20 '13 at 12:56
1

By default the Hardware Camera is Landscape(almost many devices) , so at-once you capture , i automatically rotates to 90 degree - As if it is taken in the landscape .

Thats why you get the image right when you take in Landscape and 90degree rotated image while taking in Portrait mode .

  • detect the orientation of the device and if it is in portrait , rotate to 90 degree clockwise , so that it would tally when it rotates 90degree anti-clockwise .

Use the below code to detect the orientation ,

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) 
    {
        super.onRestoreInstanceState(savedInstanceState);
        if (savedInstanceState != null) 
        {
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
            {
                // code in portrait - rotate to 90 degree
            }
            else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
            {
                    // code in landscape - do nothing
            }

        }
    }
VIGNESH
  • 2,023
  • 8
  • 31
  • 46