Oreintation Screen with Custom Camera in Samsung Mobile Phone

in my custom camera app, so develop one, before that everything works fine getting the image, but when I take pictures with android android phone of galaxy galaxy (2.3) in landscape mode it is ok, but when switching to portrait mode the picture view moves to the left at a 90 degree angle. As you can see in my code, I tried to change the position of the surface, but nothing happened. Here is the code for the 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 surface owner that holds 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've been stuck with this thing for a very long time, but haven't found any solution as I'm new to android, unable to identify the behavior on an android device. but one thing is that the same code works very well with htc cellphone. So, can I assume that this is hardware provided by the manufacturer. If anyone wants a .apk file for a test that I can send them. email id: gkundu07@gmail.com     Sorry for my language and thanks for listening to me. Any help would be appreciated.

+3


source to share


1 answer


This actually happens because the default camera of the Samsung Galaxy Ace is Landscape. Therefore, if you change it to portrait, it will automatically rotate the image as if the image were captured in landscape mode.

Find your screen orientation (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 this is a portrait



Either rotate the image and save it to SDCard, or just show it in the Image ViewView, rotate and set it to BackgroundResource

Below is the code to rotate 90 degrees,

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;
}

      

+2


source







All Articles