三星手机中定制相机的屏幕方向

时间:2013-01-24 11:26:58

标签: android

在我的应用程序要求自定义相机,所以开发一个,最好的一切工作正常得到图片但是当我拍摄我的三星galaxy王牌Android手机(2.3)在横向模式下它的确定但是在进入肖像模式图片的视图向左移动90度角。正如你在我的代码中看到的那样,我试图改变表面位置,但没有任何反应。 以下是Myactivity类的代码

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

以下是我的surfaceholder用于保存曲面视图的预览类的代码

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

    }

}

这是我的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>
  

我长时间坚持这件事,但没有找到任何东西   解       因为我是新手Android不能识别Android设备上的行为。       但有一点是有相同的代码与htc制作Android手机很好地工作。所以,我可以假设这是硬件   默认由制造商提供。如果有人想要.apk   我可以发送给他们的测试文件。       电子邮件ID:gkundu07@gmail.com       对不起我的语言,感谢您的聆听。       任何形式的帮助都将受到赞赏。

1 个答案:

答案 0 :(得分:2)

实际上这是因为默认三星Galaxy Ace的相机是Landscape。因此,如果您将其更改为纵向,它会自动旋转捕获的图像,就像在横向模式下捕获图像一样。

找到屏幕的方向(使用以下代码)

@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"));
    }
}

如果是肖像

旋转图像并保存在SD卡中,或者如果您只是在imageView中显示它,旋转并设置为其BackgroundResource

以下是将其旋转到90度的代码,

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