blackjack
blackjack

Reputation: 585

Screen Oreintation with custom camera in samsung mobile

in my app requirement for custom camera so developing one, upto that ok everything working fine getting the picture also but when i take picture with my Samsung galaxy ace android phone(2.3) on landscape mode its ok but while going to portrait mode the view of the picture move to left side with 90 degree angle.As you can see in my code i had tried to change the surface position also but nothing happen. Here is the code for Myactivity class

package com.goutam.test_camera;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

public class CameraclassActivity extends Activity implements OnClickListener,
        PictureCallback {
    ImageButton take_picture, retake_button, save_picture, setting_button;
    FrameLayout camerapreviewsurface;
    Previewclass cameradisplayclass;
    ImageView capture_image;
    HorizontalScrollView myscrollview;
    private Camera myowncamera;

    byte[] finalimage;
    Bitmap pic_cap_bitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_portrait);
        take_picture = (ImageButton) findViewById(R.id.imageButton1);
        //
        retake_button = (ImageButton) findViewById(R.id.imageButton3);
        //
        save_picture = (ImageButton) findViewById(R.id.imageButton4);
        //
        setting_button = (ImageButton) findViewById(R.id.imageButton2);
        //
        capture_image = (ImageView) findViewById(R.id.imageView1);
        //
        myscrollview = (HorizontalScrollView) findViewById(R.id.horizontalScrollView1);
        //
        take_picture.setOnClickListener(this);
        retake_button.setOnClickListener(this);
        save_picture.setOnClickListener(this);
        //
        retake_button.setVisibility(View.INVISIBLE);
        save_picture.setVisibility(View.INVISIBLE);
        setting_button.setVisibility(View.INVISIBLE);
        capture_image.setVisibility(View.INVISIBLE);
        myscrollview.setVisibility(View.INVISIBLE);
        //
        myowncamera = Camera.open();
        camerapreviewsurface = (FrameLayout) findViewById(R.id.framelayout1);
        cameradisplayclass = new Previewclass(this, myowncamera);
        camerapreviewsurface.addView(cameradisplayclass);
        Log.i("ACTIVITY", "ON CREATE");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.imageButton1:
            takePicture();
            break;
        }

    }

    private void takePicture() {
        // TODO Auto-generated method stub
        take_picture.setVisibility(View.INVISIBLE);
        cameradisplayclass.takepicture(this);

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        // setContentView(R.layout.layout_portrait);
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        // TODO Auto-generated method stub
        finalimage = data;
        // camera.stopPreview();
        this.myowncamera = camera;
        retake_button.setVisibility(View.INVISIBLE);
        camerapreviewsurface.setVisibility(View.INVISIBLE);
        setting_button.setVisibility(View.VISIBLE);
        capture_image.setVisibility(View.VISIBLE);
        Toast.makeText(getApplicationContext(), "PICTURE TAKEN", 0).show();
        new DisplayImageTask().execute(finalimage);
    }

    private class DisplayImageTask extends AsyncTask<byte[], Void, Bitmap> {
        @Override
        protected Bitmap doInBackground(byte[]... parameter) {
            pic_cap_bitmap = BitmapFactory.decodeByteArray(finalimage, 0,
                    finalimage.length);
            return pic_cap_bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap result) {
            capture_image.setImageBitmap(pic_cap_bitmap);
            super.onPostExecute(result);
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            Toast.makeText(getApplicationContext(), "DISPLAYING IMAGE", 0)
                    .show();
            super.onProgressUpdate(values);
        }

    }

    // @Override
    // public boolean onCreateOptionsMenu(Menu menu) {
    // // Inflate the menu; this adds items to the action bar if it is present.
    // getMenuInflater().inflate(R.menu.layout_portrait, menu);
    // return true;
    @Override
    protected void onPause() {
        Log.i("ACTIVITY", "ON PAUSE");
        System.gc();
        this.finish();
        super.onPause();
    }

    @Override
    protected void onResume() {

        super.onResume();
    }

    @Override
    public void onBackPressed() {
        System.gc();
        this.finish();
        myowncamera.release();
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        this.finish();
        System.gc();
        super.onDestroy();
    }
}

and here is the code for the preview class for my surfaceholder to hold the surface view

package com.goutam.test_camera;

import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PictureCallback;
import android.util.Log;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.Surface;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

public class Previewclass extends SurfaceView implements Callback {
    private Camera mcamera;
    boolean ispreviewing = false;
    // WindowManager manager;
    SurfaceHolder holder;

    public Previewclass(Context context, Camera mycamera) {

        super(context);
        this.mcamera = mycamera;
        // manager=(WindowManager)
        // context.getSystemService(Context.WINDOW_SERVICE);
        holder = this.getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        // mcamera=null;
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        Log.i("CAMERA", "ON SURFACE CHANGE");

        try {
            mcamera.setPreviewDisplay(this.holder);
            mcamera.startPreview();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // if(ispreviewing==true)
        // {
        // mcamera.stopPreview();
        // }
        // Parameters mparameters=mcamera.getParameters();
        // Display display=manager.getDefaultDisplay();
        // if(display.getRotation()==Surface.ROTATION_0)
        // {
        // mparameters.setPreviewSize(height, width);
        // mcamera.setDisplayOrientation(90);
        // }
        // if(display.getRotation()==Surface.ROTATION_90)
        // {
        // mparameters.setPreviewSize(width, height);
        // }
        // if(display.getRotation() == Surface.ROTATION_180)
        // {
        // mparameters.setPreviewSize(height, width);
        // }
        // if(display.getRotation() == Surface.ROTATION_270)
        // {
        // mparameters.setPreviewSize(width, height);
        // mcamera.setDisplayOrientation(180);
        // }
        // mcamera.setParameters(mparameters);
        // previewCamera();
    }

    // private void previewCamera()
    // {
    // try {
    // mcamera.setPreviewDisplay(this.holder);
    // mcamera.startPreview();
    // ispreviewing = true;
    // } catch (IOException e) {
    // Log.e("CAMERA DISPLAY", "ERROR");
    // e.printStackTrace();
    // }
    //
    // }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Log.i("CAMERA", "ON SURFACE CREATED");
        try {

            mcamera.setPreviewDisplay(holder);
            ispreviewing = true;
            mcamera.startPreview();
        } catch (IOException e) {
            Log.e("CAMERA OPENING", "ERROR");
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.i("CAMERA", "ON SURFACE DESTROY");
        this.mcamera = null;
        // this.manager=null;
        this.holder = null;
        // mcamera.stopPreview();
        // this.mcamera.release();

    }

    public void takepicture(PictureCallback imageCallback) {
        mcamera.takePicture(null, null, imageCallback);

    }

}

here is my androidmanifest.xml

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.goutam.test_camera"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="16" />

    <uses-permission android:name="android.permission.CAMERA" />

    <uses-feature android:name="android.hardware.camera" />
    <uses-feature
        android:name="android.hardware.camera.autofocus"
        android:required="false" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.goutam.test_camera.CameraclassActivity"
            android:configChanges="orientation|keyboardHidden"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

i am stuck with this thing for very long time but didn't found any solution as i am new to android not able identify the behaviour on android device. but one thing is there the same code is work very well with the htc made android phone.So,can i assume that this is the hardware default which is providing by the manufacturer.If anyone want the .apk file for test i can send to them. email id:[email protected] Sorry for my language and thanks for listening me. Any kind of help will be appreciate.

Upvotes: 3

Views: 439

Answers (1)

VIGNESH
VIGNESH

Reputation: 2033

Actually this happens because , by default the camera of Samsung Galaxy Ace is Landscape . So if you change it to portrait , it automatically rotates the image captured as if the image is captured in the landscape mode .

Find the orientation of the screen ( using the following code )

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) 
{
    super.onRestoreInstanceState(savedInstanceState);
    if (savedInstanceState != null) 
    {
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
        {
            // your code 
        }
        else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
        {
            // your code
        }

        savedInstanceState.getInt("param");
        Log.e("on restore saved state",""+savedInstanceState.getInt("param"));
    }
}

and if it is Portrait

Either rotate the image and save in the SDCard or if you are just displaying it in the imageView , rotate and set to its BackgroundResource

the Following is the code to rotate it to 90 degree ,

public static Bitmap rotate(Bitmap b, int degrees) 
{
    if (degrees != 0 && b != null) 
    {
        Matrix m = new Matrix();

        m.setRotate(degrees, (float) b.getWidth() / 2, (float) b.getHeight() / 2);
        try {
            Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);
            if (b != b2) 
            {
                b.recycle();
                b = b2;
            }
        } catch (OutOfMemoryError ex) 
        {
           throw ex;
        }
    }
    return b;
}

Upvotes: 2

Related Questions