0

I'm writing a custom camera application which follow this tutorial: http://marakana.com/forums/android/examples/39.html

Now I'm able to use the camera and get the image data after user click a button. But I got the problem that the preview is sideway so I add the following code in surfaceCreated

@Override
public void surfaceCreated(SurfaceHolder holder) {
    //  Open the camera when the preview is created
    Log.d("Callback", "[surfaceCreated]");
    theCamera = Camera.open();

    Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
    int displayRotation = display.getRotation();
    switch (displayRotation)  {
        case Surface.ROTATION_0:        theCamera.setDisplayOrientation(90);    break;
        case Surface.ROTATION_90:       break;
        case Surface.ROTATION_180:      break;
        case Surface.ROTATION_270:      theCamera.setDisplayOrientation(180);   break;
    }
}

The preview was fine. However when I got the image data, I use BitmapFactory.decodeByteArray to get the bitmap, and set it to my imageView that is defined in my xml layout.

Here is my main.xml

<SurfaceView
    android:id="@+id/previewSurfaceView"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_gravity="center" />

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="250px" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="Button" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center_vertical"
        android:layout_weight="1"
        android:maxHeight="100dp"
        android:maxWidth="100dp"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="fill_parent"
    android:layout_height="250px" >

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="Button" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center_vertical"
        android:layout_weight="1"
        android:maxHeight="100dp"
        android:maxWidth="100dp"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

previewSurfaceView is the small view that for the camera preview

button1 and button2 are the button when I clicked on, the camera capture the image and set the imagedata corresponding to imageView1/imageView2

There are some problems:

  1. I set the layout width and height and also the max width and height to 100dp, also the scale type to fitCenter. But it seems not to display as what I want. I want the image is show inside the bound. Am I doing wrong in the layout setting?

  2. The image is always in landscape even I took the picture in portrait mode.

Besides, I want to add a button to toggle the on/off of camera but I can't figure out how to do this.

Can somebody suggest the solution for me? Thank you.

Hanon
  • 3,917
  • 2
  • 25
  • 29

2 Answers2

0

Have a look at my answer here as it appears to be the same issue you are having.

Community
  • 1
  • 1
John J Smith
  • 11,435
  • 9
  • 53
  • 72
  • Sorry, I posted wrong code above. I have no problem in the surface view. But how can I get the orientation of the image data. It seems that I cant get the EXIF from the data – Hanon Feb 07 '12 at 08:37
0

You need to call the method setRotation on the camera parameter object and then set the parameter on the camera.

Implement the OrientationListener to get the rotation use that value in setRotation

Sample code usage here.

bluefalcon
  • 4,225
  • 1
  • 32
  • 41
  • I tried it with hardcode to 0, 90, 180 and 270. But the image data is still always in landscape. I want the image is always taken in portrait mode and display it as portrait mode too – Hanon Feb 07 '12 at 09:06
  • Hold the device in potrait mode and set the rotation as 90 and you should get a picture in portrait mode. – bluefalcon Feb 07 '12 at 09:14